Skip to content

Commit

Permalink
Improve ETagRequired error range
Browse files Browse the repository at this point in the history
Fixes #876

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 24, 2020
1 parent 3ea8abb commit 3443530
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,44 @@ public int getStartTagCloseOffset() {
return startTagCloseOffset;
}

public int getBestStartTagCloseOffset() {
if (isStartTagClosed()) {
return getStartTagCloseOffset();
}
if (hasEndTag()) {
// ex : <foo </foo>
if (hasAttributes()) {
// ex : <foo attr="" </foo>
// returns end offset of last attributes
DOMAttr lastAttr = getAttributeNodes().get(getAttributeNodes().size()-1);
return lastAttr.getEnd();
}
// returns position of tag name --> <foo| </foo>
return getStartTagOpenOffset() + getTagName().length() + 1;
}
String text = getOwnerDocument().getText();
int endOffset = text.length() - 1;
DOMNode nextNode = getFirstChild();
if (nextNode == null) {
nextNode = getNextSibling();
}
if (nextNode != null) {
endOffset = ((DOMElement) nextNode).getStartTagOpenOffset() - 1;
}
if (nextNode == null) {
DOMElement parent = getParentElement();
if (parent != null && parent.hasEndTag()) {
endOffset = parent.getEndTagOpenOffset() - 1;
}
}
// remove spaces
while (Character.isWhitespace(text.charAt(endOffset))) {
endOffset--;
}
endOffset++;
return endOffset;
}

/**
* Returns the end tag open offset and {@link DOMNode#NULL_VALUE} if it doesn't
* exist.
Expand Down Expand Up @@ -397,12 +435,23 @@ public boolean isEndTagClosed() {
* Returns true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) and false otherwise.
*
* @param tagName the end tag name.
* @return true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) and false otherwise.
*/
public boolean isOrphanEndTag(String tagName) {
return isSameTag(tagName) && hasEndTag() && !hasStartTag();
public boolean isOrphanEndTag() {
return hasEndTag() && !hasStartTag();
}

/**
* Returns true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) of the given tag name and false otherwise.
*
* @param tagName the end tag name.
* @return true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) of the given tag name and false otherwise.
*/
public boolean isOrphanEndTagOf(String tagName) {
return isSameTag(tagName) && isOrphanEndTag();
}

@Override
Expand All @@ -423,7 +472,7 @@ public DOMElement getOrphanEndElement(int offset, String tagName) {
for (DOMNode child : children) {
if (child.isElement()) {
DOMElement childElement = (DOMElement) child;
if (childElement.isOrphanEndTag(tagName)) {
if (childElement.isOrphanEndTagOf(tagName)) {
return childElement;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ public DOMElement getOrphanEndElement(int offset, String tagName) {
}
// emp| </employe>
DOMElement nextElement = (DOMElement) next;
if (nextElement.isOrphanEndTag(tagName)) {
if (nextElement.isOrphanEndTagOf(tagName)) {
return nextElement;
}
return null;
Expand Down
Loading

0 comments on commit 3443530

Please sign in to comment.