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
80 changes: 80 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,84 @@ 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#concept-select-option-selectedness
// 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;
}

// 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) {
for (JavaScriptObject child = getFirstChild(root);
child != null; child = getNextSibling(child)) {
if (getNodeType(child) == 1) {
if (localName.equals(getLocalName(child))) {
return child;
}
JavaScriptObject found = findDescendantByLocalName(
child, localName);
if (found != null) {
return found;
}
}
}
return null;
}
}
69 changes: 69 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,73 @@ protected Element createAndInsertFosterParentedElement(String ns, String name,
fatal(e);
}
}

@Override
// https://html.spec.whatwg.org/multipage/form-elements.html#concept-select-option-selectedness
// 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;
}

// 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) {
for (Node child = root.getFirstChild(); child != null;
child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE) {
Element elt = (Element) child;
if ("selectedcontent".equals(elt.getLocalName())
&& "http://www.w3.org/1999/xhtml".equals(
elt.getNamespaceURI())) {
return elt;
}
Element found = findSelectedContent(elt);
if (found != null) {
return found;
}
}
}
return null;
}
}
140 changes: 73 additions & 67 deletions src/nu/validator/htmlparser/impl/ElementName.java
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,11 @@ public void destructor() {
public static final ElementName SELECT = new ElementName("select", "select",
// CPPONLY: NS_NewHTMLSelectElement,
// CPPONLY: NS_NewSVGUnknownElement,
TreeBuilder.SELECT | SPECIAL);
TreeBuilder.SELECT | SPECIAL | SCOPING);
public static final ElementName SELECTEDCONTENT = new ElementName("selectedcontent", "selectedcontent",
// CPPONLY: NS_NewHTMLElement,
// CPPONLY: NS_NewSVGUnknownElement,
TreeBuilder.OTHER);
public static final ElementName SLOT = new ElementName("slot", "slot",
// CPPONLY: NS_NewHTMLSlotElement,
// CPPONLY: NS_NewSVGUnknownElement,
Expand Down Expand Up @@ -1484,18 +1488,18 @@ public void destructor() {
private final static @NoLength ElementName[] ELEMENT_NAMES = {
FIGCAPTION,
CITE,
FRAMESET,
FEOFFSET,
H1,
CLIPPATH,
METER,
RADIALGRADIENT,
SELECT,
B,
BGSOUND,
SOURCE,
DL,
RP,
NOFRAMES,
MTEXT,
PROGRESS,
NOSCRIPT,
VIEW,
DIV,
G,
Expand All @@ -1507,10 +1511,10 @@ public void destructor() {
ANIMATETRANSFORM,
SECTION,
HR,
CANVAS,
BASEFONT,
FEDISTANTLIGHT,
OUTPUT,
DEFS,
DATALIST,
FONT,
PLAINTEXT,
TFOOT,
FEMORPHOLOGY,
COL,
Expand All @@ -1533,14 +1537,14 @@ public void destructor() {
VIDEO,
BR,
FOOTER,
TR,
DETAILS,
DT,
FOREIGNOBJECT,
FESPOTLIGHT,
INPUT,
RT,
TT,
ADDRESS,
MS,
APPLET,
FIELDSET,
FEPOINTLIGHT,
LINEARGRADIENT,
OBJECT,
RECT,
SLOT,
MENU,
FECONVOLVEMATRIX,
Expand Down Expand Up @@ -1585,23 +1589,23 @@ public void destructor() {
ANIMATECOLOR,
FECOMPONENTTRANSFER,
HEADER,
NOBR,
ADDRESS,
DEFS,
MS,
PROGRESS,
APPLET,
DATALIST,
FIELDSET,
FEOFFSET,
FEPOINTLIGHT,
FONT,
LINEARGRADIENT,
NOSCRIPT,
OBJECT,
PLAINTEXT,
RECT,
SELECT,
TR,
CANVAS,
DETAILS,
NOFRAMES,
DT,
BASEFONT,
FOREIGNOBJECT,
FRAMESET,
FESPOTLIGHT,
FEDISTANTLIGHT,
INPUT,
MTEXT,
RT,
OUTPUT,
TT,
RADIALGRADIENT,
SELECTEDCONTENT,
SCRIPT,
TEXT,
FEDROPSHADOW,
Expand Down Expand Up @@ -1689,22 +1693,23 @@ public void destructor() {
FILTER,
FEGAUSSIANBLUR,
MARKER,
NOBR,
};
private final static int[] ELEMENT_HASHES = {
1900845386,
1748359220,
2001349720,
2001349736,
876609538,
1798686984,
1971465813,
2007781534,
2008125638,
59768833,
1730965751,
1756474198,
1864368130,
1938817026,
1988763672,
2005324101,
1990037800,
2005719336,
2060065124,
52490899,
62390273,
Expand All @@ -1716,10 +1721,10 @@ public void destructor() {
1881498736,
1907661127,
1967128578,
1982935782,
1999397992,
2001392798,
2006329158,
1983533124,
2000525512,
2001495140,
2006896969,
2008851557,
2085266636,
51961587,
Expand All @@ -1742,14 +1747,14 @@ public void destructor() {
1925844629,
1963982850,
1967795958,
1973420034,
1983633431,
1998585858,
2001309869,
2001392795,
2003183333,
2005925890,
2006974466,
1982173479,
1986527234,
1998724870,
2001349704,
2001392796,
2004635806,
2006028454,
2007601444,
2008325940,
2021937364,
2068523856,
Expand Down Expand Up @@ -1794,23 +1799,23 @@ public void destructor() {
1965334268,
1967788867,
1968836118,
1971938532,
1982173479,
1983533124,
1986527234,
1990037800,
1998724870,
2000525512,
2001349704,
2001349736,
2001392796,
2001495140,
2004635806,
2005719336,
2006028454,
2006896969,
2007601444,
2008125638,
1973420034,
1982935782,
1983633431,
1988763672,
1998585858,
1999397992,
2001309869,
2001349720,
2001392795,
2001392798,
2003183333,
2005324101,
2005925890,
2006329158,
2006974466,
2007781534,
2008305999,
2008340774,
2008994116,
2051837468,
Expand Down Expand Up @@ -1898,5 +1903,6 @@ public void destructor() {
1967795910,
1968053806,
1971461414,
1971938532,
};
}
Loading