This error message is generated when JSLint encounters incorrectly nested tags within HTML code. Take the following example:
<html>
<body>
<table>
<td></td>
</table>
</body>
</html>
When run through JSLint, this produces the following error message:
Problem at line 4 character 2: A ‘<td>’ must be within ‘< tr >’.
<td></td>
Stopping, unable to continue. (33% scanned).
Many people overlook the fact that, as well as JavaScript JSLint can work on HTML, CSS and JSON text.
JSLint does not require HTML to follow to XHTML Strict standards, however it does require the following:
- All tag names must be in lower case.
- All tags that can take a close tag (such as </p>) must have a close tag.
- All tags are correctly nested.
- The entity < must be used for literal ’<’.
Looking under the hood of JSLint, we see can see a declaration for an htmltag object:
htmltag = {
a: {},
abbr: {},
....
head: {parent: ' html '},
....
tbody: {parent: ' table '},
td: {parent: ' tr '},
....
}
This drives the rules for HTML parent/child relationships within JSLint. Referring to our example above, we can see that JSLint requires a tr to be an immediate parent of a td.
A Guide To JSLint Messages
This article is one of a series on the error and warning messages produced by JSLint.
[...] {a} ({b}% scanned) ‘{a}’ is better written without quotes. {b} {a} declared in a block. A ‘<{a}>’ must be within ‘<{b}>’. A constructor name should start with an uppercase letter. A dot following a number can be confused [...]