use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class SvgElementFactory method createElementNS.
/**
* {@inheritDoc}
*/
@Override
public DomElement createElementNS(final SgmlPage page, final String namespaceURI, String qualifiedNameLC, final Attributes attributes, final boolean checkBrowserCompatibility) {
final Map<String, DomAttr> attributeMap = toMap(page, attributes);
qualifiedNameLC = qualifiedNameLC.toLowerCase(Locale.ROOT);
String tagNameLC = qualifiedNameLC;
if (tagNameLC.indexOf(':') != -1) {
tagNameLC = tagNameLC.substring(tagNameLC.indexOf(':') + 1);
}
DomElement element = null;
final Class<?> klass = ELEMENTS_.get(tagNameLC);
if (klass != null) {
try {
element = (DomElement) klass.getDeclaredConstructors()[0].newInstance(namespaceURI, qualifiedNameLC, page, attributeMap);
} catch (final Exception e) {
throw new IllegalStateException(e);
}
}
if (element == null) {
if (page.getWebClient().getBrowserVersion().hasFeature(SVG_UNKNOWN_ARE_DOM)) {
element = new DomElement(namespaceURI, qualifiedNameLC, page, attributeMap);
} else {
element = new SvgElement(namespaceURI, qualifiedNameLC, page, attributeMap);
}
}
return element;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class Attr method detachFromParent.
/**
* Detaches this attribute from the parent HTML element after caching the attribute value.
*/
public void detachFromParent() {
final DomAttr domNode = getDomNodeOrDie();
final DomElement parent = (DomElement) domNode.getParentNode();
if (parent != null) {
domNode.setValue(parent.getAttribute(getName()));
}
domNode.remove();
}
use of com.gargoylesoftware.htmlunit.html.DomAttr 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());
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class Element method printNode.
protected void printNode(final StringBuilder builder, final DomNode node, final boolean html) {
if (node instanceof DomComment) {
if (html) {
// Remove whitespace sequences.
final String s = PRINT_NODE_PATTERN.matcher(node.getNodeValue()).replaceAll(" ");
builder.append("<!--").append(s).append("-->");
}
} else if (node instanceof DomCharacterData) {
// Remove whitespace sequences, possibly escape XML characters.
String s = node.getNodeValue();
if (html) {
s = com.gargoylesoftware.htmlunit.util.StringUtils.escapeXmlChars(s);
}
builder.append(s);
} else if (html) {
final DomElement element = (DomElement) node;
final Element scriptObject = node.getScriptableObject();
final String tag = element.getTagName();
Element htmlElement = null;
if (scriptObject instanceof HTMLElement) {
htmlElement = scriptObject;
}
builder.append('<').append(tag);
// Add the attributes. IE does not use quotes, FF does.
for (final DomAttr attr : element.getAttributesMap().values()) {
if (!attr.getSpecified()) {
continue;
}
final String name = attr.getName();
final String value = PRINT_NODE_QUOTE_PATTERN.matcher(attr.getValue()).replaceAll(""");
builder.append(' ').append(name).append('=');
builder.append('\"');
builder.append(value);
builder.append('\"');
}
builder.append('>');
// Add the children.
final boolean isHtml = html && !(scriptObject instanceof HTMLScriptElement) && !(scriptObject instanceof HTMLStyleElement);
printChildren(builder, node, isHtml);
if (null == htmlElement || !htmlElement.isEndTagForbidden()) {
builder.append("</").append(tag).append('>');
}
} else {
if (node instanceof HtmlElement) {
final HtmlElement element = (HtmlElement) node;
if ("p".equals(element.getTagName())) {
if (getBrowserVersion().hasFeature(JS_INNER_HTML_LF)) {
// \r\n because it's to implement something IE specific
builder.append('\n');
} else {
int i = builder.length() - 1;
while (i >= 0 && Character.isWhitespace(builder.charAt(i))) {
i--;
}
builder.setLength(i + 1);
builder.append('\n');
}
}
if (!"script".equals(element.getTagName())) {
printChildren(builder, node, html);
}
}
}
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class Element method setAttributeNode.
/**
* Sets the attribute node for the specified attribute.
* @param newAtt the attribute to set
* @return the replaced attribute node, if any
*/
@JsxFunction
public Attr setAttributeNode(final Attr newAtt) {
final String name = newAtt.getName();
final NamedNodeMap nodes = getAttributes();
final Attr replacedAtt = (Attr) nodes.getNamedItemWithoutSytheticClassAttr(name);
if (replacedAtt != null) {
replacedAtt.detachFromParent();
}
final DomAttr newDomAttr = newAtt.getDomNodeOrDie();
getDomNodeOrDie().setAttributeNode(newDomAttr);
return replacedAtt;
}
Aggregations