Search in sources :

Example 1 with JavaScriptLoadResult

use of com.gargoylesoftware.htmlunit.html.HtmlPage.JavaScriptLoadResult in project htmlunit by HtmlUnit.

the class ScriptElementSupport method executeScriptIfNeeded.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Executes this script node if necessary and/or possible.
 * @param element the element
 * @param ignoreAttachedToPage don't do the isAttachedToPage check
 * @param ignorePageIsAncestor don't do the element.getPage().isAncestorOf(element) check
 */
public static void executeScriptIfNeeded(final DomElement element, final boolean ignoreAttachedToPage, final boolean ignorePageIsAncestor) {
    if (!isExecutionNeeded(element, ignoreAttachedToPage, ignorePageIsAncestor)) {
        return;
    }
    final ScriptElement scriptElement = (ScriptElement) element;
    final String src = scriptElement.getSrcAttribute();
    if (src.equals(SLASH_SLASH_COLON)) {
        executeEvent(element, Event.TYPE_ERROR);
        return;
    }
    final HtmlPage page = (HtmlPage) element.getPage();
    if (src != ATTRIBUTE_NOT_DEFINED) {
        if (!src.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
            // <script src="[url]"></script>
            if (LOG.isDebugEnabled()) {
                LOG.debug("Loading external JavaScript: " + src);
            }
            try {
                scriptElement.setExecuted(true);
                Charset charset = EncodingSniffer.toCharset(scriptElement.getCharsetAttribute());
                if (charset == null) {
                    charset = page.getCharset();
                }
                final JavaScriptLoadResult result;
                final Window win = page.getEnclosingWindow().getScriptableObject();
                final Document doc = win.getDocument();
                try {
                    doc.setCurrentScript(element.getScriptableObject());
                    result = page.loadExternalJavaScriptFile(src, charset);
                } finally {
                    doc.setCurrentScript(null);
                }
                if (result == JavaScriptLoadResult.SUCCESS) {
                    executeEvent(element, Event.TYPE_LOAD);
                } else if (result == JavaScriptLoadResult.DOWNLOAD_ERROR) {
                    executeEvent(element, Event.TYPE_ERROR);
                } else if (result == JavaScriptLoadResult.NO_CONTENT) {
                    final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
                    if (browserVersion.hasFeature(JS_SCRIPT_HANDLE_204_AS_ERROR)) {
                        executeEvent(element, Event.TYPE_ERROR);
                    } else {
                        executeEvent(element, Event.TYPE_LOAD);
                    }
                }
            } catch (final FailingHttpStatusCodeException e) {
                executeEvent(element, Event.TYPE_ERROR);
                throw e;
            }
        }
    } else if (element.getFirstChild() != null) {
        // <script>[code]</script>
        final Window win = page.getEnclosingWindow().getScriptableObject();
        final Document doc = win.getDocument();
        try {
            doc.setCurrentScript(element.getScriptableObject());
            executeInlineScriptIfNeeded(element);
        } finally {
            doc.setCurrentScript(null);
        }
        if (element.hasFeature(EVENT_ONLOAD_INTERNAL_JAVASCRIPT)) {
            executeEvent(element, Event.TYPE_LOAD);
        }
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) Charset(java.nio.charset.Charset) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) JavaScriptLoadResult(com.gargoylesoftware.htmlunit.html.HtmlPage.JavaScriptLoadResult)

Aggregations

BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 JavaScriptLoadResult (com.gargoylesoftware.htmlunit.html.HtmlPage.JavaScriptLoadResult)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 Document (com.gargoylesoftware.htmlunit.javascript.host.dom.Document)1 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)1 Charset (java.nio.charset.Charset)1