Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tag rename for XML tags containing colons #1135

Merged
merged 1 commit into from
Dec 9, 2021
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 @@ -180,6 +180,9 @@ private List<TextEdit> getTagNameRenameTextEdits(DOMDocument xmlDocument, DOMEle
String fullNodeName = element.getNodeName();
int indexOfColon = fullNodeName.indexOf(":");
if (indexOfColon > 0) {
// Take the prefix and local name of the namespace tag
String prefix = element.getPrefix();

Position startTagStartPosition = startTagRange.getStart();
Position startTagPrefixPosition = new Position(startTagStartPosition.getLine(),
startTagStartPosition.getCharacter() + indexOfColon);
Expand All @@ -191,23 +194,35 @@ private List<TextEdit> getTagNameRenameTextEdits(DOMDocument xmlDocument, DOMEle
Range startTagPrefixRange = new Range(startTagStartPosition, startTagPrefixPosition);
Range endTagPrefixRange = new Range(endTagStartPosition, endTagPrefixPosition);

if (doesTagCoverPosition(startTagPrefixRange, endTagPrefixRange, position)) {// Element prefix rename
String prefix = element.getPrefix();
return renameElementNamespace(xmlDocument, element, prefix.length(), newText);
} else { // suffix rename without wiping namespace
String suffixName = element.getLocalName();
int suffixLength = suffixName.length();
Position startTagEndPosition = startTagRange.getEnd();
Position suffixStartPositionStart = new Position(startTagEndPosition.getLine(),
startTagEndPosition.getCharacter() - suffixLength);

Position endTagEndPosition = endTagRange.getEnd();
Position suffixEndPositionStart = new Position(endTagEndPosition.getLine(),
endTagEndPosition.getCharacter() - suffixLength);

Range suffixRangeStart = new Range(suffixStartPositionStart, startTagEndPosition);
Range suffixRangeEnd = new Range(suffixEndPositionStart, endTagEndPosition);
String suffixName = element.getLocalName();
int suffixLength = suffixName.length();
Position startTagEndPosition = startTagRange.getEnd();
Position suffixStartPositionStart = new Position(startTagEndPosition.getLine(),
startTagEndPosition.getCharacter() - suffixLength);

Position endTagEndPosition = endTagRange.getEnd();
Position suffixEndPositionStart = new Position(endTagEndPosition.getLine(),
endTagEndPosition.getCharacter() - suffixLength);

Range suffixRangeStart = new Range(suffixStartPositionStart, startTagEndPosition);
Range suffixRangeEnd = new Range(suffixEndPositionStart, endTagEndPosition);

if (newText.contains(":")) {
String[] tagParts = newText.split(":", 2);
List<TextEdit> tagTextEdits = new ArrayList<TextEdit>();
// Tag prefix rename
if (!prefix.equals(tagParts[0])) {
tagTextEdits.addAll(renameElementNamespace(xmlDocument, element, prefix.length(), tagParts[0]));
}
// Tag suffix/local name rename
if (!suffixName.equals(tagParts[1])) {
tagTextEdits.addAll(getRenameList(suffixRangeStart, suffixRangeEnd, tagParts[1]));
}
return tagTextEdits;

} else if (doesTagCoverPosition(startTagPrefixRange, endTagPrefixRange, position)) {
return renameElementNamespace(xmlDocument, element, prefix.length(), newText);
} else {
return getRenameList(suffixRangeStart, suffixRangeEnd, newText);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void insideEndTag() throws BadLocationException {

@Test
public void testNamespaceRename() throws BadLocationException {
String xml =
String xml =
"<aa:a xmlns:a|a=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <aa:b></aa:b>\n" +
Expand All @@ -90,71 +90,103 @@ public void testNamespaceRename() throws BadLocationException {
" <qq:b></qq:b>\n" +
"</aa:a>";
assertRename(xml, "ns", edits("ns", r(0, 12, 14), r(0, 1, 3), r(6, 2, 4), //root attribute, start tag, end tag
r(1, 3, 5), r(1, 10, 12),
r(2, 3, 5), r(2, 10, 12),
r(1, 3, 5), r(1, 10, 12),
r(2, 3, 5), r(2, 10, 12),
r(3, 3, 5), //<aa:c
r(4, 11, 13))); // attribute value
}

@Test
public void testNamespaceRenameEndTagPrefix() throws BadLocationException {
String xml =
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <aa:b></a|a:b>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";
assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12)));
assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12)));
}

@Test
public void testNamespaceRenameStartTagPrefix() throws BadLocationException {
String xml =
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <a|a:b></aa:b>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";
assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12)));
assertRename(xml, "ns", edits("ns", r(2, 3, 5), r(2, 10, 12)));
}

@Test
public void testNamespaceRenameEndTagSuffix() throws BadLocationException {
String xml =
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <aa:|b></aa:b>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";
assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14)));
assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14)));
}

@Test
public void testNamespaceRenameStartTagSuffix() throws BadLocationException {
String xml =
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <aa:b></aa:b|>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";
assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14)));
assertRename(xml, "BB", edits("BB", r(2, 6, 7), r(2, 13, 14)));
}

@Test
public void testNamespaceRenameWholeTagHoverLocalName() throws BadLocationException {
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <aa:|b></aa:b>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";

List<TextEdit> expectedEdits = edits("ns", r(2, 3, 5), r(2, 10, 12));
expectedEdits.addAll(edits("tag", r(2, 6, 7), r(2, 13, 14)));
assertRename(xml, "ns:tag", expectedEdits);
}

@Test
public void testNamespaceRenameWholeTagHoverPrefix() throws BadLocationException {
String xml =
"<aa:a xmlns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
" <a|a:b></aa:b>\n" +
" <aa:c/>\n" +
" <t type=\"aa:b\"/>\n" +
" <qq:b></qq:b>\n" +
"</aa:a>";

List<TextEdit> expectedEdits = edits("ns", r(2, 3, 5), r(2, 10, 12));
expectedEdits.addAll(edits("tag", r(2, 6, 7), r(2, 13, 14)));
assertRename(xml, "ns:tag", expectedEdits);
}

@Test
public void testTryToRenameXMLNS() throws BadLocationException {
String xml =
String xml =
"<aa:a xml|ns:aa=\"aa.com\" xmlns:qq=\"qq.com\">\n" +
" <aa:b></aa:b>\n" +
"</aa:a>";
assertRename(xml, "BBBB");
assertRename(xml, "BBBB");
}


Expand Down