use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.
the class HTMLTitleElement method setText.
/**
* Sets the {@code text} attribute.
* @param text the {@code text} attribute
*/
@JsxSetter
public void setText(final String text) {
final DomNode htmlElement = getDomNodeOrDie();
DomNode firstChild = htmlElement.getFirstChild();
if (firstChild == null) {
firstChild = new DomText(htmlElement.getPage(), text);
htmlElement.appendChild(firstChild);
} else {
firstChild.setNodeValue(text);
}
}
use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.
the class XmlUtils method createFrom.
private static DomNode createFrom(final SgmlPage page, final Node source, final boolean handleXHTMLAsHTML, final Map<Integer, List<String>> attributesOrderMap) {
if (source.getNodeType() == Node.TEXT_NODE) {
return new DomText(page, source.getNodeValue());
}
if (source.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
return new DomProcessingInstruction(page, source.getNodeName(), source.getNodeValue());
}
if (source.getNodeType() == Node.COMMENT_NODE) {
return new DomComment(page, source.getNodeValue());
}
if (source.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
final DocumentType documentType = (DocumentType) source;
return new DomDocumentType(page, documentType.getName(), documentType.getPublicId(), documentType.getSystemId());
}
final String ns = source.getNamespaceURI();
String localName = source.getLocalName();
if (handleXHTMLAsHTML && Html.XHTML_NAMESPACE.equals(ns)) {
final ElementFactory factory = page.getWebClient().getPageCreator().getHtmlParser().getFactory(localName);
return factory.createElementNS(page, ns, localName, namedNodeMapToSaxAttributes(source.getAttributes(), attributesOrderMap, source));
}
final NamedNodeMap nodeAttributes = source.getAttributes();
if (page != null && page.isHtmlPage()) {
localName = localName.toUpperCase(Locale.ROOT);
}
final String qualifiedName;
if (source.getPrefix() == null) {
qualifiedName = localName;
} else {
qualifiedName = source.getPrefix() + ':' + localName;
}
final String namespaceURI = source.getNamespaceURI();
if (Html.SVG_NAMESPACE.equals(namespaceURI)) {
return page.getWebClient().getPageCreator().getHtmlParser().getSvgFactory().createElementNS(page, namespaceURI, qualifiedName, namedNodeMapToSaxAttributes(nodeAttributes, attributesOrderMap, source));
}
final Map<String, DomAttr> attributes = new LinkedHashMap<>();
for (int i = 0; i < nodeAttributes.getLength(); i++) {
final int orderedIndex = getIndex(nodeAttributes, attributesOrderMap, source, i);
final Attr attribute = (Attr) nodeAttributes.item(orderedIndex);
final String attributeNamespaceURI = attribute.getNamespaceURI();
final String attributeQualifiedName;
if (attribute.getPrefix() == null) {
attributeQualifiedName = attribute.getLocalName();
} else {
attributeQualifiedName = attribute.getPrefix() + ':' + attribute.getLocalName();
}
final String value = attribute.getNodeValue();
final boolean specified = attribute.getSpecified();
final DomAttr xmlAttribute = new DomAttr(page, attributeNamespaceURI, attributeQualifiedName, value, specified);
attributes.put(attribute.getNodeName(), xmlAttribute);
}
return new DomElement(namespaceURI, qualifiedName, page, attributes);
}
use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.
the class XMLDOMAttribute method initTextNode.
private void initTextNode() {
if (textNode_ == null) {
final String value = getValue();
if (!org.apache.commons.lang3.StringUtils.isEmpty(value)) {
final DomText text = new DomText(getDomNodeOrDie().getPage(), value);
getDomNodeOrDie().appendChild(text);
textNode_ = text.getScriptableObject();
}
}
}
use of com.gargoylesoftware.htmlunit.html.DomText in project htmlunit by HtmlUnit.
the class HtmlUnitNekoDOMBuilder method handleCharacters.
/**
* Picks up the character data accumulated so far and add it to the current element as a text node.
*/
private void handleCharacters() {
if (characters_ != null && characters_.length() != 0) {
if (currentNode_ instanceof HtmlHtml) {
// In HTML, the <html> node only has two possible children:
// the <head> and the <body>; any text is ignored.
characters_.setLength(0);
} else {
// Use the normal behavior: append a text node for the accumulated text.
final String textValue = characters_.toString();
final DomText text = new DomText(page_, textValue);
characters_.setLength(0);
if (StringUtils.isNotBlank(textValue)) {
// malformed HTML: </td>some text</tr> => text comes before the table
if (currentNode_ instanceof HtmlTableRow) {
final HtmlTableRow row = (HtmlTableRow) currentNode_;
final HtmlTable enclosingTable = row.getEnclosingTable();
if (enclosingTable != null) {
// may be null when called from Range.createContextualFragment
if (enclosingTable.getPreviousSibling() instanceof DomText) {
final DomText domText = (DomText) enclosingTable.getPreviousSibling();
domText.setTextContent(domText + textValue);
} else {
enclosingTable.insertBefore(text);
}
}
} else if (currentNode_ instanceof HtmlTable) {
final HtmlTable enclosingTable = (HtmlTable) currentNode_;
if (enclosingTable.getPreviousSibling() instanceof DomText) {
final DomText domText = (DomText) enclosingTable.getPreviousSibling();
domText.setTextContent(domText + textValue);
} else {
enclosingTable.insertBefore(text);
}
} else if (currentNode_ instanceof HtmlImage) {
currentNode_.setNextSibling(text);
} else {
appendChild(currentNode_, text);
}
} else {
appendChild(currentNode_, text);
}
}
}
}
use of com.gargoylesoftware.htmlunit.html.DomText 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());
}
Aggregations