use of com.gargoylesoftware.htmlunit.javascript.host.Window 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;
}
use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.
the class TextRange method parentElement.
/**
* Retrieves the parent element for the given text range.
* The parent element is the element that completely encloses the text in the range.
* If the text range spans text in more than one element, this method returns the smallest element that encloses
* all the elements. When you insert text into a range that spans multiple elements, the text is placed in the
* parent element rather than in any of the contained elements.
*
* @see <a href="http://msdn.microsoft.com/en-us/library/ms536654.aspx">MSDN doc</a>
* @return the parent element object if successful, or null otherwise.
*/
@JsxFunction
public Node parentElement() {
final org.w3c.dom.Node parent = range_.getCommonAncestorContainer();
if (null == parent) {
if (null == range_.getStartContainer() || null == range_.getEndContainer()) {
try {
final Window window = (Window) getParentScope();
final HtmlPage page = (HtmlPage) window.getDomNodeOrDie();
return (Node) getScriptableFor(page.getBody());
} catch (final Exception e) {
// ok bad luck
}
}
return null;
}
return (Node) getScriptableFor(parent);
}
use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.
the class MessageEvent method jsConstructor.
/**
* JavaScript constructor.
*
* @param type the event type
* @param details the event details (optional)
*/
@Override
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
public void jsConstructor(final String type, final ScriptableObject details) {
super.jsConstructor(type, details);
if (getBrowserVersion().hasFeature(EVENT_ONMESSAGE_DEFAULT_DATA_NULL)) {
data_ = null;
}
String origin = "";
String lastEventId = "";
if (details != null && !Undefined.isUndefined(details)) {
data_ = details.get("data");
final String detailOrigin = (String) details.get(HttpHeader.ORIGIN_LC);
if (detailOrigin != null) {
origin = detailOrigin;
}
final Object detailLastEventId = details.get("lastEventId");
if (detailLastEventId != null) {
lastEventId = Context.toString(detailLastEventId);
}
source_ = null;
final Object detailSource = details.get("source");
if (detailSource instanceof Window) {
source_ = (Window) detailSource;
} else if (detailSource instanceof WindowProxy) {
source_ = ((WindowProxy) detailSource).getDelegee();
}
ports_ = details.get("ports");
}
origin_ = origin;
lastEventId_ = lastEventId;
}
use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.
the class UIEvent method jsConstructor.
/**
* JavaScript constructor.
*
* @param type the event type
* @param details the event details (optional)
*/
@JsxConstructor({ CHROME, EDGE, FF, FF_ESR })
@Override
public void jsConstructor(final String type, final ScriptableObject details) {
super.jsConstructor(type, details);
view_ = NO_VIEW;
if (details != null && !Undefined.isUndefined(details)) {
final Object view = details.get("view", details);
if (view instanceof Window) {
view_ = view;
} else if (view != Scriptable.NOT_FOUND) {
throw ScriptRuntime.typeError("View must be a window.");
}
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.Window in project htmlunit by HtmlUnit.
the class EventListenersContainer method executeEventListeners.
private void executeEventListeners(final int eventPhase, final Event event, final Object[] args) {
final DomNode node = jsNode_.getDomNodeOrNull();
// some event don't apply on all kind of nodes, for instance "blur"
if (node != null && !node.handles(event)) {
return;
}
final TypeContainer container = getTypeContainer(event.getType());
final List<Scriptable> listeners = container.getListeners(eventPhase);
if (!listeners.isEmpty()) {
event.setCurrentTarget(jsNode_);
final HtmlPage page;
if (jsNode_ instanceof Window) {
page = (HtmlPage) jsNode_.getDomNodeOrDie();
} else {
final Scriptable parentScope = jsNode_.getParentScope();
if (parentScope instanceof Window) {
page = (HtmlPage) ((Window) parentScope).getDomNodeOrDie();
} else if (parentScope instanceof HTMLDocument) {
page = ((HTMLDocument) parentScope).getPage();
} else {
page = ((HTMLElement) parentScope).getDomNodeOrDie().getHtmlPageOrNull();
}
}
// no need for a copy, listeners are copy on write
for (Scriptable listener : listeners) {
boolean isPropertyHandler = false;
if (listener == TypeContainer.EVENT_HANDLER_PLACEHOLDER) {
listener = container.handler_;
isPropertyHandler = true;
}
Function function = null;
Scriptable thisObject = null;
if (listener instanceof Function) {
function = (Function) listener;
thisObject = jsNode_;
} else if (listener instanceof NativeObject) {
final Object handleEvent = ScriptableObject.getProperty(listener, "handleEvent");
if (handleEvent instanceof Function) {
function = (Function) handleEvent;
thisObject = listener;
}
}
if (function != null) {
final ScriptResult result = page.executeJavaScriptFunction(function, thisObject, args, node);
// Return value is only honored for property handlers (Tested in Chrome/FF/IE11)
if (isPropertyHandler && !ScriptResult.isUndefined(result)) {
event.handlePropertyHandlerReturnValue(result.getJavaScriptResult());
}
}
if (event.isImmediatePropagationStopped()) {
return;
}
}
}
}
Aggregations