From fce241b57f1e0292b143a11d31b8abf9ad949398 Mon Sep 17 00:00:00 2001 From: Jonathan Hedley Date: Wed, 14 Jul 2021 11:35:39 +1000 Subject: [PATCH] Skip empty attribute values when serializing XML processing instructions Fixes #770 --- CHANGES | 3 +++ src/main/java/org/jsoup/nodes/XmlDeclaration.java | 12 ++++++++++-- .../java/org/jsoup/parser/XmlTreeBuilderTest.java | 10 ++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index d60b728bb6..c07a143b4e 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,9 @@ jsoup changelog * Bugfix: when making a HTTP POST, if the request write fails, make sure the connection is immediately cleaned up. + * Bugfix: in the XML parser, XML processing instructions without attributes would be serialized as if they did. + + * Bugfix: updated the HtmlTreeParser resetInsertionMode to the current spec for supported elements diff --git a/src/main/java/org/jsoup/nodes/XmlDeclaration.java b/src/main/java/org/jsoup/nodes/XmlDeclaration.java index d7906eb6a6..6ab9b16241 100644 --- a/src/main/java/org/jsoup/nodes/XmlDeclaration.java +++ b/src/main/java/org/jsoup/nodes/XmlDeclaration.java @@ -52,9 +52,17 @@ public String getWholeDeclaration() { private void getWholeDeclaration(Appendable accum, Document.OutputSettings out) throws IOException { for (Attribute attribute : attributes()) { - if (!attribute.getKey().equals(nodeName())) { // skips coreValue (name) + String key = attribute.getKey(); + String val = attribute.getValue(); + if (!key.equals(nodeName())) { // skips coreValue (name) accum.append(' '); - attribute.html(accum, out); + // basically like Attribute, but skip empty vals in XML + accum.append(key); + if (!val.isEmpty()) { + accum.append("=\""); + Entities.escape(accum, val, out, true, false, false); + accum.append('"'); + } } } } diff --git a/src/test/java/org/jsoup/parser/XmlTreeBuilderTest.java b/src/test/java/org/jsoup/parser/XmlTreeBuilderTest.java index d2527a9083..8fb0fbd8a8 100644 --- a/src/test/java/org/jsoup/parser/XmlTreeBuilderTest.java +++ b/src/test/java/org/jsoup/parser/XmlTreeBuilderTest.java @@ -152,6 +152,16 @@ public void testParseDeclarationAttributes() { assertEquals("", decl.outerHtml()); } + @Test + public void testParseDeclarationWithoutAttributes() { + String xml = "\n"; + Document doc = Jsoup.parse(xml, "", Parser.xmlParser()); + XmlDeclaration decl = (XmlDeclaration) doc.childNode(2); + assertEquals("myProcessingInstruction", decl.name()); + assertTrue(decl.hasAttr("My")); + assertEquals("", decl.outerHtml()); + } + @Test public void caseSensitiveDeclaration() { String xml = "";