Search in sources :

Example 16 with DomText

use of com.gargoylesoftware.htmlunit.html.DomText 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 17 with DomText

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

the class Element method insertAdjacentText.

/**
 * Inserts the given text into the element at the specified location.
 * @param where specifies where to insert the text, using one of the following values (case-insensitive):
 *      beforebegin, afterbegin, beforeend, afterend
 * @param text the text to insert
 *
 * @see <a href="http://msdn.microsoft.com/en-us/library/ie/ms536453.aspx">MSDN</a>
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void insertAdjacentText(final String where, final String text) {
    final Object[] values = getInsertAdjacentLocation(where);
    final DomNode node = (DomNode) values[0];
    final boolean append = ((Boolean) values[1]).booleanValue();
    final DomText domText = new DomText(node.getPage(), text);
    // add the new nodes
    if (append) {
        node.appendChild(domText);
    } else {
        node.insertBefore(domText);
    }
}
Also used : ProxyDomNode(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement.ProxyDomNode) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 18 with DomText

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

the class XMLDOMDocument method createTextNode.

/**
 * Creates a text node that contains the supplied data.
 * @param data the value to be supplied to the new text object's <code>nodeValue</code> property
 * @return the new text object or <code>NOT_FOUND</code> if there is an error
 */
@JsxFunction
public Object createTextNode(final String data) {
    Object result = NOT_FOUND;
    try {
        final DomText domText = new DomText(getPage(), data);
        final Object jsElement = getScriptableFor(domText);
        if (jsElement == NOT_FOUND) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("createTextNode(" + data + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domText.getClass().getName());
            }
        } else {
            result = jsElement;
        }
    } catch (final ElementNotFoundException e) {
    // Just fall through - result is already set to NOT_FOUND
    }
    return result;
}
Also used : DomText(com.gargoylesoftware.htmlunit.html.DomText) ElementNotFoundException(com.gargoylesoftware.htmlunit.ElementNotFoundException) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 19 with DomText

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

the class XMLDOMElement method getElementsByTagName.

/**
 * Returns a list of all descendant elements that match the supplied name.
 * @param tagName the name of the element to find; the tagName value '*' matches all descendant elements of this
 *     element
 * @return a list containing all elements that match the supplied name
 */
@JsxFunction
public XMLDOMNodeList getElementsByTagName(final String tagName) {
    if (tagName == null || "null".equals(tagName)) {
        throw Context.reportRuntimeError("Type mismatch.");
    }
    final String tagNameTrimmed = tagName.trim();
    if (elementsByTagName_ == null) {
        elementsByTagName_ = new HashMap<>();
    }
    XMLDOMNodeList collection = elementsByTagName_.get(tagNameTrimmed);
    if (collection != null) {
        return collection;
    }
    final DomNode node = getDomNodeOrDie();
    final String description = "XMLDOMElement.getElementsByTagName('" + tagNameTrimmed + "')";
    if ("*".equals(tagNameTrimmed)) {
        collection = new XMLDOMNodeList(node, false, description) {

            @Override
            protected boolean isMatching(final DomNode domNode) {
                return true;
            }
        };
    } else if ("".equals(tagNameTrimmed)) {
        collection = new XMLDOMNodeList(node, false, description) {

            @Override
            protected List<DomNode> computeElements() {
                final List<DomNode> response = new ArrayList<>();
                final DomNode domNode = getDomNodeOrNull();
                if (domNode == null) {
                    return response;
                }
                for (final DomNode candidate : getCandidates()) {
                    if (candidate instanceof DomText) {
                        final DomText domText = (DomText) candidate;
                        if (!StringUtils.isBlank(domText.getWholeText())) {
                            response.add(candidate);
                        }
                    } else {
                        response.add(candidate);
                    }
                }
                return response;
            }
        };
    } else {
        collection = new XMLDOMNodeList(node, false, description) {

            @Override
            protected boolean isMatching(final DomNode domNode) {
                return tagNameTrimmed.equals(domNode.getNodeName());
            }
        };
    }
    elementsByTagName_.put(tagName, collection);
    return collection;
}
Also used : DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) ArrayList(java.util.ArrayList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 20 with DomText

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

the class XmlUtils method copy.

/**
 * Copy all children from 'source' to 'dest', within the context of the specified page.
 * @param page the page which the nodes belong to
 * @param source the node to copy from
 * @param dest the node to copy to
 * @param handleXHTMLAsHTML if true elements from the XHTML namespace are handled as HTML elements instead of
 *     DOM elements
 */
private static void copy(final SgmlPage page, final Node source, final DomNode dest, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
    final NodeList nodeChildren = source.getChildNodes();
    for (int i = 0; i < nodeChildren.getLength(); i++) {
        final Node child = nodeChildren.item(i);
        switch(child.getNodeType()) {
            case Node.ELEMENT_NODE:
                final DomNode childXml = createFrom(page, child, handleXHTMLAsHTML, attributesOrderMap);
                dest.appendChild(childXml);
                copy(page, child, childXml, handleXHTMLAsHTML, attributesOrderMap);
                break;
            case Node.TEXT_NODE:
                dest.appendChild(new DomText(page, child.getNodeValue()));
                break;
            case Node.CDATA_SECTION_NODE:
                dest.appendChild(new DomCDataSection(page, child.getNodeValue()));
                break;
            case Node.COMMENT_NODE:
                dest.appendChild(new DomComment(page, child.getNodeValue()));
                break;
            case Node.PROCESSING_INSTRUCTION_NODE:
                dest.appendChild(new DomProcessingInstruction(page, child.getNodeName(), child.getNodeValue()));
                break;
            default:
                if (LOG.isWarnEnabled()) {
                    LOG.warn("NodeType " + child.getNodeType() + " (" + child.getNodeName() + ") is not yet supported.");
                }
        }
    }
}
Also used : DomCDataSection(com.gargoylesoftware.htmlunit.html.DomCDataSection) DomComment(com.gargoylesoftware.htmlunit.html.DomComment) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomText(com.gargoylesoftware.htmlunit.html.DomText) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DeferredNode(org.apache.xerces.dom.DeferredNode) DomProcessingInstruction(com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)

Aggregations

DomText (com.gargoylesoftware.htmlunit.html.DomText)23 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)15 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)5 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)3 ArrayList (java.util.ArrayList)3 NodeList (org.w3c.dom.NodeList)3 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)2 DomAttr (com.gargoylesoftware.htmlunit.html.DomAttr)2 DomCDataSection (com.gargoylesoftware.htmlunit.html.DomCDataSection)2 DomComment (com.gargoylesoftware.htmlunit.html.DomComment)2 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 DomProcessingInstruction (com.gargoylesoftware.htmlunit.html.DomProcessingInstruction)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 Node (com.gargoylesoftware.htmlunit.javascript.host.dom.Node)2 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)2 Test (org.junit.Test)2 DomDocumentFragment (com.gargoylesoftware.htmlunit.html.DomDocumentFragment)1 DomDocumentType (com.gargoylesoftware.htmlunit.html.DomDocumentType)1