Search in sources :

Example 21 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project jenkins by jenkinsci.

the class ViewTest method newJob_twoLetterIcon.

@Test
public void newJob_twoLetterIcon() throws Exception {
    CustomizableTLID customizableTLID = j.jenkins.getExtensionList(TopLevelItemDescriptor.class).get(CustomizableTLID.class);
    customizableTLID.customId = "two-letters-desc";
    customizableTLID.customDisplayName = "Two words";
    customizableTLID.customIconClassName = null;
    customizableTLID.customIconFilePathPattern = null;
    JenkinsRule.WebClient wc = j.createWebClient();
    HtmlPage page = wc.goTo("view/all/newJob");
    Object result = page.executeJavaScript("document.querySelector('." + customizableTLID.customId + " .default-icon')").getJavaScriptResult();
    assertThat(result, instanceOf(HTMLElement.class));
    HTMLElement resultHtml = (HTMLElement) result;
    HTMLElement spanA = (HTMLElement) resultHtml.getFirstElementChild();
    HTMLElement spanB = (HTMLElement) resultHtml.getLastElementChild();
    assertThat(spanA.getClassName_js(), is("a"));
    assertThat(spanA.getInnerText(), is("T"));
    assertThat(spanB.getClassName_js(), is("b"));
    assertThat(spanB.getInnerText(), is("w"));
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Test(org.junit.Test)

Example 22 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project jenkins by jenkinsci.

the class ViewTest method newJob_xssPreventedInId.

@Test
@Issue("SECURITY-2171")
public void newJob_xssPreventedInId() throws Exception {
    CustomizableTLID customizableTLID = j.jenkins.getExtensionList(TopLevelItemDescriptor.class).get(CustomizableTLID.class);
    customizableTLID.customId = "regularclass\" onclick=alert(123) other=\"";
    customizableTLID.customDisplayName = "DN-xss-id";
    JenkinsRule.WebClient wc = j.createWebClient();
    HtmlPage page = wc.goTo("view/all/newJob");
    Object result = page.executeJavaScript("Array.from(document.querySelectorAll('.label')).filter(el => el.innerText.indexOf('" + customizableTLID.customDisplayName + "') !== -1)[0].parentElement.parentElement").getJavaScriptResult();
    assertThat(result, instanceOf(HTMLElement.class));
    HTMLElement resultElement = (HTMLElement) result;
    assertThat(resultElement.getAttribute("onclick", null), nullValue());
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 23 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.

the class HtmlAnchor method click.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public <P extends Page> P click(final Event event, final boolean shiftKey, final boolean ctrlKey, final boolean altKey, final boolean ignoreVisibility) throws IOException {
    WebWindow oldWebWindow = null;
    if (ctrlKey) {
        oldWebWindow = ((HTMLElement) event.getSrcElement()).getDomNodeOrDie().getPage().getWebClient().getCurrentWindow();
    }
    P page = super.click(event, shiftKey, ctrlKey, altKey, ignoreVisibility);
    if (ctrlKey) {
        page.getEnclosingWindow().getWebClient().setCurrentWindow(oldWebWindow);
        page = (P) oldWebWindow.getEnclosedPage();
    }
    return page;
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 24 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.

the class HtmlTableRowTest method scriptCanSetJsPropertyOnCell.

/**
 * Ensure that setting a property via script sets the property on the
 * ScriptableObject that we think it should.
 */
@Test
public void scriptCanSetJsPropertyOnCell() {
    final String cmd = "document.getElementById('cell').a = 'original'; document.getElementById('cell')";
    final Object object = page_.executeJavaScript(cmd).getJavaScriptResult();
    final HTMLElement jselement = (HTMLElement) object;
    assertEquals("original", ScriptableObject.getProperty(jselement, "a"));
    assertSame(jselement, cell_.getScriptableObject());
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Test(org.junit.Test)

Example 25 with HTMLElement

use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement in project htmlunit by HtmlUnit.

the class Element method printNode.

protected void printNode(final StringBuilder builder, final DomNode node, final boolean html) {
    if (node instanceof DomComment) {
        if (html) {
            // Remove whitespace sequences.
            final String s = PRINT_NODE_PATTERN.matcher(node.getNodeValue()).replaceAll(" ");
            builder.append("<!--").append(s).append("-->");
        }
    } else if (node instanceof DomCharacterData) {
        // Remove whitespace sequences, possibly escape XML characters.
        String s = node.getNodeValue();
        if (html) {
            s = com.gargoylesoftware.htmlunit.util.StringUtils.escapeXmlChars(s);
        }
        builder.append(s);
    } else if (html) {
        final DomElement element = (DomElement) node;
        final Element scriptObject = node.getScriptableObject();
        final String tag = element.getTagName();
        Element htmlElement = null;
        if (scriptObject instanceof HTMLElement) {
            htmlElement = scriptObject;
        }
        builder.append('<').append(tag);
        // Add the attributes. IE does not use quotes, FF does.
        for (final DomAttr attr : element.getAttributesMap().values()) {
            if (!attr.getSpecified()) {
                continue;
            }
            final String name = attr.getName();
            final String value = PRINT_NODE_QUOTE_PATTERN.matcher(attr.getValue()).replaceAll("&quot;");
            builder.append(' ').append(name).append('=');
            builder.append('\"');
            builder.append(value);
            builder.append('\"');
        }
        builder.append('>');
        // Add the children.
        final boolean isHtml = html && !(scriptObject instanceof HTMLScriptElement) && !(scriptObject instanceof HTMLStyleElement);
        printChildren(builder, node, isHtml);
        if (null == htmlElement || !htmlElement.isEndTagForbidden()) {
            builder.append("</").append(tag).append('>');
        }
    } else {
        if (node instanceof HtmlElement) {
            final HtmlElement element = (HtmlElement) node;
            if ("p".equals(element.getTagName())) {
                if (getBrowserVersion().hasFeature(JS_INNER_HTML_LF)) {
                    // \r\n because it's to implement something IE specific
                    builder.append('\n');
                } else {
                    int i = builder.length() - 1;
                    while (i >= 0 && Character.isWhitespace(builder.charAt(i))) {
                        i--;
                    }
                    builder.setLength(i + 1);
                    builder.append('\n');
                }
            }
            if (!"script".equals(element.getTagName())) {
                printChildren(builder, node, html);
            }
        }
    }
}
Also used : DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HTMLScriptElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLScriptElement) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLTemplateElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTemplateElement) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) DomCharacterData(com.gargoylesoftware.htmlunit.html.DomCharacterData)

Aggregations

HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)35 AttributedString (java.text.AttributedString)10 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)7 Test (org.junit.Test)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)6 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)5 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)5 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)4 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)4 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)4 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)4 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)4 HTMLUnknownElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement)4 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)3 Element (com.gargoylesoftware.htmlunit.javascript.host.Element)3 HTMLBodyElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement)3 HTMLCanvasElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement)3