Skip to content

Commit

Permalink
XML attribute associated to wrong type from XSD
Browse files Browse the repository at this point in the history
Fixes redhat-developer/vscode-xml#524

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
azerr authored and angelozerr committed Feb 16, 2023
1 parent 5cf4a9c commit c51f2cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Expand Up @@ -141,8 +141,9 @@ public static Range selectAttributeValueFromGivenValue(String attrValue, int off
DOMNode element = document.findNodeAt(offset);
if (element != null && element.hasAttributes()) {
List<DOMAttr> attribues = element.getAttributeNodes();
for (DOMAttr attr : attribues) {
if (attrValue.equals(attr.getValue())) {
for (int i = attribues.size() - 1; i >= 0; i--) {
DOMAttr attr = attribues.get(i);
if (offset > attr.getStart() && attrValue.equals(attr.getValue())) {
return createAttrValueRange(attr, document);
}
}
Expand Down Expand Up @@ -1151,22 +1152,22 @@ public static Position getMatchingTagPosition(DOMDocument xmlDocument, Position

/**
* Select the value from the start/end node without quote.
*
*
* For the given attr value:
*
*
* <p>
* <element attr="value" />
* </p>
*
*
* it will return <element attr="|value|" /> range without ".
*
*
* @param node the DOM node.
*
*
* @return the value from the start/end node without quote.
*/
public static Range selectValueWithoutQuote(DOMRange node) {
DOMDocument document = node.getOwnerDocument();
return createRange(node.getStart() + 1, node.getEnd() - 1, document);
}

}
}
Expand Up @@ -509,6 +509,19 @@ public void cvc_datatype_valid_1_2_3Empty() throws Exception {
d(1, 1, 1, 10, XMLSchemaErrorCode.cvc_type_3_1_3));
}

@Test
public void cvc_minLength() throws Exception {
// See issue https://github.com/redhat-developer/vscode-xml/issues/524
String xml = "<?xml version=\"1.0\"?>\r\n"
+ "<?xml-model href=\"src/test/resources/xsd/minLength.xsd\"?>\r\n"
+ "<Root>\r\n"
+ " <Test Some_String=\"\" Some_Restricted_Value=\"\"/>\r\n"
+ "</Root>";
testDiagnosticsFor(xml, //
d(3, 47, 3, 49, XMLSchemaErrorCode.cvc_minlength_valid),
d(3, 47, 3, 49, XMLSchemaErrorCode.cvc_attribute_3));
}

@Test
public void cvc_maxLength_validOnAttribute() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down
21 changes: 21 additions & 0 deletions org.eclipse.lemminx/src/test/resources/xsd/minLength.xsd
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="restrictedType">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>

<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Test">
<xs:complexType>
<xs:attribute name="Some_String" type="xs:string"/>
<xs:attribute name="Some_Restricted_Value" type="restrictedType"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

0 comments on commit c51f2cf

Please sign in to comment.