com.gargoylesoftware.htmlunit.html.HtmlScript Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlScript. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Document.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code scripts} property.
 * @return the value of the {@code scripts} property
 */
@JsxGetter({CHROME, IE})
public Object getScripts() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlScript;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.scripts is not a function");
        }
    };
}
 
Example #2
Source File: HTMLScriptElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@code src} property.
 * @return the {@code src} property
 */
@JsxGetter
public String getSrc() {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    String src = tmpScript.getSrcAttribute();
    if (ATTRIBUTE_NOT_DEFINED == src) {
        return src;
    }
    try {
        final URL expandedSrc = ((HtmlPage) tmpScript.getPage()).getFullyQualifiedUrl(src);
        src = expandedSrc.toString();
    }
    catch (final MalformedURLException e) {
        // ignore
    }
    return src;
}
 
Example #3
Source File: JavaScriptEngineTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts("got here")
public void externalScript() throws Exception {
    final String html
        = "<html><head><title>foo</title><script src='/foo.js' id='script1'/>\n"
        + "</head><body>\n"
        + "<p>hello world</p>\n"
        + "<form name='form1'>\n"
        + "  <input type='text' name='textfield1' id='textfield1' value='foo' />\n"
        + "  <input type='text' name='textfield2' id='textfield2'/>\n"
        + "</form>\n"
        + "</body></html>";

    final String jsContent = "alert('got here');\n";

    getMockWebConnection().setResponse(new URL(URL_FIRST, "foo.js"), jsContent,
            "text/javascript");

    final HtmlPage page = loadPageWithAlerts(html);
    final HtmlScript htmlScript = page.getHtmlElementById("script1");
    assertNotNull(htmlScript);
}
 
Example #4
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the {@code scripts} property.
 * @return the value of the {@code scripts} property
 */
@JsxGetter({CHROME, IE})
public Object getScripts() {
    return new HTMLCollection(getDomNodeOrDie(), false) {
        @Override
        protected boolean isMatching(final DomNode node) {
            return node instanceof HtmlScript;
        }

        @Override
        public Object call(final Context cx, final Scriptable scope,
                final Scriptable thisObj, final Object[] args) {
            if (getBrowserVersion().hasFeature(JS_DOCUMENT_FORMS_FUNCTION_SUPPORTED)) {
                return super.call(cx, scope, thisObj, args);
            }
            throw Context.reportRuntimeError("TypeError: document.scripts is not a function");
        }
    };
}
 
Example #5
Source File: HTMLScriptElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@code src} property.
 * @return the {@code src} property
 */
@JsxGetter
public String getSrc() {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    String src = tmpScript.getSrcAttribute();
    if (ATTRIBUTE_NOT_DEFINED == src) {
        return src;
    }
    try {
        final URL expandedSrc = ((HtmlPage) tmpScript.getPage()).getFullyQualifiedUrl(src);
        src = expandedSrc.toString();
    }
    catch (final MalformedURLException e) {
        // ignore
    }
    return src;
}
 
Example #6
Source File: HTMLScriptElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} property.
 * @param text the {@code text} property
 */
@JsxSetter
public void setText(final String text) {
    final HtmlElement htmlElement = getDomNodeOrDie();
    htmlElement.removeAllChildren();
    final DomNode textChild = new DomText(htmlElement.getPage(), text);
    htmlElement.appendChild(textChild);

    final HtmlScript tmpScript = (HtmlScript) htmlElement;
    tmpScript.executeScriptIfNeeded();
}
 
Example #7
Source File: HTMLScriptElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Overwritten for special IE handling.
 *
 * @param childObject the node to add to this node
 * @return the newly added child node
 */
@Override
public Object appendChild(final Object childObject) {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    final boolean wasEmpty = tmpScript.getFirstChild() == null;
    final Object result = super.appendChild(childObject);

    if (wasEmpty) {
        tmpScript.executeScriptIfNeeded();
    }
    return result;
}
 
Example #8
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the node that is the last one when exploring following nodes, depth-first.
 * @param node the node to search
 * @return the searched node
 */
HtmlElement getLastHtmlElement(final HtmlElement node) {
    final DomNode lastChild = node.getLastChild();
    if (lastChild == null
            || !(lastChild instanceof HtmlElement)
            || lastChild instanceof HtmlScript) {
        return node;
    }

    return getLastHtmlElement((HtmlElement) lastChild);
}
 
Example #9
Source File: SvgScriptTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void assertType(final String expectedAlert, final DomElement element) {
    if ("[object SVGScriptElement]".equals(expectedAlert)) {
        assertTrue(SvgScript.class.isInstance(element));
    }
    else {
        assertTrue(HtmlScript.class.isInstance(element));
    }
}
 
Example #10
Source File: HTMLScriptElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the {@code text} property.
 * @param text the {@code text} property
 */
@JsxSetter
public void setText(final String text) {
    final HtmlElement htmlElement = getDomNodeOrDie();
    htmlElement.removeAllChildren();
    final DomNode textChild = new DomText(htmlElement.getPage(), text);
    htmlElement.appendChild(textChild);

    final HtmlScript tmpScript = (HtmlScript) htmlElement;
    tmpScript.executeScriptIfNeeded();
}
 
Example #11
Source File: HTMLScriptElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Overwritten for special IE handling.
 *
 * @param childObject the node to add to this node
 * @return the newly added child node
 */
@Override
public Object appendChild(final Object childObject) {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    final boolean wasEmpty = tmpScript.getFirstChild() == null;
    final Object result = super.appendChild(childObject);

    if (wasEmpty) {
        tmpScript.executeScriptIfNeeded();
    }
    return result;
}
 
Example #12
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the node that is the last one when exploring following nodes, depth-first.
 * @param node the node to search
 * @return the searched node
 */
HtmlElement getLastHtmlElement(final HtmlElement node) {
    final DomNode lastChild = node.getLastChild();
    if (lastChild == null
            || !(lastChild instanceof HtmlElement)
            || lastChild instanceof HtmlScript) {
        return node;
    }

    return getLastHtmlElement((HtmlElement) lastChild);
}
 
Example #13
Source File: Document.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * 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 BaseFrameElement) {
            ((BaseFrameElement) element).markAsCreatedByJavascript();
        }
        else 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 HtmlScript) {
            ((HtmlScript) 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;
}
 
Example #14
Source File: Document.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * 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 {
        final BrowserVersion browserVersion = getBrowserVersion();

        if (browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_STRICT)
              && (tagName.contains("<") || tagName.contains(">"))) {
            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.startsWith("<") && tagName.endsWith(">")) {
            tagName = tagName.substring(1, tagName.length() - 1);

            final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
            if (!matcher.matches()) {
                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 BaseFrameElement) {
            ((BaseFrameElement) element).markAsCreatedByJavascript();
        }
        else 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 HtmlScript) {
            ((HtmlScript) 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;
}
 
Example #15
Source File: HTMLScriptElement.java    From htmlunit with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the ready state of the script. This is an IE-only property.
 * @return the ready state of the script
 * @see DomNode#READY_STATE_UNINITIALIZED
 * @see DomNode#READY_STATE_LOADING
 * @see DomNode#READY_STATE_LOADED
 * @see DomNode#READY_STATE_INTERACTIVE
 * @see DomNode#READY_STATE_COMPLETE
 */
@JsxGetter(IE)
public Object getReadyState() {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    if (tmpScript.wasCreatedByJavascript()) {
        return Undefined.instance;
    }
    return tmpScript.getReadyState();
}
 
Example #16
Source File: HTMLScriptElement.java    From HtmlUnit-Android with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the ready state of the script. This is an IE-only property.
 * @return the ready state of the script
 * @see DomNode#READY_STATE_UNINITIALIZED
 * @see DomNode#READY_STATE_LOADING
 * @see DomNode#READY_STATE_LOADED
 * @see DomNode#READY_STATE_INTERACTIVE
 * @see DomNode#READY_STATE_COMPLETE
 */
@JsxGetter(IE)
public Object getReadyState() {
    final HtmlScript tmpScript = (HtmlScript) getDomNodeOrDie();
    if (tmpScript.wasCreatedByJavascript()) {
        return Undefined.instance;
    }
    return tmpScript.getReadyState();
}