Skip to content

Commit

Permalink
Element/Attribute names must be case sensitive (see #433)
Browse files Browse the repository at this point in the history
This PR fix #433 and avoid using String#toLowerCase() which creates a
lot of instances of String.
  • Loading branch information
angelozerr committed Jun 14, 2019
1 parent fd86020 commit beb7b68
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import org.eclipse.lsp4xml.utils.StringUtils;
import org.w3c.dom.DOMException;
Expand Down Expand Up @@ -148,7 +149,6 @@ public Collection<String> getAllPrefixes() {
if (hasAttributes()) {
Collection<String> prefixes = new ArrayList<>();
for (DOMAttr attr : getAttributeNodes()) {
String attributeName = attr.getName();
if (attr.isNoDefaultXmlns()) {
prefixes.add(attr.extractPrefixFromXmlns());
}
Expand Down Expand Up @@ -259,9 +259,16 @@ public Integer isNextChar(char c, int startOffset) {
}


public boolean isSameTag(String tagInLowerCase) {
return this.tag != null && tagInLowerCase != null && this.tag.length() == tagInLowerCase.length()
&& this.tag.toLowerCase().equals(tagInLowerCase);
/**
* Returns true if the given tag is the same tag of this element and false
* otherwise.
*
* @param tag tag element
* @return true if the given tag is the same tag of this element and false
* otherwise.
*/
public boolean isSameTag(String tag) {
return Objects.equals(this.tag, tag);
}

public boolean isInStartTag(int offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
/**
eg: <a><b><c></d> will set a,b,c end position to the start of |</d>
*/
while (!(curr.isElement() && ((DOMElement) curr).isSameTag(closeTag.toLowerCase())) && curr.parent != null) {
while (!(curr.isElement() && ((DOMElement) curr).isSameTag(closeTag)) && curr.parent != null) {
curr.end = endTagOpenOffset;
curr = curr.parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public XMLScanner(String input, int initialOffset, ScannerState initialState, bo
}

String nextElementName() {
return stream.advanceIfRegExp(ELEMENT_NAME_REGEX).toLowerCase();
return stream.advanceIfRegExp(ELEMENT_NAME_REGEX);
}

String nextAttributeName() {
return stream.advanceIfRegExp(ATTRIBUTE_NAME_REGEX).toLowerCase();
return stream.advanceIfRegExp(ATTRIBUTE_NAME_REGEX);
}

String doctypeName() {
return stream.advanceIfRegExp(ELEMENT_NAME_REGEX).toLowerCase();
return stream.advanceIfRegExp(ELEMENT_NAME_REGEX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public void selfclosed() throws BadLocationException {
}

@Test
public void caseInsensivity() throws BadLocationException {
assertHighlights("<HTML><diV><Div></dIV></dI|v></html>", new int[] { 7, 24 }, "div");
assertHighlights("<HTML><diV|><Div></dIV></dIv></html>", new int[] { 7, 24 }, "div");
public void caseSensitive() throws BadLocationException {
assertHighlights("<HTML><diV><Div></dIV></dI|v></html>", new int[] { 24 }, "div");
assertHighlights("<HTML><diV|><Div></dIV></dIv></html>", new int[] { 7 }, "div");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void selfclosed() throws BadLocationException {
}

@Test
public void caseInsensivity() throws BadLocationException {
assertRename("<HTML><diV><Div></dIV></dI|v></html>", "newText", edits("newText", r(0, 7, 10), r(0, 24, 27)));
assertRename("<HTML><diV|><Div></dIV></dIv></html>", "newText", edits("newText", r(0, 7, 10), r(0, 24, 27)));
public void caseSensitive() throws BadLocationException {
assertRename("<HTML><diV><Div></dIV></dI|v></html>", "newText", edits("newText", r(0, 24, 27)));
assertRename("<HTML><diV|><Div></dIV></dIv></html>", "newText", edits("newText", r(0, 7, 10)));
}

@Test
Expand Down

0 comments on commit beb7b68

Please sign in to comment.