Search in sources :

Example 56 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class Document method createElement.

/**
 * Creates a new element with the given tag name.
 *
 * @param tagName the tag name
 * @return the new HTML element, or NOT_FOUND if the tag is not supported
 */
@JsxFunction
public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
        if (tagName.contains("<") || tagName.contains(">")) {
            if (LOG.isInfoEnabled()) {
                LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character; '<' and '>' are not allowed");
            }
            throw Context.reportRuntimeError("String contains an invalid character");
        } else if (tagName.length() > 0 && tagName.charAt(0) == '<' && tagName.endsWith(">")) {
            tagName = tagName.substring(1, tagName.length() - 1);
            final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
            if (!matcher.matches()) {
                if (LOG.isInfoEnabled()) {
                    LOG.info("createElement: Provided string '" + tagName + "' contains an invalid character");
                }
                throw Context.reportRuntimeError("String contains an invalid character");
            }
        }
        final SgmlPage page = getPage();
        org.w3c.dom.Node element = page.createElement(tagName);
        if (element instanceof HtmlInput) {
            ((HtmlInput) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlImage) {
            ((HtmlImage) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRp) {
            ((HtmlRp) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRt) {
            ((HtmlRt) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlUnknownElement) {
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlSvg) {
            element = UnknownElementFactory.instance.createElementNS(page, "", "svg", null);
            ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
        final Object jsElement = getScriptableFor(element);
        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createElement(" + tagName + ") cannot return a result as there isn't a JavaScript object for the element " + element.getClass().getName());
            }
        } else {
            result = jsElement;
        }
    } catch (final ElementNotFoundException e) {
    // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) Matcher(java.util.regex.Matcher) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) HtmlRp(com.gargoylesoftware.htmlunit.html.HtmlRp) HtmlUnknownElement(com.gargoylesoftware.htmlunit.html.HtmlUnknownElement) HtmlSvg(com.gargoylesoftware.htmlunit.html.HtmlSvg) HtmlRt(com.gargoylesoftware.htmlunit.html.HtmlRt) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 57 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction 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());
    }
}
Also used : PopStateEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PopStateEvent) CloseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CloseEvent) DOMException(org.w3c.dom.DOMException) CustomEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CustomEvent) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) PopStateEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PopStateEvent) SVGZoomEvent(com.gargoylesoftware.htmlunit.javascript.host.event.SVGZoomEvent) ProgressEvent(com.gargoylesoftware.htmlunit.javascript.host.event.ProgressEvent) FocusEvent(com.gargoylesoftware.htmlunit.javascript.host.event.FocusEvent) CompositionEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CompositionEvent) WheelEvent(com.gargoylesoftware.htmlunit.javascript.host.event.WheelEvent) KeyboardEvent(com.gargoylesoftware.htmlunit.javascript.host.event.KeyboardEvent) PointerEvent(com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) TextEvent(com.gargoylesoftware.htmlunit.javascript.host.event.TextEvent) CloseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.CloseEvent) MouseWheelEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseWheelEvent) HashChangeEvent(com.gargoylesoftware.htmlunit.javascript.host.event.HashChangeEvent) MutationEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MutationEvent) UIEvent(com.gargoylesoftware.htmlunit.javascript.host.event.UIEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) DragEvent(com.gargoylesoftware.htmlunit.javascript.host.event.DragEvent) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 58 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class Document method createDocumentFragment.

/**
 * Creates a new document fragment.
 * @return a newly created document fragment
 */
@JsxFunction
public Object createDocumentFragment() {
    final DomDocumentFragment fragment = getDomNodeOrDie().getPage().createDocumentFragment();
    final DocumentFragment node = new DocumentFragment();
    node.setParentScope(getParentScope());
    node.setPrototype(getPrototype(node.getClass()));
    node.setDomNode(fragment);
    return getScriptableFor(fragment);
}
Also used : DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) DomDocumentFragment(com.gargoylesoftware.htmlunit.html.DomDocumentFragment) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 59 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class StyleMedia method matchMedium.

/**
 * Returns whether the specified media is supported by the object that displays the document object.
 * @param media the media query
 * @return whether the specified media is supported or not
 */
@JsxFunction
public boolean matchMedium(final String media) {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
    return CSSStyleSheet.isActive(this, mediaList);
}
Also used : CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 60 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class Range method compareBoundaryPoints.

/**
 * Compares the boundary points of two Ranges.
 * @param how a constant describing the comparison method
 * @param sourceRange the Range to compare boundary points with this range
 * @return -1, 0, or 1, indicating whether the corresponding boundary-point of range is respectively before,
 * equal to, or after the corresponding boundary-point of sourceRange.
 */
@JsxFunction
public Object compareBoundaryPoints(final int how, final Range sourceRange) {
    final Node nodeForThis;
    final int offsetForThis;
    final int containingMoficator;
    if (START_TO_START == how || END_TO_START == how) {
        nodeForThis = startContainer_;
        offsetForThis = startOffset_;
        containingMoficator = 1;
    } else {
        nodeForThis = endContainer_;
        offsetForThis = endOffset_;
        containingMoficator = -1;
    }
    final Node nodeForOther;
    final int offsetForOther;
    if (START_TO_END == how || START_TO_START == how) {
        nodeForOther = sourceRange.startContainer_;
        offsetForOther = sourceRange.startOffset_;
    } else {
        nodeForOther = sourceRange.endContainer_;
        offsetForOther = sourceRange.endOffset_;
    }
    if (nodeForThis == nodeForOther) {
        if (offsetForThis < offsetForOther) {
            return Integer.valueOf(-1);
        } else if (offsetForThis > offsetForOther) {
            return Integer.valueOf(1);
        }
        return Integer.valueOf(0);
    }
    final byte nodeComparision = (byte) nodeForThis.compareDocumentPosition(nodeForOther);
    if ((nodeComparision & Node.DOCUMENT_POSITION_CONTAINED_BY) != 0) {
        return Integer.valueOf(-1 * containingMoficator);
    } else if ((nodeComparision & Node.DOCUMENT_POSITION_PRECEDING) != 0) {
        return Integer.valueOf(-1);
    }
    // TODO: handle other cases!
    return Integer.valueOf(1);
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)133 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)30 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)20 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)19 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)16 URL (java.net.URL)14 IOException (java.io.IOException)11 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)11 WebClient (com.gargoylesoftware.htmlunit.WebClient)10 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)10 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)10 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)10 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)8 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)8 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)8 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)8 Range (org.w3c.dom.ranges.Range)8 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)7