Skip to content

Commit

Permalink
Fix line break being incorrectly added when preserveAttrLineBreaks is
Browse files Browse the repository at this point in the history
enabled

Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and angelozerr committed Jun 17, 2020
1 parent 59c04ef commit 7973e91
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,10 @@ private void formatElementStartTagSelfCloseBracket(DOMElement element) throws Ba
private void formatAttributes(DOMElement element) throws BadLocationException {
List<DOMAttr> attributes = element.getAttributeNodes();
boolean isSingleElement = hasSingleAttributeInFullDoc(element);
DOMNode prev = element;
int prevOffset = element.getStart();
for (DOMAttr attr : attributes) {
if (this.sharedSettings.getFormattingSettings().isPreserveAttrLineBreaks()
&& !isSameLine(prev.getStart(), attr.getNodeAttrName().getStart())) {
&& !isSameLine(prevOffset, attr.getStart())) {
xmlBuilder.linefeed();
xmlBuilder.indent(this.indentLevel + 1);
xmlBuilder.addSingleAttribute(attr, false, false);
Expand All @@ -559,7 +559,7 @@ private void formatAttributes(DOMElement element) throws BadLocationException {
} else {
xmlBuilder.addAttribute(attr, this.indentLevel);
}
prev = attr.getNodeAttrName();
prevOffset = attr.getEnd();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,31 @@ public void preserveAttributeLineBreaks4() throws BadLocationException {
format(content, expected, settings);
}

@Test
public void preserveAttributeLineBreaks5() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setPreserveAttrLineBreaks(true);
String content = "<a attr=\"value\" attr=\"value\"\n" + //
" attr=\n" + //
" \"value\" attr=\"value\"></a>";
String expected = "<a attr=\"value\" attr=\"value\"\n" + //
" attr=\"value\" attr=\"value\"></a>";
format(content, expected, settings);
}

@Test
public void preserveAttributeLineBreaksMissingValue() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setPreserveAttrLineBreaks(true);
String content = "<a attr= attr=\"value\"\n" + //
" attr=\n" + //
" attr=\"value\"></a>";
String expected = "<a attr= attr=\"value\"\n" + //
" attr=\n" + //
" attr=\"value\"></a>";
format(content, expected, settings);
}

@Test
public void preserveAttributeLineBreaksCollapseEmptyElement() throws BadLocationException {
SharedSettings settings = new SharedSettings();
Expand Down

0 comments on commit 7973e91

Please sign in to comment.