Skip to content

Commit b236a85

Browse files
committed
Make <br> inline not block
Fixes #2439 Related to #2387
1 parent 3ec4a5d commit b236a85

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* When using StructuralEvaluators (e.g., a `parent child` selector) across many retained threads, their memoized results could also be retained, increasing memory use. These results are now cleared immediately after use, reducing overall memory consumption. [#2411](https://github.com/jhy/jsoup/issues/2411)
2929
* Cloning a `Parser` now preserves any custom `TagSet` applied to the parser. [#2422](https://github.com/jhy/jsoup/issues/2422), [#2423](https://github.com/jhy/jsoup/pull/2423)
3030
* Custom tags marked as `Tag.Void` now parse and serialize like the built-in void elements: they no longer consume following content, and the XML serializer emits the expected self-closing form. [#2425](https://github.com/jhy/jsoup/issues/2425)
31+
* The `<br>` element is once again classified as an inline tag (`Tag.isBlock() == false`), matching common developer expectations and its role as phrasing content in HTML, while pretty-printing and text extraction continue to treat it as a line break in the rendered output. [#2387](https://github.com/jhy/jsoup/issues/2387), [#2439](https://github.com/jhy/jsoup/issues/2439)
3132

3233
### Internal Changes
3334
* Deprecated internal helper `org.jsoup.internal.Functions` (for removal in v1.23.1). This was previously used to support older Android API levels without full `java.util.function` coverage; jsoup now requires core library desugaring so this indirection is no longer necessary. [#2412](https://github.com/jhy/jsoup/pull/2412)

src/main/java/org/jsoup/nodes/Printer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ boolean isBlockEl(@Nullable Node node) {
159159
if (node == null) return false;
160160
if (node instanceof Element) {
161161
Element el = (Element) node;
162+
if (el.nameIs("br")) return true; // give <br> a newline; actually an inline tag
162163
return el.isBlock() ||
163164
(!el.tag.isKnownTag() && (el.parentNode instanceof Document || hasChildBlocks(el)));
164165
}

src/main/java/org/jsoup/parser/TagSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static TagSet initHtmlDefault() {
212212
String[] blockTags = {
213213
"html", "head", "body", "frameset", "script", "noscript", "style", "meta", "link", "title", "frame",
214214
"noframes", "section", "nav", "aside", "hgroup", "header", "footer", "p", "h1", "h2", "h3", "h4", "h5",
215-
"h6", "br", "button",
215+
"h6", "button",
216216
"ul", "ol", "pre", "div", "blockquote", "hr", "address", "figure", "figcaption", "form", "fieldset", "ins",
217217
"del", "dl", "dt", "dd", "li", "table", "caption", "thead", "tfoot", "tbody", "colgroup", "col", "tr", "th",
218218
"td", "video", "audio", "canvas", "details", "menu", "plaintext", "template", "article", "main",

src/test/java/org/jsoup/parser/TagTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public void canBeInsensitive(Locale locale) {
8181
assertFalse(p.isInline());
8282
}
8383

84+
@Test public void brSemantics() {
85+
Tag br = Tag.valueOf("br");
86+
assertTrue(br.isInline());
87+
assertFalse(br.isBlock());
88+
}
89+
8490
@Test public void imgSemantics() {
8591
Tag img = Tag.valueOf("img");
8692
assertTrue(img.isInline());

0 commit comments

Comments
 (0)