Skip to content

Commit 664592e

Browse files
Move the self-closing-tag warning to the TreeBuilder code (#74)
1 parent b2805f5 commit 664592e

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

src/nu/validator/htmlparser/impl/ErrorReportingTokenizer.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ public void setErrorProfile(HashMap<String, String> errorProfileMap) {
134134
this.errorProfileMap = errorProfileMap;
135135
}
136136

137+
/**
138+
* Gets the errorProfile.
139+
*
140+
* @param errorProfile
141+
*/
142+
@Override public HashMap getErrorProfile() {
143+
return errorProfileMap;
144+
}
145+
137146
/**
138147
* Reports on an event based on profile selected.
139148
*
@@ -710,18 +719,6 @@ private boolean isAstralPrivateUse(int c) {
710719
note("xhtml1", "Unquoted attribute value.");
711720
}
712721

713-
@Override
714-
protected void noteSelfClosingTag() throws SAXException {
715-
note("html-strict",
716-
"Self-closing tag syntax in text/html documents is widely"
717-
+ " discouraged; it’s unnecessary and interacts badly"
718-
+ " with other HTML features (e.g., unquoted attribute"
719-
+ " values). If you’re using a tool that injects"
720-
+ " self-closing tag syntax into all void elements,"
721-
+ " without any option to prevent it from doing so,"
722-
+ " then consider switching to a different tool.");
723-
}
724-
725722
/**
726723
* Sets the transitionHandler.
727724
*

src/nu/validator/htmlparser/impl/Tokenizer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
package nu.validator.htmlparser.impl;
3737

38+
import java.util.HashMap;
39+
3840
import org.xml.sax.ErrorHandler;
3941
import org.xml.sax.Locator;
4042
import org.xml.sax.ext.Locator2;
@@ -686,6 +688,15 @@ public ErrorHandler getErrorHandler() {
686688
return this.errorHandler;
687689
}
688690

691+
/**
692+
* Gets the errorProfile.
693+
*
694+
* @param errorProfile
695+
*/
696+
public HashMap getErrorProfile() {
697+
return null;
698+
}
699+
689700
/**
690701
* Sets the commentPolicy.
691702
*
@@ -2292,7 +2303,6 @@ private void ensureBufferSpace(int inputLength) throws SAXException {
22922303
* flag of the current tag token. Emit the current
22932304
* tag token.
22942305
*/
2295-
noteSelfClosingTag();
22962306
state = transition(state, emitCurrentTagToken(true, pos), reconsume, pos);
22972307
if (shouldSuspend) {
22982308
break stateloop;
@@ -7588,9 +7598,6 @@ protected void noteAttributeWithoutValue() throws SAXException {
75887598
protected void noteUnquotedAttributeValue() throws SAXException {
75897599
}
75907600

7591-
protected void noteSelfClosingTag() throws SAXException {
7592-
}
7593-
75947601
/**
75957602
* Sets the encodingDeclarationHandler.
75967603
*

src/nu/validator/htmlparser/impl/TreeBuilder.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,8 @@ public final void startTag(ElementName elementName,
14581458
flushCharacters();
14591459

14601460
// [NOCPP[
1461+
boolean wasSelfClosing = selfClosing;
1462+
boolean voidElement = false;
14611463
if (errorHandler != null) {
14621464
// ID uniqueness
14631465
@IdType String id = attributes.getId();
@@ -1580,6 +1582,9 @@ public final void startTag(ElementName elementName,
15801582
elementName,
15811583
attributes);
15821584
selfClosing = false;
1585+
// [NOCPP[
1586+
voidElement = true;
1587+
// ]NOCPP]
15831588
attributes = null; // CPP
15841589
break starttagloop;
15851590
case TITLE:
@@ -1592,6 +1597,9 @@ public final void startTag(ElementName elementName,
15921597
elementName,
15931598
attributes);
15941599
selfClosing = false;
1600+
// [NOCPP[
1601+
voidElement = true;
1602+
// ]NOCPP]
15951603
attributes = null; // CPP
15961604
break starttagloop;
15971605
case SCRIPT:
@@ -1778,6 +1786,9 @@ public final void startTag(ElementName elementName,
17781786
attributes,
17791787
formPointer);
17801788
selfClosing = false;
1789+
// [NOCPP[
1790+
voidElement = true;
1791+
// ]NOCPP]
17811792
attributes = null; // CPP
17821793
break starttagloop;
17831794
case FORM:
@@ -2124,6 +2135,9 @@ public final void startTag(ElementName elementName,
21242135
elementName,
21252136
attributes);
21262137
selfClosing = false;
2138+
// [NOCPP[
2139+
voidElement = true;
2140+
// ]NOCPP]
21272141
attributes = null; // CPP
21282142
break starttagloop;
21292143
case HR:
@@ -2132,6 +2146,9 @@ public final void startTag(ElementName elementName,
21322146
elementName,
21332147
attributes);
21342148
selfClosing = false;
2149+
// [NOCPP[
2150+
voidElement = true;
2151+
// ]NOCPP]
21352152
attributes = null; // CPP
21362153
break starttagloop;
21372154
case IMAGE:
@@ -2145,6 +2162,9 @@ public final void startTag(ElementName elementName,
21452162
elementName, attributes,
21462163
formPointer);
21472164
selfClosing = false;
2165+
// [NOCPP[
2166+
voidElement = true;
2167+
// ]NOCPP]
21482168
attributes = null; // CPP
21492169
break starttagloop;
21502170
case TEXTAREA:
@@ -2324,6 +2344,9 @@ public final void startTag(ElementName elementName,
23242344
elementName,
23252345
attributes);
23262346
selfClosing = false;
2347+
// [NOCPP[
2348+
voidElement = true;
2349+
// ]NOCPP]
23272350
attributes = null; // CPP
23282351
break starttagloop;
23292352
case META:
@@ -2391,6 +2414,9 @@ public final void startTag(ElementName elementName,
23912414
elementName,
23922415
attributes);
23932416
selfClosing = false;
2417+
// [NOCPP[
2418+
voidElement = true;
2419+
// ]NOCPP]
23942420
attributes = null; // CPP
23952421
break starttagloop;
23962422
case META:
@@ -2399,6 +2425,9 @@ public final void startTag(ElementName elementName,
23992425
elementName,
24002426
attributes);
24012427
selfClosing = false;
2428+
// [NOCPP[
2429+
voidElement = true;
2430+
// ]NOCPP]
24022431
attributes = null; // CPP
24032432
break starttagloop;
24042433
case STYLE:
@@ -2438,6 +2467,9 @@ public final void startTag(ElementName elementName,
24382467
elementName,
24392468
attributes);
24402469
selfClosing = false;
2470+
// [NOCPP[
2471+
voidElement = true;
2472+
// ]NOCPP]
24412473
attributes = null; // CPP
24422474
break starttagloop;
24432475
case TEMPLATE:
@@ -2572,6 +2604,9 @@ public final void startTag(ElementName elementName,
25722604
elementName,
25732605
attributes);
25742606
selfClosing = false;
2607+
// [NOCPP[
2608+
voidElement = true;
2609+
// ]NOCPP]
25752610
attributes = null; // CPP
25762611
break starttagloop;
25772612
default:
@@ -2745,6 +2780,9 @@ public final void startTag(ElementName elementName,
27452780
elementName,
27462781
attributes);
27472782
selfClosing = false;
2783+
// [NOCPP[
2784+
voidElement = true;
2785+
// ]NOCPP]
27482786
pop(); // head
27492787
attributes = null; // CPP
27502788
break starttagloop;
@@ -2756,6 +2794,9 @@ public final void startTag(ElementName elementName,
27562794
elementName,
27572795
attributes);
27582796
selfClosing = false;
2797+
// [NOCPP[
2798+
voidElement = true;
2799+
// ]NOCPP]
27592800
pop(); // head
27602801
attributes = null; // CPP
27612802
break starttagloop;
@@ -2844,6 +2885,18 @@ public final void startTag(ElementName elementName,
28442885
}
28452886
if (selfClosing) {
28462887
errSelfClosing();
2888+
// [NOCPP[
2889+
} else if (wasSelfClosing && voidElement
2890+
&& tokenizer.getErrorProfile() != null
2891+
&& tokenizer.getErrorProfile().get("html-strict") != null) {
2892+
warn("Self-closing tag syntax in text/html documents is widely"
2893+
+ " discouraged; it’s unnecessary and interacts badly"
2894+
+ " with other HTML features (e.g., unquoted attribute"
2895+
+ " values). If you’re using a tool that injects"
2896+
+ " self-closing tag syntax into all void elements,"
2897+
+ " without any option to prevent it from doing so,"
2898+
+ " then consider switching to a different tool.");
2899+
// ]NOCPP]
28472900
}
28482901
// CPPONLY: if (mBuilder == null && attributes != HtmlAttributes.EMPTY_ATTRIBUTES) {
28492902
// CPPONLY: Portability.delete(attributes);

0 commit comments

Comments
 (0)