Skip to content

Commit

Permalink
Added DOMElement properties (more offset values). Also fixed formatti…
Browse files Browse the repository at this point in the history
…ng issues. (#249)

Fixes #227

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen authored Dec 5, 2018
1 parent b349053 commit f9676fe
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class DOMElement extends DOMNode implements org.w3c.dom.Element {

String tag;
boolean selfClosed;

//DomElement.start == startTagOpenOffset
Integer startTagOpenOffset; // |<root>
Integer startTagCloseOffset; // <root |>

Integer startTagOpenOffset;
Integer startTagCloseOffset; // <root|>
// Integer startTagSelfCloseOffset;
Integer endTagOpenOffset; // <root>|</root>
Integer endTagCloseOffset;
Integer endTagOffset;
Integer endTagOpenOffset; // <root> |</root >
Integer endTagCloseOffset;// <root> </root |>
//DomElement.end = <root> </root>| , is always scanner.getTokenEnd()

public DOMElement(int start, int end, DOMDocument ownerDocument) {
super(start, end, ownerDocument);
Expand Down Expand Up @@ -274,7 +275,7 @@ public boolean isInEndTag(int offset) {
// case >|
return false;
}
if (offset > endTagOpenOffset && offset <= getEnd()) {
if (offset > endTagOpenOffset && offset < getEnd()) {
// case <\bean | >
return true;
}
Expand All @@ -297,6 +298,10 @@ public Integer getEndTagOpenOffset() {
return endTagOpenOffset;
}

public Integer getEndTagCloseOffset() {
return endTagCloseOffset;
}

/**
* Returns true if has a start tag.
*
Expand All @@ -321,6 +326,28 @@ public boolean hasEndTag() {
return endTagOpenOffset != null;
}

/**
* If '>' exists in <root>
*/
public boolean isStartTagClosed() {
return startTagCloseOffset != null;
}

/**
* If '>' exists in </root>
*/
public boolean isEndTagClosed() {
return endTagCloseOffset != null;
}

@Override
/**
* If Element has a closing end tag eg: <a> </a> -> true , <a> </b> -> false
*/
public boolean isClosed() {
return super.isClosed();
}

@Override
public String getAttributeNS(String arg0, String arg1) throws DOMException {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public abstract class DOMNode implements Node {
private XMLNamedNodeMap<DOMAttr> attributeNodes;
private XMLNodeList<DOMNode> children;

final int start;
int end;
final int start; // |<root> </root>
int end; // <root> </root>|

DOMNode parent;
private final DOMDocument ownerDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
while (token != TokenType.EOS) {
switch (token) {
case StartTagOpen: {
DOMElement child = xmlDocument.createElement(scanner.getTokenOffset(), text.length());
if(curr.parent != null) {
curr.end = scanner.getTokenOffset();
}
DOMElement child = xmlDocument.createElement(scanner.getTokenOffset(), scanner.getTokenEnd());
child.startTagOpenOffset = scanner.getTokenOffset();
curr.addChild(child);
curr = child;
Expand All @@ -71,14 +74,18 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
case StartTag: {
DOMElement element = (DOMElement) curr;
element.tag = scanner.getTokenText();
curr.end = scanner.getTokenEnd();
break;
}


case StartTagClose:
if (curr.isElement()) {
DOMElement element = (DOMElement) curr;
curr.end = scanner.getTokenEnd(); // might be later set to end tag position
element.startTagCloseOffset = scanner.getTokenOffset();

//never enters isEmptyElement() is always false
if (element.getTagName() != null && isEmptyElement(element.getTagName()) && curr.parent != null) {
curr.closed = true;
curr = curr.parent;
Expand All @@ -92,19 +99,24 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
curr = curr.parent;
}
}
curr.end = scanner.getTokenEnd();
break;

case EndTagOpen:
endTagOpenOffset = scanner.getTokenOffset();
curr.end = scanner.getTokenOffset();
break;

case EndTag:
// end tag (ex: </root>)
String closeTag = scanner.getTokenText().toLowerCase();
DOMNode current = curr;

/**
eg: <a><b><c></d> will set a,b,c end position to the start of |</d>
*/
while (!(curr.isElement() && ((DOMElement) curr).isSameTag(closeTag)) && curr.parent != null) {
curr.end = endTagOpenOffset;
curr.closed = false;
curr = curr.parent;
}
if (curr != xmlDocument) {
Expand All @@ -114,10 +126,11 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
} else if (curr.isProcessingInstruction() || curr.isProlog()) {
((DOMProcessingInstruction) curr).endTagOpenOffset = endTagOpenOffset;
}
curr.end = scanner.getTokenEnd();
} else {
// element open tag not found (ex: <root>) add a fake elementg which have just
// element open tag not found (ex: <root>) add a fake element which have just
// end tag (no start tag).
DOMElement element = xmlDocument.createElement(scanner.getTokenOffset() - 2, text.length());
DOMElement element = xmlDocument.createElement(scanner.getTokenOffset() - 2, scanner.getTokenEnd());
element.endTagOpenOffset = endTagOpenOffset;
element.tag = closeTag;
current.addChild(element);
Expand All @@ -139,6 +152,9 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
if (curr.parent != null) {
curr.end = scanner.getTokenEnd();
lastClosed = curr;
if(lastClosed.isElement()) {
((DOMElement) curr).endTagCloseOffset = scanner.getTokenOffset();
}
curr = curr.parent;
}
break;
Expand All @@ -148,6 +164,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
attr = new DOMAttr(pendingAttribute, scanner.getTokenOffset(),
scanner.getTokenOffset() + pendingAttribute.length(), curr);
curr.setAttributeNode(attr);
curr.end = scanner.getTokenEnd();
break;
}

Expand All @@ -158,6 +175,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
}
pendingAttribute = null;
attr = null;
curr.end = scanner.getTokenEnd();
break;
}

Expand All @@ -172,6 +190,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
DOMCDATASection cdataNode = (DOMCDATASection) curr;
cdataNode.startContent = scanner.getTokenOffset();
cdataNode.endContent = scanner.getTokenEnd();
curr.end = scanner.getTokenEnd();
break;
}

Expand Down Expand Up @@ -313,7 +332,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
case DTDEndInternalSubset: {
if (!curr.isDoctype()) {
curr.end = scanner.getTokenOffset() - 1;
curr =curr.getParentNode();
curr = curr.getParentNode();
}

DOMDocumentType doctype = (DOMDocumentType) curr;
Expand All @@ -324,7 +343,7 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
case DTDStartElementDecl: {
if (!curr.isDoctype()) {
curr.end = scanner.getTokenOffset() - 1;
curr =curr.getParentNode();
curr = curr.getParentNode();
}

DTDElementDecl child = new DTDElementDecl(scanner.getTokenOffset(), text.length(),
Expand Down Expand Up @@ -397,9 +416,8 @@ public DOMDocument parse(TextDocument document, URIResolverExtensionManager reso
}
token = scanner.scan();
}
while (curr.parent != null) {
while (curr.parent != null ) {
curr.end = text.length();
curr.closed = false;
curr = curr.parent;
}
return xmlDocument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private int generate(CMElementDeclaration elementDeclaration, String prefix, int
xml.endElement(prefix, elementDeclaration.getName());
}
} else if (elementDeclaration.isEmpty() && autoCloseTags) {
xml.endElement();
xml.selfCloseElement();
} else {
xml.closeStartElement();
if (canSupportSnippets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void format(DOMNode node, int level, int end, XMLBuilder xml) {
String tag = element.getTagName();
if (element.hasEndTag() && !element.hasStartTag()) {
// bad element which have not start tag (ex: <\root>)
xml.endElement(tag);
xml.endElement(tag, element.isEndTagClosed());
} else {
xml.startElement(tag, false);
if (element.hasAttributes()) {
Expand All @@ -178,12 +178,17 @@ private void format(DOMNode node, int level, int end, XMLBuilder xml) {
}
}
}

if(element.isStartTagClosed()) {
xml.closeStartElement();
}

boolean hasElements = false;
boolean startElementClosed = false;
if (node.hasChildNodes()) {
// element has body
xml.closeStartElement();
startElementClosed = true;


//startElementClosed = true;
level++;
for (DOMNode child : node.getChildren()) {
boolean textElement = !child.isText();
Expand All @@ -194,25 +199,21 @@ private void format(DOMNode node, int level, int end, XMLBuilder xml) {
}
level--;
}
if (node.isClosed()) {
if (element.hasEndTag()) {
if (hasElements) {
xml.linefeed();
xml.indent(level);
}
// end tag element is done, only if the element is closed
// the format, doesn't fix the close tag
if (element.hasEndTag() && element.getEndTagOpenOffset() <= end) {
if (!startElementClosed) {
xml.closeStartElement();
}
xml.endElement(tag);
xml.endElement(tag, element.isEndTagClosed());
} else {
xml.endElement();
}
} else if (element.hasStartTagClose()) {
if (!startElementClosed) {
xml.closeStartElement();
xml.selfCloseElement();
}
} else if (element.isSelfClosed()) {
xml.selfCloseElement();

}
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,28 @@ public XMLBuilder startElement(String name, boolean close) {
return startElement(null, name, close);
}

public XMLBuilder endElement(String name, boolean isEndTagClosed) {
return endElement(null, name, isEndTagClosed);
}

public XMLBuilder endElement(String name) {
return endElement(null, name);
return endElement(null, name, true);
}

public XMLBuilder endElement(String prefix, String name) {
return endElement(prefix, name, true);
}

public XMLBuilder endElement(String prefix, String name, boolean isEndTagClosed) {
xml.append("</");
if (prefix != null && !prefix.isEmpty()) {
xml.append(prefix);
xml.append(":");
}
xml.append(name);
xml.append(">");
if(isEndTagClosed) {
xml.append(">");
}
return this;
}

Expand All @@ -71,7 +81,7 @@ public XMLBuilder closeStartElement() {
return this;
}

public XMLBuilder endElement() {
public XMLBuilder selfCloseElement() {
if (formattingOptions.isSpaceBeforeEmptyCloseTag()) {
xml.append(" ");
}
Expand Down
Loading

0 comments on commit f9676fe

Please sign in to comment.