Skip to content

Commit

Permalink
Fix error range for cvc-123 on empty content
Browse files Browse the repository at this point in the history
Closes eclipse-lemminx#871

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Sep 10, 2020
1 parent 8f19b8e commit eb22a4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

/**
* XML Schema error code.
*
*
* @see https://wiki.xmldation.com/Support/Validator
*
*/
Expand Down Expand Up @@ -126,7 +126,7 @@ public static XMLSchemaErrorCode get(String name) {

/**
* Create the LSP range from the SAX error.
*
*
* @param location
* @param key
* @param arguments
Expand Down Expand Up @@ -242,7 +242,9 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
} else {
// Try with text
DOMElement element = (DOMElement) document.findNodeAt(offset);
if (DOMUtils.containsTextOnly(element)) {
if (element != null && element.isEmpty()) {
return XMLPositionUtility.createRange(element);
} else if (DOMUtils.containsTextOnly(element)) {
return XMLPositionUtility.selectTrimmedText(offset, document);
} else {
return XMLPositionUtility.selectFirstChild(offset, document);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void cvc_type_4_Multiple_attributes() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<invoice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:noNamespaceSchemaLocation=\"src/test/resources/xsd/invoice.xsd\">\r\n" + //
" <date>2017-11-30</date>\r\n" + //
" <date>2017-11-30</date>\r\n" + //
" <number>2</number>\r\n" + //
" <products>\r\n" + //
" <product />\r\n" + // <- error
Expand All @@ -103,7 +103,7 @@ public void cvc_type_4_Multiple_attributes() throws Exception {

@Test
public void cvc_complex_type_2_4_a() throws Exception {
String xml =
String xml =
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\r\n"+ //
Expand Down Expand Up @@ -142,7 +142,7 @@ public void cvc_complex_type_2_4_d() throws Exception {

@Test
public void cvc_complex_type_2_4_f() throws Exception {
String xml =
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<root\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
Expand Down Expand Up @@ -343,6 +343,15 @@ public void cvc_datatype_valid_1_2_3OnText() throws Exception {
d(3, 15, 3, 18, XMLSchemaErrorCode.cvc_type_3_1_3));
}

@Test
public void cvc_datatype_valid_1_2_3Empty() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<dresssize xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:noNamespaceSchemaLocation=\"src/test/resources/xsd/dressSize.xsd\"></dresssize>";
testDiagnosticsFor(xml, d(1, 0, 2, 82, XMLSchemaErrorCode.cvc_datatype_valid_1_2_3),
d(1, 0, 2, 82, XMLSchemaErrorCode.cvc_type_3_1_3));
}

@Test
public void cvc_maxLength_validOnAttribute() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down Expand Up @@ -386,12 +395,12 @@ public void cvc_complex_type_2_1() throws Exception {
@Test
public void cvc_complex_type_2_1_SelfClosing() throws Exception {
String xml = "<money xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"src/test/resources/xsd/money.xsd\" currency=\"euros\"> </money>";

Diagnostic d = d(0, 143, 0, 144, XMLSchemaErrorCode.cvc_complex_type_2_1);
testDiagnosticsFor(xml, d);
testCodeActionsFor(xml, d, ca(d, te(0, 142, 0, 152, "/>")));
}

@Test
public void cvc_complex_type_2_1WithLinefeed() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down Expand Up @@ -472,7 +481,7 @@ public void cvc_type_3_1_2() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<invoice xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:noNamespaceSchemaLocation=\"src/test/resources/xsd/invoice.xsd\">\r\n" + //
" <date>2017-11-30</date>\r\n" + //
" <date>2017-11-30</date>\r\n" + //
" <number><a></a></number>\r\n" + //<- error
" <products>\r\n" + //
" <product price=\"1\" description=\"\"/>\r\n" + //
Expand All @@ -481,7 +490,7 @@ public void cvc_type_3_1_2() throws Exception {
" <payment amount=\"1\" method=\"credit\"/>\r\n" + //
" </payments>\r\n" + //
"</invoice>";
testDiagnosticsFor(xml,
testDiagnosticsFor(xml,
d(4, 3, 4, 9, XMLSchemaErrorCode.cvc_type_3_1_2),
d(4,10,4,17, XMLSchemaErrorCode.cvc_datatype_valid_1_2_1),
d(4,10,4,17, XMLSchemaErrorCode.cvc_type_3_1_3));
Expand Down Expand Up @@ -535,7 +544,7 @@ public void schema_reference_4_schemaLocationMultipleBothWrong() {

@Test
public void fuzzyElementNameCodeActionTest() throws Exception {
String xml =
String xml =
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" +
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\r\n" +
Expand All @@ -552,7 +561,7 @@ public void fuzzyElementNameCodeActionTest() throws Exception {

@Test
public void fuzzyElementNamesWithOtherOptionsCodeActionTest() throws Exception {
String xml =
String xml =
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" \r\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" +
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\r\n" +
Expand All @@ -569,7 +578,7 @@ public void fuzzyElementNamesWithOtherOptionsCodeActionTest() throws Exception {

@Test
public void fuzzyElementNamesWithPrefix() throws Exception {
String xml =
String xml =
"<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns:camel=\"http://camel.apache.org/schema/spring\"\n" +
Expand All @@ -588,7 +597,7 @@ public void fuzzyElementNamesWithPrefix() throws Exception {

@Test
public void fuzzyElementNamesWithPrefixAndNoMatch() throws Exception {
String xml =
String xml =
"<schemaB:BRootElement \n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" +
" xsi:schemaLocation=\"http://schemaA src/test/resources/xsd/fuzzyCodeAction/FuzzySchemaA.xsd http://schemaB src/test/resources/xsd/fuzzyCodeAction/FuzzySchemaB.xsd\" \n" +
Expand All @@ -598,7 +607,7 @@ public void fuzzyElementNamesWithPrefixAndNoMatch() throws Exception {
"</schemaB:BRootElement>";
Diagnostic diagnostic = d(5, 4, 5, 17, XMLSchemaErrorCode.cvc_complex_type_2_4_c, "cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'schemaA:XXXXX'.");
testDiagnosticsFor(xml, diagnostic);
testCodeActionsFor(xml, diagnostic, ca(diagnostic, te(5, 12, 5, 17, "AElement1"), te(5, 28, 5, 33, "AElement1")),
testCodeActionsFor(xml, diagnostic, ca(diagnostic, te(5, 12, 5, 17, "AElement1"), te(5, 28, 5, 33, "AElement1")),
ca(diagnostic, te(5, 12, 5, 17, "AElement2"), te(5, 28, 5, 33, "AElement2")));
}

Expand Down Expand Up @@ -652,7 +661,7 @@ public void testTargetNamespace_1ShortNS() throws Exception {
);
testCodeActionsFor(xml, targetNamespace, ca(targetNamespace, te(2, 23, 2, 26, "\"http://two-letter-name\"")));
}

@Test
public void testTargetNamespace_1SingleQuotes() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + //
Expand Down

0 comments on commit eb22a4a

Please sign in to comment.