use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method click.
/**
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
*
* Simulates clicking on this element, returning the page in the window that has the focus
* after the element has been clicked. Note that the returned page may or may not be the same
* as the original page, depending on the type of element being clicked, the presence of JavaScript
* action listeners, etc.
*
* @param shiftKey {@code true} if SHIFT is pressed during the click
* @param ctrlKey {@code true} if CTRL is pressed during the click
* @param altKey {@code true} if ALT is pressed during the click
* @param triggerMouseEvents if true trigger the mouse events also
* @param handleFocus if true set the focus (and trigger the event)
* @param ignoreVisibility whether to ignore visibility or not
* @param disableProcessLabelAfterBubbling ignore label processing
* @param <P> the page type
* @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
* @exception IOException if an IO error occurs
*/
@SuppressWarnings("unchecked")
public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean triggerMouseEvents, final boolean handleFocus, final boolean ignoreVisibility, final boolean disableProcessLabelAfterBubbling) throws IOException {
// make enclosing window the current one
final SgmlPage page = getPage();
page.getWebClient().setCurrentWindow(page.getEnclosingWindow());
if (!ignoreVisibility) {
if (!(page instanceof HtmlPage)) {
return (P) page;
}
if (!isDisplayed()) {
if (LOG.isWarnEnabled()) {
LOG.warn("Calling click() ignored because the target element '" + this + "' is not displayed.");
}
return (P) page;
}
if (isDisabledElementAndDisabled()) {
if (LOG.isWarnEnabled()) {
LOG.warn("Calling click() ignored because the target element '" + this + "' is disabled.");
}
return (P) page;
}
}
synchronized (page) {
if (triggerMouseEvents) {
mouseDown(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
}
if (handleFocus) {
// give focus to current element (if possible) or only remove it from previous one
DomElement elementToFocus = null;
if (this instanceof SubmittableElement || this instanceof HtmlAnchor && ATTRIBUTE_NOT_DEFINED != ((HtmlAnchor) this).getHrefAttribute() || this instanceof HtmlArea && (ATTRIBUTE_NOT_DEFINED != ((HtmlArea) this).getHrefAttribute() || getPage().getWebClient().getBrowserVersion().hasFeature(JS_AREA_WITHOUT_HREF_FOCUSABLE)) || this instanceof HtmlElement && ((HtmlElement) this).getTabIndex() != null) {
elementToFocus = this;
} else if (this instanceof HtmlOption) {
elementToFocus = ((HtmlOption) this).getEnclosingSelect();
}
if (elementToFocus == null) {
((HtmlPage) page).setFocusedElement(null);
} else {
elementToFocus.focus();
}
}
if (triggerMouseEvents) {
mouseUp(shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
}
MouseEvent event = null;
if (page.getWebClient().isJavaScriptEnabled()) {
final BrowserVersion browser = page.getWebClient().getBrowserVersion();
if (browser.hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
if (browser.hasFeature(EVENT_ONCLICK_POINTEREVENT_DETAIL_0)) {
event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
} else {
event = new PointerEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 1);
}
} else {
event = new MouseEvent(getEventTargetElement(), MouseEvent.TYPE_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
}
if (disableProcessLabelAfterBubbling) {
event.disableProcessLabelAfterBubbling();
}
}
return click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method dblClick.
/**
* Simulates double-clicking on this element, returning the page in the window that has the focus
* after the element has been clicked. Note that the returned page may or may not be the same
* as the original page, depending on the type of element being clicked, the presence of JavaScript
* action listeners, etc. Note also that {@link #click(boolean, boolean, boolean)} is automatically
* called first.
*
* @param shiftKey {@code true} if SHIFT is pressed during the double-click
* @param ctrlKey {@code true} if CTRL is pressed during the double-click
* @param altKey {@code true} if ALT is pressed during the double-click
* @param <P> the page type
* @return the page that occupies this element's window after the element has been double-clicked
* @exception IOException if an IO error occurs
*/
@SuppressWarnings("unchecked")
public <P extends Page> P dblClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey) throws IOException {
if (isDisabledElementAndDisabled()) {
return (P) getPage();
}
// call click event first
P clickPage = click(shiftKey, ctrlKey, altKey);
if (clickPage != getPage()) {
if (LOG.isDebugEnabled()) {
LOG.debug("dblClick() is ignored, as click() loaded a different page.");
}
return clickPage;
}
// call click event a second time
clickPage = click(shiftKey, ctrlKey, altKey);
if (clickPage != getPage()) {
if (LOG.isDebugEnabled()) {
LOG.debug("dblClick() is ignored, as click() loaded a different page.");
}
return clickPage;
}
final Event event;
final WebClient webClient = getPage().getWebClient();
if (webClient.getBrowserVersion().hasFeature(EVENT_ONDOUBLECLICK_USES_POINTEREVENT)) {
event = new PointerEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
} else {
event = new MouseEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
}
final ScriptResult scriptResult = fireEvent(event);
if (scriptResult == null) {
return clickPage;
}
return (P) webClient.getCurrentWindow().getEnclosedPage();
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent in project htmlunit by HtmlUnit.
the class Document method createEvent.
/**
* Implementation of the {@link org.w3c.dom.events.DocumentEvent} interface's
* {@link org.w3c.dom.events.DocumentEvent#createEvent(String)} method. The method creates an
* uninitialized event of the specified type.
*
* @see <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-DocumentEvent">DocumentEvent</a>
* @param eventType the event type to create
* @return an event object for the specified type
* @throws DOMException if the event type is not supported (will have a type of
* DOMException.NOT_SUPPORTED_ERR)
*/
@JsxFunction
public Event createEvent(final String eventType) throws DOMException {
Class<? extends Event> clazz = SUPPORTED_DOM2_EVENT_TYPE_MAP.get(eventType);
if (clazz == null) {
clazz = SUPPORTED_DOM3_EVENT_TYPE_MAP.get(eventType);
if (CloseEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONCLOSE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
clazz = null;
}
}
if (clazz == null && ("Events".equals(eventType) || "HashChangeEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_HASHCHANGEEVENT) || "BeforeUnloadEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_BEFOREUNLOADEVENT) || "MouseWheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_MOUSEWHEELEVENT) || "PointerEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_POINTEREVENT) || "PopStateEvent".equals(eventType) || "ProgressEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_PROGRESSEVENT) || "FocusEvent".equals(eventType) || "WheelEvent".equals(eventType) && getBrowserVersion().hasFeature(EVENT_TYPE_WHEELEVENT))) {
clazz = SUPPORTED_VENDOR_EVENT_TYPE_MAP.get(eventType);
if (PopStateEvent.class == clazz && getBrowserVersion().hasFeature(EVENT_ONPOPSTATE_DOCUMENT_CREATE_NOT_SUPPORTED)) {
clazz = null;
}
}
if (clazz == null) {
Context.throwAsScriptRuntimeEx(new DOMException(DOMException.NOT_SUPPORTED_ERR, "Event Type is not supported: " + eventType));
// to stop eclipse warning
return null;
}
try {
final Event event = clazz.newInstance();
event.setParentScope(getWindow());
event.setPrototype(getPrototype(clazz));
event.eventCreated();
return event;
} catch (final InstantiationException | IllegalAccessException e) {
throw Context.reportRuntimeError("Failed to instantiate event: class ='" + clazz.getName() + "' for event type of '" + eventType + "': " + e.getMessage());
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method doMouseEvent.
/**
* Simulates the specified mouse event, returning the page which this element's window contains after the event.
* The returned page may or may not be the same as the original page, depending on JavaScript event handlers, etc.
*
* @param eventType the mouse event type to simulate
* @param shiftKey {@code true} if SHIFT is pressed during the mouse event
* @param ctrlKey {@code true} if CTRL is pressed during the mouse event
* @param altKey {@code true} if ALT is pressed during the mouse event
* @param button the button code, must be {@link MouseEvent#BUTTON_LEFT}, {@link MouseEvent#BUTTON_MIDDLE}
* or {@link MouseEvent#BUTTON_RIGHT}
* @return the page which this element's window contains after the event
*/
private Page doMouseEvent(final String eventType, final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final int button) {
final SgmlPage page = getPage();
if (!page.getWebClient().isJavaScriptEnabled()) {
return page;
}
final ScriptResult scriptResult;
final Event event;
if (MouseEvent.TYPE_CONTEXT_MENU.equals(eventType) && getPage().getWebClient().getBrowserVersion().hasFeature(EVENT_ONCLICK_USES_POINTEREVENT)) {
event = new PointerEvent(this, eventType, shiftKey, ctrlKey, altKey, button, 0);
} else {
event = new MouseEvent(this, eventType, shiftKey, ctrlKey, altKey, button);
}
scriptResult = fireEvent(event);
final Page currentPage;
if (scriptResult == null) {
currentPage = page;
} else {
currentPage = page.getWebClient().getCurrentWindow().getEnclosedPage();
}
final boolean mouseOver = !MouseEvent.TYPE_MOUSE_OUT.equals(eventType);
if (mouseOver_ != mouseOver) {
mouseOver_ = mouseOver;
page.clearComputedStyles();
}
return currentPage;
}
Aggregations