use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class HtmlPage method setFocusedElement.
/**
* Moves the focus to the specified element. This will trigger any relevant JavaScript
* event handlers.
*
* @param newElement the element that will receive the focus, use {@code null} to remove focus from any element
* @param windowActivated - whether the enclosing window got focus resulting in specified element getting focus
* @return true if the specified element now has the focus
* @see #getFocusedElement()
*/
public boolean setFocusedElement(final DomElement newElement, final boolean windowActivated) {
if (elementWithFocus_ == newElement && !windowActivated) {
// nothing to do
return true;
}
final DomElement oldFocusedElement = elementWithFocus_;
elementWithFocus_ = null;
if (getWebClient().isJavaScriptEnabled()) {
final Object o = getScriptableObject();
if (o instanceof HTMLDocument) {
((HTMLDocument) o).setActiveElement(null);
}
}
if (!windowActivated) {
if (hasFeature(EVENT_FOCUS_IN_FOCUS_OUT_BLUR)) {
if (oldFocusedElement != null) {
oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
}
if (newElement != null) {
newElement.fireEvent(Event.TYPE_FOCUS_IN);
}
}
if (oldFocusedElement != null) {
oldFocusedElement.removeFocus();
oldFocusedElement.fireEvent(Event.TYPE_BLUR);
if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
oldFocusedElement.fireEvent(Event.TYPE_FOCUS_OUT);
}
}
}
elementWithFocus_ = newElement;
// might be changed by another thread
if (newElement instanceof SelectableTextInput && hasFeature(PAGE_SELECTION_RANGE_FROM_SELECTABLE_TEXT_INPUT)) {
final SelectableTextInput sti = (SelectableTextInput) newElement;
setSelectionRange(new SimpleRange(sti, sti.getSelectionStart(), sti, sti.getSelectionEnd()));
}
if (newElement != null) {
if (getWebClient().isJavaScriptEnabled()) {
final Object o = getScriptableObject();
if (o instanceof HTMLDocument) {
final Object e = newElement.getScriptableObject();
if (e instanceof HTMLElement) {
((HTMLDocument) o).setActiveElement((HTMLElement) e);
}
}
}
newElement.focus();
newElement.fireEvent(Event.TYPE_FOCUS);
if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
newElement.fireEvent(Event.TYPE_FOCUS_IN);
}
}
// element will not have the focus because its page has gone away.
return this == getEnclosingWindow().getEnclosedPage();
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class HtmlImage method fireEvent.
/**
* {@inheritDoc}
*/
@Override
public ScriptResult fireEvent(final Event event) {
if (event instanceof MouseEvent) {
final MouseEvent mouseEvent = (MouseEvent) event;
final HTMLElement scriptableObject = (HTMLElement) getScriptableObject();
if (lastClickX_ >= 0) {
mouseEvent.setClientX(scriptableObject.getPosX() + lastClickX_);
}
if (lastClickY_ >= 0) {
mouseEvent.setClientY(scriptableObject.getPosX() + lastClickY_);
}
}
return super.fireEvent(event);
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class HtmlElement method getDoTypeNode.
/**
* Returns the node to type into.
* @return the node
*/
private DomText getDoTypeNode() {
final HTMLElement scriptElement = getScriptableObject();
if (scriptElement.isIsContentEditable() || "on".equals(((Document) scriptElement.getOwnerDocument()).getDesignMode())) {
DomNodeList<DomNode> children = getChildNodes();
while (!children.isEmpty()) {
final DomNode lastChild = children.get(children.size() - 1);
if (lastChild instanceof DomText) {
return (DomText) lastChild;
}
children = lastChild.getChildNodes();
}
final DomText domText = new DomText(getPage(), "");
appendChild(domText);
return domText;
}
return null;
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class HtmlUnitScriptable method makeScriptableFor.
/**
* Builds a new the JavaScript object that corresponds to the specified object.
* @param domNode the DOM node for which a JS object should be created
* @return the JavaScript object
*/
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
// Get the JS class name for the specified DOM node.
// Walk up the inheritance chain if necessary.
Class<? extends HtmlUnitScriptable> javaScriptClass = null;
if (domNode instanceof HtmlImage && "image".equals(((HtmlImage) domNode).getOriginalQualifiedName()) && ((HtmlImage) domNode).wasCreatedByJavascript()) {
if (domNode.hasFeature(HTMLIMAGE_HTMLELEMENT)) {
javaScriptClass = HTMLElement.class;
} else if (domNode.hasFeature(HTMLIMAGE_HTMLUNKNOWNELEMENT)) {
javaScriptClass = HTMLUnknownElement.class;
}
}
if (javaScriptClass == null) {
final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine();
for (Class<?> c = domNode.getClass(); javaScriptClass == null && c != null; c = c.getSuperclass()) {
javaScriptClass = javaScriptEngine.getJavaScriptClass(c);
}
}
final HtmlUnitScriptable scriptable;
if (javaScriptClass == null) {
// We don't have a specific subclass for this element so create something generic.
scriptable = new HTMLElement();
if (LOG.isDebugEnabled()) {
LOG.debug("No JavaScript class found for element <" + domNode.getNodeName() + ">. Using HTMLElement");
}
} else {
try {
scriptable = javaScriptClass.newInstance();
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(e);
}
}
initParentScope(domNode, scriptable);
scriptable.setPrototype(getPrototype(javaScriptClass));
scriptable.setDomNode(domNode);
return scriptable;
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.
the class Range method getClientRects.
/**
* Retrieves a collection of rectangles that describes the layout of the contents of an object
* or range within the client. Each rectangle describes a single line.
* @return a collection of rectangles that describes the layout of the contents
*/
@JsxFunction
public ClientRectList getClientRects() {
final Window w = getWindow();
final ClientRectList rectList = new ClientRectList();
rectList.setParentScope(w);
rectList.setPrototype(getPrototype(rectList.getClass()));
// simple impl for now
for (final DomNode node : toW3C().containedNodes()) {
final ScriptableObject scriptable = node.getScriptableObject();
if (scriptable instanceof HTMLElement) {
final ClientRect rect = new ClientRect(0, 0, 1, 1);
rect.setParentScope(w);
rect.setPrototype(getPrototype(rect.getClass()));
rectList.add(rect);
}
}
return rectList;
}
Aggregations