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]));
}
}
}
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);
}
}
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;
}
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;
}
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.");
}
}
}
}
Aggregations