diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index b7ab311cb1..e64354df24 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -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 * apex * [#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 * [#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 diff --git a/pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/LineNumbers.java b/pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/LineNumbers.java index 423b2772fb..5419b30e3a 100644 --- a/pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/LineNumbers.java +++ b/pmd-html/src/main/java/net/sourceforge/pmd/lang/html/ast/LineNumbers.java @@ -50,15 +50,14 @@ class LineNumbers { nextIndex = determineLocation((AbstractHtmlNode) child, nextIndex); } - // autoclosing element, eg - boolean isAutoClose = n.getNumChildren() == 0 - && n instanceof ASTHtmlElement - // nextIndex is up to the closing > at this point - && htmlString.startsWith("/>", nextIndex - 2); + // explicitly closing element, eg. + boolean hasCloseElement = n instanceof ASTHtmlElement + // nextIndex is up to the closing tag at this point + && htmlString.startsWith("", nextIndex); if (n instanceof ASTHtmlDocument) { nextIndex = htmlString.length(); - } else if (n instanceof ASTHtmlElement && !isAutoClose) { + } else if (n instanceof ASTHtmlElement && hasCloseElement) { nextIndex += 2 + n.getXPathNodeName().length() + 1; // } else if (n instanceof ASTHtmlComment) { nextIndex += 4 + 3; // diff --git a/pmd-html/src/test/java/net/sourceforge/pmd/lang/html/cpd/HtmlCpdLexerTest.java b/pmd-html/src/test/java/net/sourceforge/pmd/lang/html/cpd/HtmlCpdLexerTest.java index 04db5c6152..088837d2ce 100644 --- a/pmd-html/src/test/java/net/sourceforge/pmd/lang/html/cpd/HtmlCpdLexerTest.java +++ b/pmd-html/src/test/java/net/sourceforge/pmd/lang/html/cpd/HtmlCpdLexerTest.java @@ -21,4 +21,13 @@ class HtmlCpdLexerTest extends CpdTextComparisonTest { doTest("SimpleHtmlFile"); } + @Test + void invalidHtml() { + doTest("InvalidHtml"); + } + + @Test + void metaTag() { + doTest("MetaTag"); + } } diff --git a/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.html b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.html new file mode 100644 index 0000000000..73acc68e1d --- /dev/null +++ b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.html @@ -0,0 +1,7 @@ + + + + +
+ + diff --git a/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.txt b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.txt new file mode 100644 index 0000000000..2cc097ffd2 --- /dev/null +++ b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/InvalidHtml.txt @@ -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 diff --git a/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.html b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.html new file mode 100644 index 0000000000..d8a96810a8 --- /dev/null +++ b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.html @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.txt b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.txt new file mode 100644 index 0000000000..0547e117b8 --- /dev/null +++ b/pmd-html/src/test/resources/net/sourceforge/pmd/lang/html/cpd/testdata/MetaTag.txt @@ -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