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

Allow attributes valid in html when converting #1648

Merged
merged 4 commits into from Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions src/main/java/org/jsoup/helper/W3CDom.java
Expand Up @@ -38,10 +38,12 @@
import java.util.Map;
import java.util.Properties;
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.Locale;

import static javax.xml.transform.OutputKeys.METHOD;
import static org.jsoup.nodes.Document.OutputSettings.Syntax.html;
import static org.jsoup.nodes.Document.OutputSettings.Syntax.xml;
import static org.jsoup.nodes.Document.OutputSettings.Syntax;

/**
* Helper class to transform a {@link org.jsoup.nodes.Document} to a {@link org.w3c.dom.Document org.w3c.dom.Document},
Expand Down Expand Up @@ -342,8 +344,15 @@ public void tail(org.jsoup.nodes.Node source, int depth) {
}

private void copyAttributes(org.jsoup.nodes.Node source, Element el) {
final Syntax syntax;
jhy marked this conversation as resolved.
Show resolved Hide resolved
if (this.doc.getDoctype() != null &&
this.doc.getDoctype().getName().toLowerCase(Locale.ROOT).equals("html")) {
syntax = html;
} else {
syntax = xml;
}
for (Attribute attribute : source.attributes()) {
String key = Attribute.getValidKey(attribute.getKey(), xml);
String key = Attribute.getValidKey(attribute.getKey(), syntax);
if (key != null) { // null if couldn't be coerced to validity
el.setAttribute(key, attribute.getValue());
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/jsoup/helper/W3CDomTest.java
Expand Up @@ -190,6 +190,20 @@ public void handlesInvalidAttributeNames() {
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html><head/><body name=\"\" style=\"color: red\"/></html>", xml);
}

@Test
public void handlesAccentedCharsAttributeNames() {
String html = "<!DOCTYPE html><html><head></head><body style=\"color: red\" \" name\"><p hành=\"1\" hình=\"2\">unicode attr names</p></body></html>";
org.jsoup.nodes.Document jsoupDoc;
jsoupDoc = Jsoup.parse(html);
Element body = jsoupDoc.select("body").first();
assertTrue(body.hasAttr("\"")); // actually an attribute with key '"'. Correct per HTML5 spec, but w3c xml dom doesn't dig it
assertTrue(body.hasAttr("name\""));

Document w3Doc = W3CDom.convert(jsoupDoc);
String out = W3CDom.asString(w3Doc, W3CDom.OutputHtml());
assertEquals("<!DOCTYPE html SYSTEM \"about:legacy-compat\">\n<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body name=\"\" style=\"color: red\"><p hành=\"1\" hình=\"2\">unicode attr names</p></body></html>", out);
}

@Test
public void handlesInvalidTagAsText() {
org.jsoup.nodes.Document jsoup = Jsoup.parse("<インセンティブで高収入!>Text <p>More</p>");
Expand Down