Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.lemminx.dom.XMLModel;
import org.eclipse.lemminx.services.extensions.IDocumentLinkParticipant;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.eclipse.lemminx.utils.DOMUtils;
import org.eclipse.lsp4j.DocumentLink;
import org.w3c.dom.NamedNodeMap;

Expand Down Expand Up @@ -64,7 +65,7 @@ public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
if (location != null) {
try {
DOMRange systemIdRange = docType.getSystemIdNode();
if (systemIdRange != null) {
if (DOMUtils.isNonEmptyRange(systemIdRange, true)) {
links.add(createDocumentLink(systemIdRange, location, true));
}
} catch (BadLocationException e) {
Expand All @@ -81,7 +82,7 @@ public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
if (location != null) {
try {
DOMRange systemIdRange = entity.getSystemIdNode();
if (systemIdRange != null) {
if (DOMUtils.isNonEmptyRange(systemIdRange, true)) {
links.add(createDocumentLink(systemIdRange, location, true));
}
} catch (BadLocationException e) {
Expand All @@ -98,7 +99,7 @@ public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
if (location != null) {
try {
DOMRange hrefRange = xmlModel.getHrefNode();
if (hrefRange != null) {
if (DOMUtils.isNonEmptyRange(hrefRange, true)) {
links.add(createDocumentLink(hrefRange, location, true));
}
} catch (BadLocationException e) {
Expand All @@ -115,7 +116,7 @@ public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
noNamespaceSchemaLocation.getLocation());
if (location != null) {
DOMRange attrValue = noNamespaceSchemaLocation.getAttr().getNodeAttrValue();
if (attrValue != null) {
if (DOMUtils.isNonEmptyRange(attrValue, true)) {
links.add(createDocumentLink(attrValue, location, true));
}
}
Expand All @@ -132,7 +133,7 @@ public void findDocumentLinks(DOMDocument document, List<DocumentLink> links) {
String location;
for (SchemaLocationHint schemaLocationHint : schemaLocationHints) {
location = resolverManager.resolve(document.getDocumentURI(), null, schemaLocationHint.getHint());
if (location != null) {
if (DOMUtils.isNonEmptyRange(schemaLocationHint, false)) {
links.add(createDocumentLink(schemaLocationHint, location, false));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMParser;
import org.eclipse.lemminx.dom.DOMRange;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.xml.sax.InputSource;
import org.xml.sax.SAXNotRecognizedException;
Expand Down Expand Up @@ -287,4 +288,18 @@ public static InputSource createInputSource(DOMDocument document) {
inputSource.setSystemId(uri);
return inputSource;
}

/**
* Returns false if the range is zero-length, and true otherwise.
*
* @param range the range to check
* @param adjust true if the leading and trailing quotes should be removed before checking if it's zero-length, false otherwise
* @return false if the range is zero-length, and true otherwise
*/
public static boolean isNonEmptyRange(DOMRange range, boolean adjust) {
if (range == null) {
return false;
}
return range.getEnd() - range.getStart() > (adjust ? 2 : 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,13 @@ public static Location createLocation(DOMRange target) {
public static DocumentLink createDocumentLink(DOMRange target, String location, boolean adjust)
throws BadLocationException {
DOMDocument document = target.getOwnerDocument();
Position start = document.positionAt(target.getStart() + (adjust ? 1 : 0));
Position end = document.positionAt(target.getEnd() - (adjust ? 1 : 0));
int startOffset = target.getStart() + (adjust ? 1 : 0);
int endOffset = target.getEnd() - (adjust ? 1 : 0);
if (startOffset == endOffset) {
throw new IllegalArgumentException("empty range");
}
Position start = document.positionAt(startOffset);
Position end = document.positionAt(endOffset);
return new DocumentLink(new Range(start, end), location);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public void testCatalogWithCatalogDOCTYPE() {

}

@Test
public void testCatalogWithCatalogDOCTYPEZeroLength() {
String xml = "<!DOCTYPE catalog PUBLIC \"-//OASIS//DTD XML Catalogs V1.1//EN\" \"catalog.dtd\" >\n"
+ //
"<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
" <uri\n" + //
" id=\"\"\n" + //
" uri=\"\" />\n" + //
"</catalog>";
testDocumentLinkFor(xml, CATALOG_PATH,
dl(r(0, 64, 0, 75), "src/test/resources/catalog.dtd"));

}

@Test
public void testSystemSuffixEntryDocumentLink() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
Expand All @@ -78,6 +92,14 @@ public void testSystemSuffixEntryDocumentLink() {
dl(r(1, 45, 1, 57), "src/test/resources/mySchema.xsd"));
}

@Test
public void testSystemSuffixEntryZeroLengthDocumentLink() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
" <systemSuffix id=\"http://example.org\" uri=\"\"></systemSuffix>\n" + //
"</catalog>";
testDocumentLinkFor(xml, CATALOG_PATH);
}

@Test
public void testURISuffixEntryDocumentLink() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
Expand All @@ -87,6 +109,14 @@ public void testURISuffixEntryDocumentLink() {
dl(r(1, 42, 1, 54), "src/test/resources/mySchema.xsd"));
}

@Test
public void testURISuffixEntryZeroLengthDocumentLink() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
" <uriSuffix id=\"\" uri=\"\"></uriSuffix>\n" + //
"</catalog>";
testDocumentLinkFor(xml, CATALOG_PATH);
}

@Test
public void testMustBeCatalog1() {
String xml = "<catalog><public url=\"document.xsd\" /></catalog>";
Expand Down Expand Up @@ -127,6 +157,14 @@ public void testDelegatePublicEntry() {
dl(r(1, 27, 1, 54), "src/test/resources/catalogs/catalog-public.xml"));
}

@Test
public void testDelegatePublicEntryZeroLength() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
" <delegatePublic catalog=\"\" />\n" + //
"</catalog>";
testDocumentLinkFor(xml, CATALOG_PATH);
}

@Test
public void testDelegateSystemEntry() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
Expand All @@ -136,6 +174,14 @@ public void testDelegateSystemEntry() {
dl(r(1, 27, 1, 54), "src/test/resources/catalogs/catalog-public.xml"));
}

@Test
public void testDelegateSystemEntryZeroLength() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
" <delegateSystem catalog=\"\" />\n" + //
"</catalog>";
testDocumentLinkFor(xml, CATALOG_PATH);
}

@Test
public void testDelegateUriEntry() {
String xml = "<catalog xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n" + //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ public void docTypeSYSTEM() throws BadLocationException {
dl(r(1, 31, 1, 55), "src/test/resources/dtd/entities/base.dtd"));
}

@Test
public void docTypeSYSTEMZeroLengh() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + //
"<!DOCTYPE root-element SYSTEM \"\" [\r\n" + //
"\r\n" + //
"]>\r\n" + //
"<root-element />";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xml/base.xml");
}

@Test
public void docTypePUBLIC() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + //
Expand All @@ -47,6 +57,16 @@ public void docTypePUBLIC() throws BadLocationException {
dl(r(1, 38, 1, 62), "src/test/resources/dtd/entities/base.dtd"));
}

@Test
public void docTypePUBLICZeroLength() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + //
"<!DOCTYPE root-element PUBLIC \"ABCD\" \"\" [\r\n" + //
"\r\n" + //
"]>\r\n" + //
"<root-element />";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xml/base.xml");
}

@Test
public void noLinks() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n" + //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,34 @@ public void xmlModelHref() throws BadLocationException {
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/Format.xml",
dl(r(1, 18, 1, 32), "src/test/resources/xsd/Format.xsd"));
}

@Test
public void xmlModelHrefZeroLength() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + //
"<?xml-model href=\"\" ?>\r\n" + //
"<Configuration>\r\n" + //
" <ViewDefinitions>\r\n" + //
" <View>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/Format.xml");
}

@Test
public void xmlModelHrefZeroLength2() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + //
"<?xml-model href= ?>\r\n" + //
"<Configuration>\r\n" + //
" <ViewDefinitions>\r\n" + //
" <View>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/Format.xml");
}

@Test
public void xmlModelHrefZeroLength3() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + //
"<?xml-model href ?>\r\n" + //
"<Configuration>\r\n" + //
" <ViewDefinitions>\r\n" + //
" <View>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/Format.xml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public void entity() throws BadLocationException {
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xml/base.dtd",
dl(r(0, 28, 0, 43), "src/test/resources/document.ent"));
}

@Test
public void entityZeroLength() throws BadLocationException {
String xml = "<!ENTITY % document SYSTEM \"\">";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xml/base.dtd");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,55 @@ public void externalRef() throws BadLocationException {
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/relaxng/main.rng",
dl(r(11, 24, 11, 34), "src/test/resources/relaxng/inline.rng"));
}

@Test
public void emptyIncludeHref() throws BadLocationException {
String xml = "<grammar xmlns=\"http://relaxng.org/ns/structure/1.0\" >\n"
+ " <include href=\"\">\n"
Comment thread
datho7561 marked this conversation as resolved.
+ " <define name=\"cardContent\">\n"
+ " <element name=\"name\">\n"
+ " <text />\n"
+ " </element>\n"
+ " <element name=\"emailAddress\">\n"
+ " <text />\n"
+ " </element>\n"
+ " </define>\n"
+ " </include>\n"
+ "</grammar>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/relaxng/main.rng");
}

@Test
public void emptyIncludeHref2() throws BadLocationException {
String xml = "<grammar xmlns=\"http://relaxng.org/ns/structure/1.0\" >\n"
+ " <include href= >\n"
+ " <define name=\"cardContent\">\n"
+ " <element name=\"name\">\n"
+ " <text />\n"
+ " </element>\n"
+ " <element name=\"emailAddress\">\n"
+ " <text />\n"
+ " </element>\n"
+ " </define>\n"
+ " </include>\n"
+ "</grammar>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/relaxng/main.rng");
}

@Test
public void emptyIncludeHref3() throws BadLocationException {
String xml = "<grammar xmlns=\"http://relaxng.org/ns/structure/1.0\" >\n"
+ " <include href >\n"
+ " <define name=\"cardContent\">\n"
+ " <element name=\"name\">\n"
+ " <text />\n"
+ " </element>\n"
+ " <element name=\"emailAddress\">\n"
+ " <text />\n"
+ " </element>\n"
+ " </define>\n"
+ " </include>\n"
+ "</grammar>";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/relaxng/main.rng");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ public void includeHrefInAnyLevel() throws BadLocationException {
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xinclude/main.xml",
dl(r(3, 22, 3, 35), "src/test/resources/xinclude/reference.xml"));
}

@Test
public void includeHrefZeroLength() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
"<book xmlns:xi=\"http://www.w3.org/2001/XInclude\"> \n" + //
" <author> \n" + //
" <xi:include href=\"\" />\n" + // <-- documentLink
" </author>\n" + //
"</book>\n";
XMLAssert.testDocumentLinkFor(xml, "src/test/resources/xinclude/main.xml");
}
}
Loading