Skip to content

Commit 2b3f0aa

Browse files
committed
issue 40: <span> that are dropped because they lack attributes lead to the wrong </span> tag being dropped
1 parent 2fec8b3 commit 2b3f0aa

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/main/java/org/owasp/html/ElementAndAttributePolicyBasedSanitizerPolicy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void writeOpenTag(
182182
}
183183

184184
void deferOpenTag(String elementName) {
185-
if (HtmlTextEscapingMode.isVoidElement(elementName)) {
185+
if (!HtmlTextEscapingMode.isVoidElement(elementName)) {
186186
openElementStack.add(elementName);
187187
openElementStack.add(null);
188188
}

src/test/java/org/owasp/html/HtmlSanitizerTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ public static final void testScriptInIframe() {
341341
+ "</iframe>"));
342342
}
343343

344+
@Test
345+
public static final void testBalancingOfEmptyTags() {
346+
assertEquals(
347+
"<span style=\"color:rgb( 72 , 72 , 72 );font-family:&#39;helveticaneue&#39;\">"
348+
+ " "
349+
+ "my \u00A0"
350+
+ " list of style names or a "
351+
+ "</span>",
352+
sanitize(
353+
"<span style=\"color:rgb(72, 72, 72); font-family:helveticaneue\">"
354+
+ " "
355+
+ "<span>my &nbsp;</span>"
356+
+ " list of style names or a "
357+
+ "</span>"));
358+
}
359+
344360
private static String sanitize(@Nullable String html) {
345361
StringBuilder sb = new StringBuilder();
346362
HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
@@ -373,6 +389,7 @@ public String apply(
373389
}
374390
})
375391
.globally()
392+
.allowStyling()
376393
// Don't throw out useless <img> and <input> elements to ease debugging.
377394
.allowWithoutAttributes("img", "input")
378395
.build(renderer);

0 commit comments

Comments
 (0)