Search in sources :

Example 6 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class SvgElementFactory method createElementNS.

/**
 * {@inheritDoc}
 */
@Override
public DomElement createElementNS(final SgmlPage page, final String namespaceURI, String qualifiedNameLC, final Attributes attributes, final boolean checkBrowserCompatibility) {
    final Map<String, DomAttr> attributeMap = toMap(page, attributes);
    qualifiedNameLC = qualifiedNameLC.toLowerCase(Locale.ROOT);
    String tagNameLC = qualifiedNameLC;
    if (tagNameLC.indexOf(':') != -1) {
        tagNameLC = tagNameLC.substring(tagNameLC.indexOf(':') + 1);
    }
    DomElement element = null;
    final Class<?> klass = ELEMENTS_.get(tagNameLC);
    if (klass != null) {
        try {
            element = (DomElement) klass.getDeclaredConstructors()[0].newInstance(namespaceURI, qualifiedNameLC, page, attributeMap);
        } catch (final Exception e) {
            throw new IllegalStateException(e);
        }
    }
    if (element == null) {
        if (page.getWebClient().getBrowserVersion().hasFeature(SVG_UNKNOWN_ARE_DOM)) {
            element = new DomElement(namespaceURI, qualifiedNameLC, page, attributeMap);
        } else {
            element = new SvgElement(namespaceURI, qualifiedNameLC, page, attributeMap);
        }
    }
    return element;
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomElement(com.gargoylesoftware.htmlunit.html.DomElement)

Example 7 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class Attr method detachFromParent.

/**
 * Detaches this attribute from the parent HTML element after caching the attribute value.
 */
public void detachFromParent() {
    final DomAttr domNode = getDomNodeOrDie();
    final DomElement parent = (DomElement) domNode.getParentNode();
    if (parent != null) {
        domNode.setValue(parent.getAttribute(getName()));
    }
    domNode.remove();
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomElement(com.gargoylesoftware.htmlunit.html.DomElement)

Example 8 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project fcrepo by fcrepo.

the class FedoraHtmlResponsesIT method testVersionCreationAndNavigation.

/**
 * This test walks through the steps for creating an object, setting some
 * metadata, creating a version, updating that metadata, viewing the
 * version history to find that old version.
 *
 * @throws IOException exception thrown during this function
 */
@Test
public void testVersionCreationAndNavigation() throws Exception {
    final String pid = newPid();
    createAndVerifyObjectWithIdFromRootPage(pid);
    TimeUnit.SECONDS.sleep(1);
    final String updateSparql = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "PREFIX fedora: <" + REPOSITORY_NAMESPACE + ">\n" + "\n" + "INSERT DATA { <> dc:title \"Object Title\". }";
    postSparqlUpdateUsingHttpClient(updateSparql, pid);
    final HtmlPage objectPage = javascriptlessWebClient.getPage(serverAddress + pid);
    assertEquals("Title should be set.", "Object Title", objectPage.getFirstByXPath("//span[@property='http://purl.org/dc/elements/1.1/title']/text()").toString());
    TimeUnit.SECONDS.sleep(1);
    final String updateSparql2 = "PREFIX dc: <http://purl.org/dc/elements/1.1/>\n" + "\n" + "DELETE { <> dc:title ?t }\n" + "INSERT { <> dc:title \"Updated Title\" }" + "WHERE { <> dc:title ?t }";
    postSparqlUpdateUsingHttpClient(updateSparql2, pid);
    final HtmlPage versions = javascriptlessWebClient.getPage(serverAddress + pid + "/fcr:versions");
    final List<DomAttr> versionLinks = castList(versions.getByXPath("//a[@class='version_link']/@href"));
    assertEquals("There should be three versions.", 3, versionLinks.size());
    // get the labels
    // will look like "Version from 2013-00-0T00:00:00.000Z"
    // and will sort chronologically based on a String comparison
    final List<DomText> labels = castList(versions.getByXPath("//a[@class='version_link']/text()"));
    final boolean chronological = labels.get(0).asText().compareTo(labels.get(1).toString()) < 0;
    logger.debug("Versions {} in chronological order: {}, {}", chronological ? "are" : "are not", labels.get(0).asText(), labels.get(1).asText());
    final HtmlPage firstRevision = javascriptlessWebClient.getPage(versionLinks.get(chronological ? 1 : 2).getNodeValue());
    final List<DomText> v1Titles = castList(firstRevision.getByXPath("//span[@property='http://purl.org/dc/elements/1.1/title']/text()"));
    final HtmlPage secondRevision = javascriptlessWebClient.getPage(versionLinks.get(chronological ? 2 : 1).getNodeValue());
    final List<DomText> v2Titles = castList(secondRevision.getByXPath("//span[@property='http://purl.org/dc/elements/1.1/title']/text()"));
    assertEquals("Version one should have one title.", 1, v1Titles.size());
    assertEquals("Version two should have one title.", 1, v2Titles.size());
    assertNotEquals("Each version should have a different title.", v1Titles.get(0), v2Titles.get(0));
    assertEquals("First version should be preserved.", "Object Title", v1Titles.get(0).getWholeText());
    assertEquals("Second version should be preserved.", "Updated Title", v2Titles.get(0).getWholeText());
}
Also used : DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) DomText(com.gargoylesoftware.htmlunit.html.DomText) Test(org.junit.Test)

Example 9 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr 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)

Example 10 with DomAttr

use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.

the class Element method setAttributeNode.

/**
 * Sets the attribute node for the specified attribute.
 * @param newAtt the attribute to set
 * @return the replaced attribute node, if any
 */
@JsxFunction
public Attr setAttributeNode(final Attr newAtt) {
    final String name = newAtt.getName();
    final NamedNodeMap nodes = getAttributes();
    final Attr replacedAtt = (Attr) nodes.getNamedItemWithoutSytheticClassAttr(name);
    if (replacedAtt != null) {
        replacedAtt.detachFromParent();
    }
    final DomAttr newDomAttr = newAtt.getDomNodeOrDie();
    getDomNodeOrDie().setAttributeNode(newDomAttr);
    return replacedAtt;
}
Also used : NamedNodeMap(com.gargoylesoftware.htmlunit.javascript.NamedNodeMap) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) DomAttr(com.gargoylesoftware.htmlunit.html.DomAttr) Attr(com.gargoylesoftware.htmlunit.javascript.host.dom.Attr) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)17 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)11 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 NamedNodeMap (org.w3c.dom.NamedNodeMap)3 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)2 DomText (com.gargoylesoftware.htmlunit.html.DomText)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 Attr (com.gargoylesoftware.htmlunit.javascript.host.dom.Attr)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 DomCharacterData (com.gargoylesoftware.htmlunit.html.DomCharacterData)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)1 ElementFactory (com.gargoylesoftware.htmlunit.html.ElementFactory)1 HtmlDivision (com.gargoylesoftware.htmlunit.html.HtmlDivision)1