Skip to content

Commit

Permalink
Fix range with ContentIllegalInProlog after.
Browse files Browse the repository at this point in the history
See eclipse-lemminx#611

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Mar 9, 2020
1 parent d096dfe commit 49b80b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,23 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
return XMLPositionUtility.selectChildEndTag(tag, offset, document);
}
case ContentIllegalInProlog: {
int endOffset = document.getText().indexOf("<");
int startOffset = offset + 1;
int endOffset = 0;
int errorOffset = offset + 1;
String text = document.getText();
int startPrologOffset = text.indexOf("<");
if (errorOffset < startPrologOffset) {
// Invalid content given before prolog. Prolog should be the first thing in the
// file if given.
startOffset = errorOffset;
endOffset = startPrologOffset;
} else {
// Invalid content given after prolog. Either root tag or comment should be
// present
int firstStartTagOffset = text.indexOf("<", errorOffset);
startOffset = errorOffset;
endOffset = firstStartTagOffset != -1 ? firstStartTagOffset : text.length();
}
return XMLPositionUtility.createRange(startOffset, endOffset, document);
}
case DashDashInComment: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,35 @@ public void testAttributePrefixUnbound() throws Exception {
* @throws Exception
*/
@Test
public void testContentIllegalInProlog() throws Exception {
public void testBeforeContentIllegalInProlog() throws Exception {
String xml = " ab?<xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
testDiagnosticsFor(xml, d(0, 1, 0, 4, XMLSyntaxErrorCode.ContentIllegalInProlog));
}

/**
* ContentIllegalInProlog tests
*
* @see https://wiki.xmldation.com/Support/Validator/ContentIllegalInProlog
* @throws Exception
*/
@Test
public void testAfterContentIllegalInProlog() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>ab\ncd";
testDiagnosticsFor(xml, d(0, 54, 1, 2, XMLSyntaxErrorCode.ContentIllegalInProlog));
}

/**
* ContentIllegalInProlog tests
*
* @see https://wiki.xmldation.com/Support/Validator/ContentIllegalInProlog
* @throws Exception
*/
@Test
public void testAfterContentIllegalInProlog2() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>ab\ncd<root>";
testDiagnosticsFor(xml, d(0, 54, 1, 2, XMLSyntaxErrorCode.ContentIllegalInProlog));
}

@Test
public void testEncodingUTF_16() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\"?><root />";
Expand Down

0 comments on commit 49b80b3

Please # to comment.