Search in sources :

Example 26 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class HTMLElement method setInnerText.

/**
 * Replaces all child elements of this element with the supplied text value.
 * (see https://html.spec.whatwg.org/multipage/dom.html#the-innertext-idl-attribute)
 * @param value the new value for the contents of this element
 */
@JsxSetter
public void setInnerText(final Object value) {
    final String valueString;
    if (value == null && getBrowserVersion().hasFeature(JS_INNER_TEXT_VALUE_NULL)) {
        valueString = null;
    } else {
        valueString = Context.toString(value);
    }
    final DomNode domNode = getDomNodeOrDie();
    final SgmlPage page = domNode.getPage();
    domNode.removeAllChildren();
    if (StringUtils.isNotEmpty(valueString)) {
        final String[] parts = valueString.split("\\r?\\n");
        for (int i = 0; i < parts.length; i++) {
            if (i != 0) {
                domNode.appendChild(page.createElement(HtmlBreak.TAG_NAME));
            }
            domNode.appendChild(new DomText(page, parts[i]));
        }
    }
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 27 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class Element method getElementsByTagName.

/**
 * Returns all the descendant elements with the specified tag name.
 * @param tagName the name to search for
 * @return all the descendant elements with the specified tag name
 */
@JsxFunction
public HTMLCollection getElementsByTagName(final String tagName) {
    if (elementsByTagName_ == null) {
        elementsByTagName_ = new HashMap<>();
    }
    final String searchTagName;
    final boolean caseSensitive;
    final DomNode dom = getDomNodeOrNull();
    if (dom == null) {
        searchTagName = tagName.toLowerCase(Locale.ROOT);
        caseSensitive = false;
    } else {
        final SgmlPage page = dom.getPage();
        if (page != null && page.hasCaseSensitiveTagNames()) {
            searchTagName = tagName;
            caseSensitive = true;
        } else {
            searchTagName = tagName.toLowerCase(Locale.ROOT);
            caseSensitive = false;
        }
    }
    HTMLCollection collection = elementsByTagName_.get(searchTagName);
    if (collection != null) {
        return collection;
    }
    final DomNode node = getDomNodeOrDie();
    if ("*".equals(tagName)) {
        collection = new HTMLCollection(node, false) {

            @Override
            protected boolean isMatching(final DomNode nodeToMatch) {
                return true;
            }
        };
    } else {
        collection = new HTMLCollection(node, false) {

            @Override
            protected boolean isMatching(final DomNode nodeToMatch) {
                if (caseSensitive) {
                    return searchTagName.equals(nodeToMatch.getNodeName());
                }
                return searchTagName.equalsIgnoreCase(nodeToMatch.getNodeName());
            }
        };
    }
    elementsByTagName_.put(tagName, collection);
    return collection;
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HTMLCollection(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 28 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class XMLDOMNode method getOwnerDocument.

/**
 * Returns the root of the document that contains the node.
 * @return the root of the document that contains the node
 */
@JsxGetter
public Object getOwnerDocument() {
    final DomNode domNode = getDomNodeOrDie();
    final Object document = domNode.getOwnerDocument();
    if (document == null) {
        return null;
    }
    return ((SgmlPage) document).getScriptableObject();
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 29 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class XMLSerializer method serializeToString.

/**
 * The subtree rooted by the specified element is serialized to a string.
 * @param root the root of the subtree to be serialized (this may be any node, even a document)
 * @return the serialized string
 */
@JsxFunction
public String serializeToString(Node root) {
    if (root == null) {
        return "";
    }
    if (root instanceof DocumentFragment) {
        if (root.getOwnerDocument() instanceof HTMLDocument && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_HTML_DOCUMENT_FRAGMENT_ALWAYS_EMPTY)) {
            return "";
        }
        Node node = root.getFirstChild();
        if (node == null) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();
        while (node != null) {
            builder.append(serializeToString(node));
            node = node.getNextSibling();
        }
        return builder.toString();
    }
    if (root instanceof Document) {
        root = ((Document) root).getDocumentElement();
    }
    if (root instanceof Element) {
        final StringBuilder builder = new StringBuilder();
        final DomNode node = root.getDomNodeOrDie();
        final SgmlPage page = node.getPage();
        final boolean isHtmlPage = page != null && page.isHtmlPage();
        String forcedNamespace = null;
        if (isHtmlPage) {
            forcedNamespace = "http://www.w3.org/1999/xhtml";
        }
        toXml(1, node, builder, forcedNamespace);
        return builder.toString();
    }
    if (root instanceof CDATASection && getBrowserVersion().hasFeature(JS_XML_SERIALIZER_ROOT_CDATA_AS_ESCAPED_TEXT)) {
        final DomCDataSection domCData = (DomCDataSection) root.getDomNodeOrDie();
        final String data = domCData.getData();
        if (org.apache.commons.lang3.StringUtils.isNotBlank(data)) {
            return StringUtils.escapeXmlChars(data);
        }
    }
    return root.getDomNodeOrDie().asXml();
}
Also used : CDATASection(com.gargoylesoftware.htmlunit.javascript.host.dom.CDATASection) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Node(com.gargoylesoftware.htmlunit.javascript.host.dom.Node) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) Document(com.gargoylesoftware.htmlunit.javascript.host.dom.Document) DocumentFragment(com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 30 with SgmlPage

use of com.gargoylesoftware.htmlunit.SgmlPage in project htmlunit by HtmlUnit.

the class XSLTProcessor method transform.

/**
 * @return {@link Node} or {@link String}
 */
private Object transform(final Node source) {
    try {
        final DomNode sourceDomNode = source.getDomNodeOrDie();
        Source xmlSource = new DOMSource(sourceDomNode);
        final DomNode xsltDomNode = style_.getDomNodeOrDie();
        final Source xsltSource = new DOMSource(xsltDomNode);
        final Transformer transformer = TransformerFactory.newInstance().newTransformer(xsltSource);
        for (final Map.Entry<String, Object> entry : parameters_.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
        // hack to preserve indention
        // the transformer only accepts the OutputKeys.INDENT setting if
        // the StreamResult is used
        final SgmlPage page = sourceDomNode.getPage();
        if (page != null && page.getWebClient().getBrowserVersion().hasFeature(JS_XSLT_TRANSFORM_INDENT)) {
            final DomNode outputNode = findOutputNode(xsltDomNode);
            if (outputNode != null) {
                final org.w3c.dom.Node indentNode = outputNode.getAttributes().getNamedItem("indent");
                if (indentNode != null && "yes".equalsIgnoreCase(indentNode.getNodeValue())) {
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                        transformer.transform(xmlSource, new StreamResult(out));
                        final WebResponseData data = new WebResponseData(out.toByteArray(), 200, null, Collections.emptyList());
                        final WebResponse response = new WebResponse(data, null, 0);
                        return XmlUtils.buildDocument(response);
                    }
                }
            }
        }
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        final org.w3c.dom.Document containerDocument = factory.newDocumentBuilder().newDocument();
        final org.w3c.dom.Element containerElement = containerDocument.createElement("container");
        containerDocument.appendChild(containerElement);
        final DOMResult result = new DOMResult(containerElement);
        transformer.transform(xmlSource, result);
        final org.w3c.dom.Node transformedNode = result.getNode();
        if (transformedNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
            return transformedNode;
        }
        // output is not DOM (text)
        xmlSource = new DOMSource(source.getDomNodeOrDie());
        final StringWriter writer = new StringWriter();
        final Result streamResult = new StreamResult(writer);
        transformer.transform(xmlSource, streamResult);
        return writer.toString();
    } catch (final Exception e) {
        throw Context.reportRuntimeError("Exception: " + e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) StringWriter(java.io.StringWriter) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) WebResponseData(com.gargoylesoftware.htmlunit.WebResponseData) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)38 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)7 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)5 DomText (com.gargoylesoftware.htmlunit.html.DomText)5 Page (com.gargoylesoftware.htmlunit.Page)4 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)4 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)3 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)3 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)2 HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)2 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)2 DocumentFragment (com.gargoylesoftware.htmlunit.javascript.host.dom.DocumentFragment)2 MouseEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent)2 PointerEvent (com.gargoylesoftware.htmlunit.javascript.host.event.PointerEvent)2