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

Remove spacing when formatting processing instruction #670

Merged
merged 1 commit into from
May 4, 2020
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 @@ -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