Skip to content

Commit

Permalink
Select start tag when none text to select (see #145)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Sep 26, 2018
1 parent e5c67b2 commit 90bd091
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static Range selectAttributeNameFromGivenNameAt(String attrName, int offs
}
return null;
}

private static Range createAttrNameRange(Attr attr, XMLDocument document) {
int startOffset = attr.getNodeName().getStart();
int endOffset = attr.getNodeName().getEnd();
Expand Down Expand Up @@ -174,19 +174,23 @@ static Node findChildNode(String childTag, List<Node> children) {
public static Range selectStartTag(int offset, XMLDocument document) {
Node element = document.findNodeAt(offset);
if (element != null) {
int startOffset = element.getStart() + 1; // <
int endOffset = startOffset + getStartTagLength(element);
if (element.isProcessingInstruction() || element.isProlog()) {
// in the case of prolog or processing instruction, tag is equals to "xml"
// without '?' -> <?xml
// increment end offset to select '?xml' instead of selecting '?xm'
endOffset++;
}
return createRange(startOffset, endOffset, document);
return selectStartTag(element, document);
}
return null;
}

private static Range selectStartTag(Node element, XMLDocument document) {
int startOffset = element.getStart() + 1; // <
int endOffset = startOffset + getStartTagLength(element);
if (element.isProcessingInstruction() || element.isProlog()) {
// in the case of prolog or processing instruction, tag is equals to "xml"
// without '?' -> <?xml
// increment end offset to select '?xml' instead of selecting '?xm'
endOffset++;
}
return createRange(startOffset, endOffset, document);
}

private static int getStartTagLength(Node node) {
if (node.isElement()) {
Element element = (Element) node;
Expand Down Expand Up @@ -310,11 +314,17 @@ public static Range createRange(int startOffset, int endOffset, XMLDocument docu

public static Range selectText(int offset, XMLDocument document) {
Node node = document.findNodeAt(offset);
if (node != null && node.hasChildren()) {
for (Node child : node.getChildren()) {
if (child.isText()) {
return createRange(child.getStart(), child.getEnd(), document);
if (node != null) {
if (node.hasChildren()) {
// <root>BAD TEXT</root>
for (Node child : node.getChildren()) {
if (child.isText()) {
return createRange(child.getStart(), child.getEnd(), document);
}
}
} else if (node.isElement()) {
// node has NONE text (ex: <root></root>, select the start tag
return selectStartTag(node, document);
}
}
return null;
Expand Down

0 comments on commit 90bd091

Please sign in to comment.