Skip to content

Commit

Permalink
Treat custom elements as labelable elements
Browse files Browse the repository at this point in the history
This change causes the checker to consider all custom elements to be
“labelable elements” per the definition of that term in the spec at
https://html.spec.whatwg.org/multipage/forms.html#category-label.

Without this change, the checker fails to recognize “form-associated
custom elements” with valid id attributes as labelable elements, and so
ends up reporting false positives for any “label” elements in a document
that reference the id attribute of a custom element.

This change isn’t completely conforming to the spec because it
effectively doesn’t distinguish between custom elements that aren’t
“form-associated custom elements” defined in the spec at
https://html.spec.whatwg.org/custom-elements.html#form-associated-custom-element
and custom elements that are true “form-associated custom elements”.

The problem with trying to conform to that spec requirement to only
treat form-associated custom elements as labelable — but not other
custom elements — is that it necessitates executing the document’s
JavaScript and using DOM APIs to examine the document. But in the
checker architecture, that’s not possible — the checker doesn’t
execute JavaScript and can’t use DOM APIs to inspect documents.

Fixes #963
  • Loading branch information
sideshowbarker committed May 21, 2020
1 parent ab3fcba commit 325ab52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ NN XXX NNNN
- Allow `height` and `width` attributes for SVG `symbol` element
- Allow `capture` attribute for the `input[type=file]` element
- Allow `disabled` attribute for the `link[rel=stylesheet]` element
- Treat custom elements as labelable elements for `label[for]` handling
- ARIA: Fix bug that disallowed implicit combobox/listbox for `select`
- ARIA: Improve error message for `alt`-less `img` with ARIA attributes

Expand Down
51 changes: 27 additions & 24 deletions src/nu/validator/checker/schematronequiv/Assertions.java
Expand Up @@ -1711,6 +1711,7 @@ public void startElement(String uri, String localName, String name,
boolean languageJavaScript = false;
boolean typeNotTextJavaScript = false;
boolean hasAriaAttributesOtherThanAriaHidden = false;
boolean isCustomElement = false;
String xmlLang = null;
String lang = null;
String id = null;
Expand Down Expand Up @@ -1930,6 +1931,30 @@ public void startElement(String uri, String localName, String name,
}
}

if (localName.contains("-")) {
isCustomElement = true;
if (atts.getIndex("", "is") > -1) {
err("Autonomous custom elements must not specify the"
+ " \u201cis\u201d attribute.");
}
try {
CustomElementName.THE_INSTANCE.checkValid(localName);
} catch (DatatypeException e) {
try {
if (getErrorHandler() != null) {
String msg = e.getMessage();
if (e instanceof Html5DatatypeException) {
msg = msg.substring(msg.indexOf(": ") + 2);
}
VnuBadElementNameException ex = new VnuBadElementNameException(
localName, uri, msg, getDocumentLocator(),
CustomElementName.class, false);
getErrorHandler().error(ex);
}
} catch (ClassNotFoundException ce) {
}
}
}
if ("input".equals(localName)) {
if (atts.getIndex("", "name") > -1
&& "isindex".equals(atts.getValue("", "name"))) {
Expand Down Expand Up @@ -2661,7 +2686,8 @@ else if ("bdo" == localName && atts.getIndex("", "dir") < 0) {
|| "output" == localName //
|| "progress" == localName //
|| "select" == localName //
|| "textarea" == localName) {
|| "textarea" == localName //
|| isCustomElement) {
formControlIds.addAll(ids);
}

Expand Down Expand Up @@ -3249,29 +3275,6 @@ else if ("bdo" == localName && atts.getIndex("", "dir") < 0) {
// ARIA
}
}
if (localName.contains("-")) {
if (atts.getIndex("", "is") > -1) {
err("Autonomous custom elements must not specify the"
+ " \u201cis\u201d attribute.");
}
try {
CustomElementName.THE_INSTANCE.checkValid(localName);
} catch (DatatypeException e) {
try {
if (getErrorHandler() != null) {
String msg = e.getMessage();
if (e instanceof Html5DatatypeException) {
msg = msg.substring(msg.indexOf(": ") + 2);
}
VnuBadElementNameException ex = new VnuBadElementNameException(
localName, uri, msg, getDocumentLocator(),
CustomElementName.class, false);
getErrorHandler().error(ex);
}
} catch (ClassNotFoundException ce) {
}
}
}
} else if ("http://n.validator.nu/custom-elements/" == uri) {
/*
* For elements with names containing "-" (custom elements), the
Expand Down

0 comments on commit 325ab52

Please sign in to comment.