[html] Test for a closing tag when determining node positions (#5328)

This commit is contained in:
Juan Martín Sotuyo Dodero 2024-11-15 08:50:53 -06:00 committed by GitHub
commit c73e5cb8b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 81 additions and 6 deletions

View File

@ -19,6 +19,8 @@ This is a {{ site.pmd.release_type }} release.
* [#1860](https://github.com/pmd/pmd/issues/1860): \[ant] Reflective access warnings on java > 9 and java < 17 * [#1860](https://github.com/pmd/pmd/issues/1860): \[ant] Reflective access warnings on java > 9 and java < 17
* apex * apex
* [#5333](https://github.com/pmd/pmd/issues/5333): \[apex] Token recognition errors for string containing unicode escape sequence * [#5333](https://github.com/pmd/pmd/issues/5333): \[apex] Token recognition errors for string containing unicode escape sequence
* html
* [5322](https://github.com/pmd/pmd/issues/5322): \[html] CPD throws exception on when HTML file is missing closing tag
* java * java
* [#5293](https://github.com/pmd/pmd/issues/5293): \[java] Deadlock when executing PMD in multiple threads * [#5293](https://github.com/pmd/pmd/issues/5293): \[java] Deadlock when executing PMD in multiple threads
* [#5324](https://github.com/pmd/pmd/issues/5324): \[java] Issue with type inference of nested lambdas * [#5324](https://github.com/pmd/pmd/issues/5324): \[java] Issue with type inference of nested lambdas

View File

@ -50,15 +50,14 @@ class LineNumbers {
nextIndex = determineLocation((AbstractHtmlNode<?>) child, nextIndex); nextIndex = determineLocation((AbstractHtmlNode<?>) child, nextIndex);
} }
// autoclosing element, eg <a /> // explicitly closing element, eg. </a>
boolean isAutoClose = n.getNumChildren() == 0 boolean hasCloseElement = n instanceof ASTHtmlElement
&& n instanceof ASTHtmlElement // nextIndex is up to the closing tag at this point
// nextIndex is up to the closing > at this point && htmlString.startsWith("</" + n.getXPathNodeName() + ">", nextIndex);
&& htmlString.startsWith("/>", nextIndex - 2);
if (n instanceof ASTHtmlDocument) { if (n instanceof ASTHtmlDocument) {
nextIndex = htmlString.length(); nextIndex = htmlString.length();
} else if (n instanceof ASTHtmlElement && !isAutoClose) { } else if (n instanceof ASTHtmlElement && hasCloseElement) {
nextIndex += 2 + n.getXPathNodeName().length() + 1; // </nodename> nextIndex += 2 + n.getXPathNodeName().length() + 1; // </nodename>
} else if (n instanceof ASTHtmlComment) { } else if (n instanceof ASTHtmlComment) {
nextIndex += 4 + 3; // <!-- and --> nextIndex += 4 + 3; // <!-- and -->

View File

@ -21,4 +21,13 @@ class HtmlCpdLexerTest extends CpdTextComparisonTest {
doTest("SimpleHtmlFile"); doTest("SimpleHtmlFile");
} }
@Test
void invalidHtml() {
doTest("InvalidHtml");
}
@Test
void metaTag() {
doTest("MetaTag");
}
} }

View File

@ -0,0 +1,7 @@
<!doctype html>
<html lang="en">
<body>
<!-- missing closing tag for div -->
<div class='wrapper'>
</body>
</html>

View File

@ -0,0 +1,22 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[#document] 1 8
[#doctype] 1 15
[\n] 16 16
L2
[html] 1 7
[\n] 17 17
L3
[body] 1 7
[\n] 7 7
L4
[#comment] 1 36
[\n] 37 37
L5
[div] 1 22
[\n] 22 22
L6
[\n] 8 8
L7
[\n] 8 8
EOF

View File

@ -0,0 +1,9 @@
<!doctype html>
<html lang="en">
<head>
<!-- meta tag doesn't have a closing tag and this is valid -->
<meta charset="UTF-8">
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,27 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[#document] 1 8
[#doctype] 1 15
[\n] 16 16
L2
[html] 1 7
[\n] 17 17
L3
[head] 1 7
[\n ] 7 4
L4
[#comment] 5 66
[\n ] 67 4
L5
[meta] 5 27
[\n] 27 27
L6
[\n] 8 8
L7
[body] 1 7
[\n] 7 7
L8
[\n] 8 8
L9
[\n] 8 8
EOF