diff --git a/CHANGES.md b/CHANGES.md index f67d2ce5f5..6acebf64d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -58,6 +58,8 @@ special tags. [2258](https://github.com/jhy/jsoup/issues/2258) * An `:nth-child` selector with a negative digit-less step, such as `:nth-child(-n+2)`, would be parsed incorrectly as a positive step, and so would not match as expected. [1147](https://github.com/jhy/jsoup/issues/1147) +* Calling `doc.charset(charset)` on an empty XML document would throw an + `IndexOutOfBoundsException`. [2266](https://github.com/jhy/jsoup/issues/2266) ## 1.18.3 (2024-Dec-02) diff --git a/src/main/java/org/jsoup/nodes/Document.java b/src/main/java/org/jsoup/nodes/Document.java index 5d46e8bd3e..0d077af445 100644 --- a/src/main/java/org/jsoup/nodes/Document.java +++ b/src/main/java/org/jsoup/nodes/Document.java @@ -53,7 +53,7 @@ public Document(String baseUri) { } /** - Create a valid, empty shell of a document, suitable for adding more elements to. + Create a valid, empty shell of an HTML document, suitable for adding more elements to. @param baseUri baseUri of document @return document with html, head, and body elements. */ @@ -326,7 +326,7 @@ private void ensureMetaCharsetElement() { } private XmlDeclaration ensureXmlDecl() { - Node node = ensureChildNodes().get(0); + Node node = firstChild(); if (node instanceof XmlDeclaration) { XmlDeclaration decl = (XmlDeclaration) node; if (decl.name().equals("xml")) return decl; diff --git a/src/test/java/org/jsoup/nodes/DocumentTest.java b/src/test/java/org/jsoup/nodes/DocumentTest.java index c7e28f47d0..1d6456a941 100644 --- a/src/test/java/org/jsoup/nodes/DocumentTest.java +++ b/src/test/java/org/jsoup/nodes/DocumentTest.java @@ -434,6 +434,17 @@ private Document createXmlDocument(String version, String charset, boolean addDe return doc; } + @Test void charsetOnEmptyDoc() { + Document xml = new Document(Parser.NamespaceXml, "https://example.com"); // no nodes + xml.outputSettings().syntax(Syntax.xml); + xml.charset(StandardCharsets.UTF_8); + assertEquals("", xml.html()); + + Document html = new Document("https://example.com"); + html.charset(StandardCharsets.UTF_8); + assertEquals("", TextUtil.stripNewlines(html.html())); + } + @Test public void testShiftJisRoundtrip() throws Exception { String input =