Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions gwt-src/nu/validator/htmlparser/gwt/BrowserTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,98 @@ private static native void removeChild(JavaScriptObject parent,
fatal(e);
}
}

private static native JavaScriptObject getNextSibling(
JavaScriptObject node) /*-{
return node.nextSibling;
}-*/;

private static native String getLocalName(
JavaScriptObject node) /*-{
return node.localName;
}-*/;

private static native boolean hasAttribute(
JavaScriptObject node, String name) /*-{
return node.hasAttribute(name);
}-*/;

@Override
// https://html.spec.whatwg.org/multipage/form-elements.html#maybe-clone-an-option-into-selectedcontent
// Implements "maybe clone an option into selectedcontent"
protected void optionElementPopped(JavaScriptObject option)
throws SAXException {
try {
// Find the nearest ancestor <select> element
JavaScriptObject ancestor = getParentNode(option);
JavaScriptObject select = null;
while (ancestor != null && getNodeType(ancestor) == 1) {
if ("select".equals(getLocalName(ancestor))) {
select = ancestor;
break;
}
ancestor = getParentNode(ancestor);
}
if (select == null) {
return;
}
if (hasAttribute(select, "multiple")) {
return;
}

// Find the first <selectedcontent> descendant of <select>
JavaScriptObject selectedContent = findDescendantByLocalName(
select, "selectedcontent");
if (selectedContent == null) {
return;
}

// Check option selectedness
boolean hasSelectedAttr = hasAttribute(option, "selected");
if (!hasSelectedAttr && hasChildNodes(selectedContent)) {
// Not the first option and no explicit selected attr
return;
}

// Clear selectedcontent children and deep-clone option children
while (hasChildNodes(selectedContent)) {
removeChild(selectedContent, getFirstChild(selectedContent));
}
for (JavaScriptObject child = getFirstChild(option);
child != null; child = getNextSibling(child)) {
appendChild(selectedContent, cloneNodeDeep(child));
}
} catch (JavaScriptException e) {
fatal(e);
}
}

private JavaScriptObject findDescendantByLocalName(
JavaScriptObject root, String localName) {
JavaScriptObject current = getFirstChild(root);
if (current == null) {
return null;
}
JavaScriptObject next;
for (;;) {
if (getNodeType(current) == 1
&& localName.equals(getLocalName(current))) {
return current;
}
if ((next = getFirstChild(current)) != null) {
current = next;
continue;
}
for (;;) {
if (current == root) {
return null;
}
if ((next = getNextSibling(current)) != null) {
current = next;
break;
}
current = getParentNode(current);
}
}
}
}
85 changes: 85 additions & 0 deletions src/nu/validator/htmlparser/dom/DOMTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,89 @@ protected Element createAndInsertFosterParentedElement(String ns, String name,
fatal(e);
}
}

@Override
// https://html.spec.whatwg.org/multipage/form-elements.html#maybe-clone-an-option-into-selectedcontent
// Implements "maybe clone an option into selectedcontent"
protected void optionElementPopped(Element option) throws SAXException {
try {
// Find the nearest ancestor <select> element
Node ancestor = option.getParentNode();
Element select = null;
while (ancestor != null) {
if (ancestor.getNodeType() == Node.ELEMENT_NODE) {
Element elt = (Element) ancestor;
if ("select".equals(elt.getLocalName())
&& "http://www.w3.org/1999/xhtml".equals(
elt.getNamespaceURI())) {
select = elt;
break;
}
}
ancestor = ancestor.getParentNode();
}
if (select == null) {
return;
}
if (select.hasAttribute("multiple")) {
return;
}

// Find the first <selectedcontent> descendant of <select>
Element selectedContent = findSelectedContent(select);
if (selectedContent == null) {
return;
}

// Check option selectedness
boolean hasSelected = option.hasAttribute("selected");
if (!hasSelected && selectedContent.hasChildNodes()) {
// Not the first option and no explicit selected attr
return;
}

// Clear selectedcontent children and deep-clone option children
while (selectedContent.hasChildNodes()) {
selectedContent.removeChild(selectedContent.getFirstChild());
}
for (Node child = option.getFirstChild(); child != null;
child = child.getNextSibling()) {
selectedContent.appendChild(child.cloneNode(true));
}
} catch (DOMException e) {
fatal(e);
}
}

private Element findSelectedContent(Element root) {
Node current = root.getFirstChild();
if (current == null) {
return null;
}
Node next;
for (;;) {
if (current.getNodeType() == Node.ELEMENT_NODE) {
Element elt = (Element) current;
if ("selectedcontent".equals(elt.getLocalName())
&& "http://www.w3.org/1999/xhtml".equals(
elt.getNamespaceURI())) {
return elt;
}
}
if ((next = current.getFirstChild()) != null) {
current = next;
continue;
}
for (;;) {
if (current == root) {
return null;
}
if ((next = current.getNextSibling()) != null) {
current = next;
break;
}
current = current.getParentNode();
}
}
}
}
Loading
Loading