diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java index c2d5580e3..970c23f96 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java @@ -178,20 +178,32 @@ public static Range selectChildEndTag(String childTag, int offset, DOMDocument d if (parent == null || !parent.isElement() || ((DOMElement) parent).getTagName() == null) { return null; } - if (parent != null) { - DOMNode child = findChildNode(childTag, parent.getChildren()); - if (child != null) { + + DOMNode curr = parent; + DOMNode child; + while (curr != null) { + child = findUnclosedChildNode(childTag, curr.getChildren()); + if (child == null) { + curr = findUnclosedChildNode(curr.getChildren()); + } else { return createRange(child.getStart() + 1, child.getStart() + 1 + childTag.length(), document); } - if(parent.isElement()) { - String parentName = ((DOMElement) parent).getTagName(); - return createRange(parent.getStart() + 2, parent.getStart() + 2 + parentName.length(), document); + } + + String parentName = ((DOMElement) parent).getTagName(); + return createRange(parent.getStart() + 2, parent.getStart() + 2 + parentName.length(), document); + } + + public static DOMNode findUnclosedChildNode(List children) { + for (DOMNode child: children) { + if (!child.isClosed()) { + return child; } } return null; } - static DOMNode findChildNode(String childTag, List children) { + static DOMNode findUnclosedChildNode(String childTag, List children) { for (DOMNode child : children) { if (child.isElement() && childTag != null && childTag.equals(((DOMElement) child).getTagName()) && !child.isClosed()) { diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java index 34e857f7c..072b7867c 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java @@ -167,6 +167,16 @@ public void testETagRequired2() throws Exception { testDiagnosticsFor(xml, d(1, 13, 1, 15, XMLSyntaxErrorCode.ETagRequired)); } + @Test + public void testETagRequired3() throws Exception { + String xml = "\r\n" + + " Name\r\n" + + " \r\n" + + " \r\n" + + ""; + testDiagnosticsFor(xml, d(3, 5, 3, 7, XMLSyntaxErrorCode.ETagRequired)); + } + /** * Test ETagUnterminated *