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