use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class HTMLElement method readAttributes.
/**
* Gets the attributes of the element in the form of a {@link org.xml.sax.Attributes}.
* @param element the element to read the attributes from
* @return the attributes
*/
protected AttributesImpl readAttributes(final HtmlElement element) {
final AttributesImpl attributes = new AttributesImpl();
for (final DomAttr entry : element.getAttributesMap().values()) {
final String name = entry.getName();
final String value = entry.getValue();
attributes.addAttribute(null, name, name, null, value);
}
return attributes;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class WindowConcurrencyTest method concurrentModificationException_computedStyles.
/**
* Regression test for
* <a href="http://sourceforge.net/support/tracker.php?aid=2820051">bug 2820051</a>.
* @throws Exception if the test fails
*/
@Test
public void concurrentModificationException_computedStyles() throws Exception {
final String html = "<html><head><script>\n" + "function test() {\n" + " getComputedStyle(document.body, null);\n" + "}\n" + "</script></head><body onload='test()'>\n" + "<iframe src='foo.html' name='myFrame' id='myFrame'></iframe>\n" + "</body></html>";
final String html2 = "<html><head><script>\n" + "function forceStyleComputationInParent() {\n" + " var newNode = parent.document.createElement('span');\n" + " parent.document.body.appendChild(newNode);\n" + " parent.getComputedStyle(newNode, null);\n" + "}\n" + "setInterval(forceStyleComputationInParent, 10);\n" + "</script></head></body></html>";
try (WebClient client = new WebClient(BrowserVersion.FIREFOX)) {
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(URL_FIRST, html);
webConnection.setDefaultResponse(html2);
client.setWebConnection(webConnection);
final HtmlPage page1 = client.getPage(URL_FIRST);
// Recreating what can occur with two threads requires
// to know a bit about the style invalidation used in Window.DomHtmlAttributeChangeListenerImpl
final HtmlElement elt = new HtmlDivision("div", page1, new HashMap<String, DomAttr>()) {
@Override
public DomNode getParentNode() {
// this gets called by CSS invalidation logic
try {
// enough to let setInterval run
Thread.sleep(1000);
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
return super.getParentNode();
}
};
page1.getBody().appendChild(elt);
}
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class XMLDOMElement method setAttributeNode.
/**
* Sets or updates the supplied attribute node on this element.
* @param newAtt the attribute node to be associated with this element
* @return the replaced attribute node, if any, {@code null} otherwise
*/
@JsxFunction
public XMLDOMAttribute setAttributeNode(final XMLDOMAttribute newAtt) {
if (newAtt == null) {
throw Context.reportRuntimeError("Type mismatch.");
}
final String name = newAtt.getBaseName();
final XMLDOMNamedNodeMap nodes = (XMLDOMNamedNodeMap) getAttributes();
final XMLDOMAttribute replacedAtt = (XMLDOMAttribute) nodes.getNamedItemWithoutSyntheticClassAttr(name);
if (replacedAtt != null) {
replacedAtt.detachFromParent();
}
final DomAttr newDomAttr = newAtt.getDomNodeOrDie();
getDomNodeOrDie().setAttributeNode(newDomAttr);
return replacedAtt;
}
use of com.gargoylesoftware.htmlunit.html.DomAttr in project htmlunit by HtmlUnit.
the class Element method setDomNode.
/**
* Sets the DOM node that corresponds to this JavaScript object.
* @param domNode the DOM node
*/
@Override
public void setDomNode(final DomNode domNode) {
super.setDomNode(domNode);
setParentScope(getWindow().getDocument());
// CSSStyleDeclaration uses the parent scope
style_ = new CSSStyleDeclaration(this);
// Convert JavaScript snippets defined in the attribute map to executable event handlers.
// Should be called only on construction.
final DomElement htmlElt = (DomElement) domNode;
for (final DomAttr attr : htmlElt.getAttributesMap().values()) {
final String eventName = attr.getName().toLowerCase(Locale.ROOT);
if (eventName.startsWith("on")) {
createEventHandler(eventName.substring(2), attr.getValue());
}
}
}
use of com.gargoylesoftware.htmlunit.html.DomAttr 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);
}
Aggregations