// Try lower-casing the name to avoid HTML name fuzziness.
String lQname = Strings.lower(qname);
if (lQname != qname && (elNs = ns.forElementName(lQname)) != null) {
qname = lQname;
} else {
FilePosition pos = Nodes.getFilePositionFor(el);
ns = elNs = AbstractElementStack.unknownNamespace(
pos, ns, qname, mq);
}
}
Element replacement;
try {
replacement = doc.createElementNS(elNs.uri, qname);
} catch (DOMException e) {
FilePosition pos = Nodes.getFilePositionFor(el);
mq.addMessage(MessageType.INVALID_TAG_NAME, MessageLevel.WARNING,
FilePosition.startOf(pos),
MessagePart.Factory.valueOf(qname));
if (!asXml) {
// Try creating a non namespaced element as most likely so will
// the browser.
// At this point, we can be sure that createElement will not fail
// because Html5ElementStack did not ignore this element.
replacement = doc.createElement(qname);
} else {
throw new ParseException(new Message(
DomParserMessageType.IGNORING_TOKEN, pos,
MessagePart.Factory.valueOf("'" + qname + "'")), e);
}
}
el.getParentNode().replaceChild(replacement, el);
for (Node child; (child = el.getFirstChild()) != null; ) {
replacement.appendChild(child);
}
NamedNodeMap attrs = el.getAttributes();
while (attrs.getLength() != 0) {
Attr a = (Attr) attrs.item(0);
el.removeAttributeNode(a);
replacement.setAttributeNodeNS(a);
}
if (needsDebugData) {
Nodes.setFilePositionFor(replacement, Nodes.getFilePositionFor(el));
}
node = el = replacement; // Return the replacement.
} else {
elNs = ns.forUri(el.getNamespaceURI());
if (elNs == null) {
FilePosition pos = Nodes.getFilePositionFor(el);
ns = elNs = AbstractElementStack.unknownNamespace(
pos, ns, el.getTagName(), mq);
}
}
// And finally, namespace all the attributes.