Search in sources :

Example 1 with NativeArray

use of net.sourceforge.htmlunit.corejs.javascript.NativeArray in project htmlunit by HtmlUnit.

the class MutationObserver method attributeReplaced.

/**
 * {@inheritDoc}
 */
@Override
public void attributeReplaced(final HtmlAttributeChangeEvent event) {
    final HtmlElement target = event.getHtmlElement();
    if (subtree_ || target == node_.getDomNodeOrDie()) {
        final String attributeName = event.getName();
        if (attributeFilter_ == null || attributeFilter_.contains(attributeName)) {
            final MutationRecord mutationRecord = new MutationRecord();
            final Scriptable scope = getParentScope();
            mutationRecord.setParentScope(scope);
            mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
            mutationRecord.setAttributeName(attributeName);
            mutationRecord.setType("attributes");
            mutationRecord.setTarget(target.getScriptableObject());
            if (attributeOldValue_) {
                mutationRecord.setOldValue(event.getValue());
            }
            final Window window = getWindow();
            final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
            final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
            jsEngine.addPostponedAction(new PostponedAction(owningPage, "MutationObserver.attributeReplaced") {

                @Override
                public void execute() throws Exception {
                    final NativeArray array = new NativeArray(new Object[] { mutationRecord });
                    ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array);
                    jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] { array });
                }
            });
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 2 with NativeArray

use of net.sourceforge.htmlunit.corejs.javascript.NativeArray in project jenkins by jenkinsci.

the class HeteroListTest method xssPrevented_usingToolInstallation_withJustDisplayName.

// correspond to the hardening of escapeEntryTitleAndDescription
@Test
@Issue("SECURITY-2035")
public void xssPrevented_usingToolInstallation_withJustDisplayName() throws Exception {
    JenkinsRule.WebClient wc = j.createWebClient();
    HtmlPage page = wc.goTo("configureTools/");
    // check the displayName
    Object resultDN = page.executeJavaScript("var settingFields = document.querySelectorAll('.jenkins-form-label');" + "var children = Array.from(settingFields).filter(b => b.textContent.indexOf('XSS:') !== -1)[0].children;" + "Array.from(children).filter(c => c.tagName === 'IMG')").getJavaScriptResult();
    assertThat(resultDN, instanceOf(NativeArray.class));
    NativeArray resultDNNA = (NativeArray) resultDN;
    assertEquals(0, resultDNNA.size());
    // check the description
    Object resultDesc = page.executeJavaScript("var settingFields = document.querySelectorAll('.jenkins-form-description');" + "var children = Array.from(settingFields).filter(b => b.textContent.indexOf('XSS:') !== -1)[0].children;" + "Array.from(children).filter(c => c.tagName === 'IMG')").getJavaScriptResult();
    assertThat(resultDesc, instanceOf(NativeArray.class));
    NativeArray resultDescNA = (NativeArray) resultDesc;
    assertEquals(0, resultDescNA.size());
}
Also used : NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 3 with NativeArray

use of net.sourceforge.htmlunit.corejs.javascript.NativeArray in project htmlunit by HtmlUnit.

the class DateTimeFormat method jsConstructor.

/**
 * JavaScript constructor.
 * @param cx the current context
 * @param args the arguments to the WebSocket constructor
 * @param ctorObj the function object
 * @param inNewExpr Is new or not
 * @return the java object to allow JavaScript to access
 */
@JsxConstructor
public static Scriptable jsConstructor(final Context cx, final Object[] args, final Function ctorObj, final boolean inNewExpr) {
    final String[] locales;
    if (args.length != 0) {
        if (args[0] instanceof NativeArray) {
            final NativeArray array = (NativeArray) args[0];
            locales = new String[(int) array.getLength()];
            for (int i = 0; i < locales.length; i++) {
                locales[i] = Context.toString(array.get(i));
            }
        } else {
            locales = new String[] { Context.toString(args[0]) };
        }
    } else {
        locales = new String[] { "" };
    }
    final Window window = getWindow(ctorObj);
    final DateTimeFormat format = new DateTimeFormat(locales, window.getBrowserVersion());
    format.setParentScope(window);
    format.setPrototype(window.getPrototype(format.getClass()));
    return format;
}
Also used : NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)

Example 4 with NativeArray

use of net.sourceforge.htmlunit.corejs.javascript.NativeArray in project htmlunit by HtmlUnit.

the class MutationObserver method characterDataChanged.

/**
 * {@inheritDoc}
 */
@Override
public void characterDataChanged(final CharacterDataChangeEvent event) {
    final ScriptableObject target = event.getCharacterData().getScriptableObject();
    if (subtree_ || target == node_) {
        final MutationRecord mutationRecord = new MutationRecord();
        final Scriptable scope = getParentScope();
        mutationRecord.setParentScope(scope);
        mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
        mutationRecord.setType("characterData");
        mutationRecord.setTarget(target);
        if (characterDataOldValue_) {
            mutationRecord.setOldValue(event.getOldValue());
        }
        final Window window = getWindow();
        final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
        jsEngine.addPostponedAction(new PostponedAction(owningPage, "MutationObserver.characterDataChanged") {

            @Override
            public void execute() throws Exception {
                final NativeArray array = new NativeArray(new Object[] { mutationRecord });
                ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array);
                jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] { array });
            }
        });
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

NativeArray (net.sourceforge.htmlunit.corejs.javascript.NativeArray)4 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)3 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)2 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)2 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)2 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)2 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)1 Test (org.junit.Test)1 Issue (org.jvnet.hudson.test.Issue)1 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)1