Skip to content

Commit

Permalink
Fix error range for cvc-complex-type-2.3
Browse files Browse the repository at this point in the history
Now handles text nodes that are only take up one line,
as well as <![CDATA[]]>.

Closes eclipse-lemminx#885

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 authored and angelozerr committed Oct 15, 2020
1 parent 11f6f6d commit af09659
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private XMLPositionUtility() {

/**
* Returns the attribute name range and null otherwise.
*
*
* @param attr the attribute.
* @return the attribute name range and null otherwise.
*/
Expand All @@ -100,7 +100,7 @@ public static Range selectAttributeNameAt(int offset, DOMDocument document) {

/**
* Returns the attribute value range and null otherwise.
*
*
* @param attr the attribute.
* @return the attribute value range and null otherwise.
*/
Expand Down Expand Up @@ -171,7 +171,7 @@ public static Range selectAttributeNameFromGivenNameAt(String attrName, int offs

/**
* Returns the range of the prefix of an attribute name
*
*
* For example, if attrName = "xsi:example", the range for "xsi" will be
* returned
*/
Expand Down Expand Up @@ -309,7 +309,7 @@ private static DOMNode findUnclosedChildNode(String childTag, List<DOMNode> chil
/**
* Returns the range of the root start tag (excludes the '<') of the given
* <code>document</code> and null otherwise.
*
*
* @param document the DOM document.
* @return the range of the root start tag (excludes the '<') of the given
* <code>document</code> and null otherwise.
Expand All @@ -328,9 +328,9 @@ public static Range selectRootStartTag(DOMDocument document) {
/**
* Finds the root element of the given document and returns the attribute value
* <code>Range</code> for the attribute <code>attrName</code>.
*
*
* If <code>attrName</code> is not declared then null is returned.
*
*
* @param attrName The name of the attribute to find the range of the value for
* @param document The document to use the root element of
* @return The range in <code>document</code> where the declared value of
Expand Down Expand Up @@ -363,7 +363,7 @@ public static Range selectStartTagName(int offset, DOMDocument document) {
/**
* Returns the range of the start tag name (excludes the '<') of the given
* <code>element</code> and null otherwise.
*
*
* @param element the DOM element
* @return the range of the start tag of the given <code>element</code> and null
* otherwise.
Expand All @@ -375,7 +375,7 @@ public static Range selectStartTagName(DOMNode element) {
/**
* Returns the range of a tag's local name. If the tag does not have a prefix,
* implying it doesn't have a local name, it will return null.
*
*
* @param element
* @return
*/
Expand All @@ -386,10 +386,10 @@ public static Range selectStartTagLocalName(DOMNode element) {
/**
* Returns the range of the start tag name (excludes the '<') of the given
* <code>element</code> and null otherwise.
*
*
* If suffixOnly is true then it will try to return the range of the
* localName/suffix. Else it will return null.
*
*
* @param element the DOM element
* @param suffixOnly select the suffix portion, only when a prefix exists
* @return the range of the start tag of the given <code>element</code> and null
Expand Down Expand Up @@ -450,7 +450,7 @@ public static Range selectEndTagName(int offset, DOMDocument document) {
/**
* Returns the range of the end tag of the given <code>element</code> name and
* null otherwise.
*
*
* @param element the DOM element
* @return the range of the end tag of the given <code>element</code> and null
* otherwise.
Expand All @@ -462,7 +462,7 @@ public static Range selectEndTagName(DOMElement element) {
/**
* Returns the range of the end tag of the given LOCAL <code>element</code> name
* and null otherwise.
*
*
* @param element the DOM element
* @return the range of the end tag of the given <code>element</code> and null
* otherwise.
Expand All @@ -474,7 +474,7 @@ public static Range selectEndTagLocalName(DOMElement element) {
/**
* Returns the range of the end tag of the given <code>element</code> and null
* otherwise.
*
*
* @param element the DOM element
* @return the range of the end tag of the given <code>element</code> and null
* otherwise.
Expand Down Expand Up @@ -502,7 +502,7 @@ public static Range selectEndTagName(DOMElement element, boolean localNameOnly)
/**
* Returns the range of the entity reference in a text node (ex : &amp;) and
* null otherwise.
*
*
* @param offset the offset
* @param document the document
* @return the range of the entity reference in a text node (ex : &amp;) and
Expand All @@ -515,7 +515,7 @@ public static EntityReferenceRange selectEntityReference(int offset, DOMDocument
/**
* Returns the range of the entity reference in a text node (ex : &amp;) and
* null otherwise.
*
*
* @param offset the offset
* @param document the document
* @param endsWithSemicolon true if the entity reference must end with ';' and
Expand Down Expand Up @@ -547,7 +547,7 @@ public static EntityReferenceRange selectEntityReference(int offset, DOMDocument
/**
* Returns the start offset of the entity reference (ex : &am|p;) from the left
* of the given offset and -1 if no entity reference.
*
*
* @param text the XML content.
* @param offset the offset.
* @return the start offset of the entity reference (ex : &am|p;) from the left
Expand Down Expand Up @@ -582,7 +582,7 @@ public static int getEntityReferenceStartOffset(String text, int offset) {
/**
* Returns the end offset of the entity reference (ex : &am|p;) from the right
* of the given offset and -1 if no entity reference.
*
*
* @param text the XML content.
* @param offset the offset.
* @return the end offset of the entity reference (ex : &am|p;) from the right
Expand All @@ -605,12 +605,13 @@ public static Range selectFirstNonWhitespaceText(int offset, DOMDocument documen
DOMNode element = document.findNodeAt(offset);
if (element != null) {
for (DOMNode node : element.getChildren()) {
if (node.isCharacterData() && ((DOMCharacterData) node).hasMultiLine()) {
String content = ((DOMCharacterData) node).getData();
int start = node.getStart();
if (node.isCharacterData()) {
DOMCharacterData data = (DOMCharacterData) node;
int start = data.getStartContent();
Integer end = null;
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
String text = document.getText();
for (int i = start; i < data.getEndContent(); i++) {
char c = text.charAt(i);
if (end == null) {
if (Character.isWhitespace(c)) {
start++;
Expand All @@ -637,7 +638,7 @@ public static Range selectFirstNonWhitespaceText(int offset, DOMDocument documen

/**
* Returns the text content range and null otherwise.
*
*
* @param text the DOM text node..
* @return the text content range and null otherwise.
*/
Expand Down Expand Up @@ -670,17 +671,17 @@ public static Range selectContent(int offset, DOMDocument document) {
/**
* Returns the range covering the trimmed text belonging to the node located at
* offset.
*
*
* This method assumes that the node located at offset only contains text.
*
*
* For example, if the node located at offset is:
*
*
* <a> hello
*
*
* </a>
*
*
* the returned range will cover only "hello".
*
*
* @param offset
* @param document
* @return range covering the trimmed text belonging to the node located at
Expand Down Expand Up @@ -731,7 +732,7 @@ public static Range selectDTDElementDeclAt(int offset, DOMDocument document) {
/**
* Will give the range for the last VALID DTD Decl parameter at 'offset'. An
* unrecognized Parameter is not considered VALID,
*
*
* eg: "<!ELEMENT elementName (content) UNRECOGNIZED_CONTENT" will give the
* range of 1 character length, after '(content)'
*/
Expand Down Expand Up @@ -768,7 +769,7 @@ public static Range getLastValidDTDDeclParameter(int offset, DOMDocument documen
/**
* Will give the range for the last VALID DTD Decl parameter at 'offset'. An
* unrecognized Parameter is not considered VALID,
*
*
* eg: <!ELEMENT elementName (content) UNRECOGNIZED_CONTENT will give the range
* 1 character after '(content)'
*/
Expand Down Expand Up @@ -799,7 +800,7 @@ public static Range getLastValidDTDDeclParameterOrUnrecognized(int offset, DOMDo
/**
* Will give the range for the last DTD Decl parameter at 'offset'. An
* unrecognized Parameter is considered as well.
*
*
* eg: <!ELEMENT elementName (content) UNRECOGNIZED_CONTENT will give the range
* 1 character after '(content)'
*/
Expand Down Expand Up @@ -848,7 +849,7 @@ public static Range getElementDeclMissingContentOrCategory(int offset, DOMDocume

/**
* Returns the range for the given <code>node</code>.
*
*
* @param node the node
* @return the range for the given <code>node</code>.
*/
Expand All @@ -868,7 +869,7 @@ public static Range createRange(int startOffset, int endOffset, DOMDocument docu
/**
* Returns the location link for the given <code>origin</code> and
* <code>target</code> nodes.
*
*
* @param origin the origin node.
* @param target the target node.
* @return the location link for the given <code>origin</code> and
Expand All @@ -887,7 +888,7 @@ public static LocationLink createLocationLink(DOMRange origin, DOMRange target)
/**
* Returns the location link for the given <code>origin</code> and
* <code>target</code> nodes.
*
*
* @param origin the origin node.
* @param target the target node.
* @return the location link for the given <code>origin</code> and
Expand All @@ -903,7 +904,7 @@ public static LocationLink createLocationLink(Range origin, DOMRange target) {
/**
* Returns the location link for the given <code>origin</code> and
* <code>target</code> nodes.
*
*
* @param origin the origin node.
* @param target the target node.
* @return the location link for the given <code>origin</code> and
Expand All @@ -917,7 +918,7 @@ public static LocationLink createLocationLink(Range origin, TargetRange target)

/**
* Returns the location for the given <code>target</code> node.
*
*
* @param target the target node.
* @return the location for the given <code>target</code> node.
*/
Expand All @@ -929,7 +930,7 @@ public static Location createLocation(DOMRange target) {

/**
* Create a document link
*
*
* @param target The range in the document that should be the link
* @param location URI where the link should point
* @param adjust <code>true</code> means the first and last character of
Expand All @@ -949,10 +950,10 @@ public static DocumentLink createDocumentLink(DOMRange target, String location,

/**
* Returns the range covering the first child of the node located at offset.
*
*
* Returns null if node is not a DOMElement, or if a node does not exist at
* offset.
*
*
* @param offset
* @param document
* @return range covering the first child of the node located at offset
Expand Down
Loading

0 comments on commit af09659

Please sign in to comment.