Skip to content

Commit

Permalink
Fix error range for cvc-datatype-valid.1.2.3
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#864

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 2, 2020
1 parent cb4eea0 commit b63cc03
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public enum XMLSchemaErrorCode implements IXMLErrorCode {
cvc_complex_type_3_2_2("cvc-complex-type.3.2.2"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-3-2-2
cvc_complex_type_4("cvc-complex-type.4"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-4
cvc_datatype_valid_1_2_1("cvc-datatype-valid.1.2.1"), // https://wiki.xmldation.com/Support/Validator/cvc-datatype-valid-1-2-1
cvc_datatype_valid_1_2_3("cvc-datatype-valid.1.2.3"), // https://wiki.xmldation.com/Support/Validator/cvc-datatype-valid-1-2-3
cvc_elt_1_a("cvc-elt.1.a"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-1
cvc_elt_3_1("cvc-elt.3.1"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-3-1
cvc_elt_3_2_1("cvc-elt.3.2.1"), // https://wiki.xmldation.com/Support/Validator/cvc-elt-3-2-1
Expand Down Expand Up @@ -223,22 +224,8 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case cvc_elt_3_2_1:
return XMLPositionUtility.selectContent(offset, document);
case cvc_type_3_1_3:
case cvc_datatype_valid_1_2_1: {
String attrValue = getString(arguments[0]);
Range range = XMLPositionUtility.selectAttributeValueFromGivenValue(attrValue, offset, document);

if (range != null) {
return range;
}

DOMElement element = (DOMElement) document.findNodeAt(offset);

if (DOMUtils.containsTextOnly(element)) {
return XMLPositionUtility.selectTrimmedText(offset, document);
} else {
return XMLPositionUtility.selectFirstChild(offset, document);
}
}
case cvc_datatype_valid_1_2_1:
case cvc_datatype_valid_1_2_3:
case cvc_enumeration_valid:
case cvc_maxlength_valid:
case cvc_minlength_valid:
Expand All @@ -254,7 +241,12 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
return range;
} else {
// Try with text
return XMLPositionUtility.selectContent(offset, document);
DOMElement element = (DOMElement) document.findNodeAt(offset);
if (DOMUtils.containsTextOnly(element)) {
return XMLPositionUtility.selectTrimmedText(offset, document);
} else {
return XMLPositionUtility.selectFirstChild(offset, document);
}
}
}
case cvc_type_3_1_2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,17 @@ public void cvc_datatype_valid_1_2_1_TwoElements() throws Exception {
d(3, 4, 3, 11, XMLSchemaErrorCode.cvc_type_3_1_3));
}

@Test
public void cvc_datatype_valid_1_2_3OnText() 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\">\r\n" + //
" XXX \r\n" + // <-- error
"</dresssize> ";
testDiagnosticsFor(xml, d(3, 15, 3, 18, XMLSchemaErrorCode.cvc_datatype_valid_1_2_3),
d(3, 15, 3, 18, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<dresssize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../xsd/dressSize.xsd">
XXX
</dresssize>
25 changes: 25 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/dressSize.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:simpleType name="SizeType">
<xs:union memberTypes="DressSizeType">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="small" />
<xs:enumeration value="medium" />
<xs:enumeration value="large" />
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:simpleType name="DressSizeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="2" />
<xs:maxInclusive value="18" />
</xs:restriction>
</xs:simpleType>

<xs:element name="dresssize" type="SizeType" />

</xs:schema>

0 comments on commit b63cc03

Please sign in to comment.