Skip to content

Commit

Permalink
Remove spacing when formatting processing instruction
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 authored and angelozerr committed May 4, 2020
1 parent f12cf5d commit 2bfc70d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -658,23 +658,22 @@ private static void addPrologToXMLBuilder(DOMNode node, XMLBuilder xml) {
DOMProcessingInstruction processingInstruction = (DOMProcessingInstruction) node;
xml.startPrologOrPI(processingInstruction.getTarget());
if (node.hasAttributes()) {
addAttributes(node, xml);
addPrologAttributes(node, xml);
}
xml.endPrologOrPI();
}

/**
* Will add all attributes, to the given builder, on a single line
*/
private static void addAttributes(DOMNode node, XMLBuilder xmlBuilder) {
private static void addPrologAttributes(DOMNode node, XMLBuilder xmlBuilder) {
List<DOMAttr> attrs = node.getAttributeNodes();
if (attrs == null) {
return;
}
for (DOMAttr attr : attrs) {
xmlBuilder.addAttributesOnSingleLine(attr, true);
xmlBuilder.addSingleAttribute(attr, true);
}
xmlBuilder.appendSpace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public XMLBuilder closeStartElement() {

public XMLBuilder selfCloseElement() {
if (formattingOptions.isSpaceBeforeEmptyCloseTag()) {
xml.append(" ");
appendSpace();
}
xml.append("/>");
return this;
Expand All @@ -107,32 +107,37 @@ public XMLBuilder addSingleAttribute(String name, String value) {
*
* It will not perform any linefeeds and only basic indentation.
*
* @param name attribute name
* @param value attribute value
* @return
* @param name attribute name
* @param value attribute value
* @param surroundWithQuotes true if quotes should be added around originalValue
* @return this XML Builder
*/
public XMLBuilder addSingleAttribute(String name, String value, boolean surroundWithQuotes) {
xml.append(" ");
appendSpace();
addAttributeContents(name, true, value, surroundWithQuotes);

return this;
}

public XMLBuilder addAttributesOnSingleLine(DOMAttr attr, boolean surroundWithQuotes) {
xml.append(" ");
/**
* Used when only one attribute is being added to a node.
*
* It will not perform any linefeeds and only basic indentation.
*
* @param attr attribute
* @param surroundWithQuotes true if value should be added with quotes, false
* otherwise
* @return this XML Builder
*/
public XMLBuilder addSingleAttribute(DOMAttr attr, boolean surroundWithQuotes) {
appendSpace();
addAttributeContents(attr.getName(), attr.hasDelimiter(), attr.getValue(), surroundWithQuotes);

return this;
}

public XMLBuilder addAttribute(String name, String value, int level) {
return addAttribute(name, value, level, false);
}

/**
* Used when you are knowingly adding multiple attributes.
*
* It will do linefeeds and indentation.
* Does linefeeds and indentation.
*
* @param name
* @param value
Expand All @@ -144,7 +149,7 @@ public XMLBuilder addAttribute(String name, String value, int level, boolean sur
linefeed();
indent(level + splitAttributesIndent);
} else {
xml.append(" ");
appendSpace();
}

addAttributeContents(name, true, value, surroundWithQuotes);
Expand All @@ -160,24 +165,25 @@ public XMLBuilder addAttribute(DOMAttr attr, int level, boolean surroundWithQuot
linefeed();
indent(level + splitAttributesIndent);
} else {
xml.append(" ");
appendSpace();
}

addAttributeContents(attr.getName(), attr.hasDelimiter(), attr.getOriginalValue(), surroundWithQuotes);
return this;
}

/**
* * Builds the attribute {name, '=', and value}.
*
* Never puts quotes around unquoted values unless indicated to by
* 'surroundWithQuotes'
*
* @param name name of the attribute
* @param equalsSign true if equals sign exists, false otherwise
* @param originalValue value of the attribute
* @param surroundWithQuotes true if value should be added with quotes, false otherwise
*/
/**
* Builds the attribute {name, '=', and value}.
*
* Never puts quotes around unquoted values unless indicated to by
* 'surroundWithQuotes'
*
* @param name name of the attribute
* @param equalsSign true if equals sign exists, false otherwise
* @param originalValue value of the attribute
* @param surroundWithQuotes true if quotes should be added around originalValue,
* false otherwise
*/
private void addAttributeContents(String name, boolean equalsSign, String originalValue,
boolean surroundWithQuotes) {
if (name != null) {
Expand Down Expand Up @@ -278,7 +284,7 @@ public XMLBuilder indent(int level) {
for (int i = 0; i < level; i++) {
if (isInsertSpaces()) {
for (int j = 0; j < getTabSize(); j++) {
xml.append(" ");
appendSpace();
}
} else {
xml.append("\t");
Expand All @@ -294,9 +300,9 @@ public XMLBuilder startPrologOrPI(String tagName) {
}

public XMLBuilder addContentPI(String content) {
xml.append(" ");
appendSpace();
xml.append(content);
xml.append(" ");
appendSpace();
return this;
}

Expand Down Expand Up @@ -340,17 +346,17 @@ public XMLBuilder endCDATA() {

public XMLBuilder startComment(DOMComment comment) {
if (comment.isCommentSameLineEndTag()) {
xml.append(" ");
appendSpace();
}
xml.append("<!--");
return this;
}

public XMLBuilder addContentComment(String content) {
if (isJoinCommentLines()) {
xml.append(" ");
appendSpace();
xml.append(normalizeSpace(content));
xml.append(" ");
appendSpace();
} else {
xml.append(content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,23 +283,23 @@ public void rangeSelectWithinText() throws BadLocationException {

@Test
public void testProlog() throws BadLocationException {
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" ?>" + lineSeparator();
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + lineSeparator();
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" ?>\r\n";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
format(content, expected);
}

@Test
public void testProlog2() throws BadLocationException {
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" ?><a>bb</a>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + lineSeparator() + //
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSeparator() + //
"<a>bb</a>";
format(content, expected);
}

@Test
public void testProlog3() throws BadLocationException {
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" ?><a><b>c</b></a>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + lineSeparator() + //
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSeparator() + //
"<a>" + lineSeparator() + //
" <b>c</b>" + lineSeparator() + //
"</a>";
Expand All @@ -309,7 +309,7 @@ public void testProlog3() throws BadLocationException {
@Test
public void testProlog4WithUnknownVariable() throws BadLocationException {
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" unknown=\"unknownValue\" ?><a><b>c</b></a>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" unknown=\"unknownValue\" ?>" + lineSeparator() + //
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" unknown=\"unknownValue\"?>" + lineSeparator() + //
"<a>" + lineSeparator() + //
" <b>c</b>" + lineSeparator() + //
"</a>";
Expand Down Expand Up @@ -411,7 +411,7 @@ public void testNestedAttributesNoSplit() throws BadLocationException {
@Test
public void testSplitAttributesProlog() throws BadLocationException {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
Expand Down Expand Up @@ -1442,7 +1442,7 @@ public void testAllDoctypeParameters() throws BadLocationException {
" <servlet-class>dd</servlet-class>\r\n" + //
" </servlet>\r\n" + //
"</web-app>";
String expected = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\r\n" + //
String expected = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n" + //
"<!DOCTYPE web-app PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\" \"http://java.sun.com/dtd/web-app_2_3.dtd\" [\r\n"
+ //
" <!ELEMENT h1 %horiz.model;>\r\n" + //
Expand Down Expand Up @@ -1634,7 +1634,7 @@ public void testXMLInDTDFile() throws BadLocationException {
" <property name=\"propB\" value=\"...\" />\r\n" + //
" </resource>\r\n" + //
"</resources>";
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<resources variant=\"\">\r\n" + //
"<resource name=\"res00\" >\r\n" + //
"<property name=\"propA\" value=\"...\" />\r\n" + //
Expand Down

0 comments on commit 2bfc70d

Please sign in to comment.