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

Add option to sort attributes in HTML output. #2097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/main/java/org/jsoup/nodes/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.IntStream;

import static org.jsoup.internal.Normalizer.lowerCase;
import static org.jsoup.internal.SharedConstants.AttrRangeKey;
Expand Down Expand Up @@ -465,13 +466,29 @@ public String html() {
}

final void html(final Appendable accum, final Document.OutputSettings out) throws IOException {
final int sz = size;
for (int i = 0; i < sz; i++) {
if (isInternalKey(keys[i]))
continue;
final String key = Attribute.getValidKey(keys[i], out.syntax());
if (key != null)
Attribute.htmlNoValidate(key, (String) vals[i], accum.append(' '), out);
if (out.sortAttributes()) {
IntStream.range(0, keys.length)
.limit(size)
.filter(i -> !isInternalKey(keys[i])
&& Attribute.getValidKey(keys[i], out.syntax()) != null)
.mapToObj(Integer::valueOf)
.sorted((a, b) -> keys[a].compareTo(keys[b]))
.forEachOrdered(i -> {
try {
Attribute.htmlNoValidate(keys[i], (String) vals[i], accum.append(' '), out);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} else {
final int sz = size;
for (int i = 0; i < sz; i++) {
if (isInternalKey(keys[i]))
continue;
final String key = Attribute.getValidKey(keys[i], out.syntax());
if (key != null)
Attribute.htmlNoValidate(key, (String) vals[i], accum.append(' '), out);
}
}
}

Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/jsoup/nodes/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
import org.jsoup.helper.DataUtil;
import org.jsoup.helper.Validate;
import org.jsoup.internal.StringUtil;
import org.jsoup.parser.ParseSettings;
import org.jsoup.parser.Parser;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
import org.jsoup.select.Evaluator;
import org.jsoup.select.Selector;
import org.jsoup.parser.*;
import org.jsoup.select.*;
import org.jspecify.annotations.Nullable;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -403,6 +399,7 @@ public enum Syntax {html, xml}

private boolean prettyPrint = true;
private boolean outline = false;
private boolean sortAttributes = false;
private int indentAmount = 1;
private int maxPaddingWidth = 30;
private Syntax syntax = Syntax.html;
Expand Down Expand Up @@ -520,7 +517,25 @@ public OutputSettings prettyPrint(boolean pretty) {
prettyPrint = pretty;
return this;
}


/**
* Get if attribute sorting is enabled. Default is false. If enabled, attributes will be sorted by name
* @return if attribute sorting is enabled.
*/
public boolean sortAttributes() {
return sortAttributes;
}

/**
* Enable or disable attribute sorting.
* @param sort new attribute sorting setting
* @return this, for chaining
*/
public OutputSettings sortAttributes(boolean sort) {
sortAttributes = sort;
return this;
}

/**
* Get if outline mode is enabled. Default is false. If enabled, the HTML output methods will consider
* all tags as block.
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ public void testOuterHtml() {
TextUtil.stripNewlines(doc.outerHtml()));
}

@Test
public void testOuterHtmlWithSortedAttributes() {
Document doc = Jsoup.parse("<p style='margin: 0' id='hello'>Hello</p>");
doc.outputSettings().sortAttributes(true);
assertEquals("<html><head></head><body><p id=\"hello\" style=\"margin: 0\">Hello</p></body></html>",
TextUtil.stripNewlines(doc.outerHtml()));
}

@Test
public void testInnerHtml() {
Document doc = Jsoup.parse("<div>\n <p>Hello</p> </div>");
Expand Down