Skip to content

Commit e44b673

Browse files
committed
Unbreak CssSchema.withProperties(Map)
This method never worked before. Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 parent 91c5fdc commit e44b673

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,16 @@ public static CssSchema withProperties(
164164
Map<String, Property> propertyMap =
165165
new HashMap<>();
166166
// check that all fnKeys are defined in properties.
167-
for (Map.Entry<String, Property> e : propertyMap.entrySet()) {
167+
for (Map.Entry<? extends String, ? extends Property> e : properties.entrySet()) {
168168
Property property = e.getValue();
169169
for (String fnKey : property.fnKeys.values()) {
170-
if (!propertyMap.containsKey(fnKey)) {
170+
if (!properties.containsKey(fnKey)) {
171171
throw new IllegalArgumentException(
172172
"Property map is not self contained. " + e.getValue()
173173
+ " depends on undefined function key " + fnKey);
174174
}
175175
}
176+
propertyMap.put(e.getKey(), e.getValue());
176177
}
177178
return new CssSchema(Map.copyOf(propertyMap));
178179
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
package org.owasp.html;
3030

3131
import java.util.Arrays;
32+
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Locale;
35+
import java.util.Map;
3436
import java.util.Set;
3537
import java.util.regex.Pattern;
3638
import java.util.stream.Collectors;
@@ -229,6 +231,54 @@ public static final void testStyleFiltering() {
229231
.allowStandardUrlProtocols()));
230232
}
231233

234+
@Test
235+
public void testCustomPropertyStyleFilterung() {
236+
assertEquals(
237+
Arrays.stream(new String[] {
238+
"<h1>Header</h1>",
239+
"<p>Paragraph 1</p>",
240+
"<p>Click me out</p>",
241+
"<p></p>",
242+
"<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>.",
243+
"</p><p style=\"text-align:center\">Stylish Para 1</p>",
244+
"<p>Stylish Para 2</p>",
245+
""}).collect(Collectors.joining("\n")),
246+
apply(new HtmlPolicyBuilder()
247+
.allowCommonInlineFormattingElements()
248+
.allowCommonBlockElements()
249+
.allowStyling(
250+
CssSchema.withProperties(
251+
Map.of("text-align",
252+
new CssSchema.Property(0,
253+
Set.of("center"),
254+
Collections.emptyMap()))))
255+
.allowStandardUrlProtocols()));
256+
}
257+
258+
@Test
259+
public void testCustomPropertyStyleFilterungDisallowed() {
260+
assertEquals(
261+
Arrays.stream(new String[] {
262+
"<h1>Header</h1>",
263+
"<p>Paragraph 1</p>",
264+
"<p>Click me out</p>",
265+
"<p></p>",
266+
"<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>.",
267+
"</p><p>Stylish Para 1</p>",
268+
"<p>Stylish Para 2</p>",
269+
""}).collect(Collectors.joining("\n")),
270+
apply(new HtmlPolicyBuilder()
271+
.allowCommonInlineFormattingElements()
272+
.allowCommonBlockElements()
273+
.allowStyling(
274+
CssSchema.withProperties(
275+
Map.of("text-align",
276+
new CssSchema.Property(0,
277+
Set.of("left", "right"),
278+
Collections.emptyMap()))))
279+
.allowStandardUrlProtocols()));
280+
}
281+
232282
@Test
233283
public static final void testElementTransforming() {
234284
assertEquals(

0 commit comments

Comments
 (0)