Skip to content

Commit fc539b0

Browse files
authored
Merge pull request #162 from microcosm-cc/buro9/161
Add picture to allowlist of elements that do not need attributes to resolve #161
2 parents 1ee3c12 + ef72ac8 commit fc539b0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

policy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ func (p *Policy) addDefaultElementsWithoutAttrs() {
879879
p.setOfElementsAllowedWithoutAttrs["optgroup"] = struct{}{}
880880
p.setOfElementsAllowedWithoutAttrs["option"] = struct{}{}
881881
p.setOfElementsAllowedWithoutAttrs["p"] = struct{}{}
882+
p.setOfElementsAllowedWithoutAttrs["picture"] = struct{}{}
882883
p.setOfElementsAllowedWithoutAttrs["pre"] = struct{}{}
883884
p.setOfElementsAllowedWithoutAttrs["q"] = struct{}{}
884885
p.setOfElementsAllowedWithoutAttrs["rp"] = struct{}{}

sanitize_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3931,3 +3931,37 @@ func TestRemovingEmptySelfClosingTag(t *testing.T) {
39313931
expected)
39323932
}
39333933
}
3934+
3935+
func TestIssue161(t *testing.T) {
3936+
// https://github.com/microcosm-cc/bluemonday/issues/161
3937+
//
3938+
// ```
3939+
// p.AllowElementsMatching(regexp.MustCompile(`^custom-`))
3940+
// p.AllowNoAttrs().Matching(regexp.MustCompile(`^custom-`))
3941+
// ```
3942+
// This does not work as expected. This looks like a limitation, and the
3943+
// question is whether the matching has to be applied in a second location
3944+
// to overcome the limitation.
3945+
//
3946+
// However the issue is really that the `.Matching()` returns an attribute
3947+
// test that has to be bound to some elements, it isn't a global test.
3948+
//
3949+
// This should work:
3950+
// ```
3951+
// p.AllowNoAttrs().Matching(regexp.MustCompile(`^custom-`)).OnElementsMatching(regexp.MustCompile(`^custom-`))
3952+
// ```
3953+
p := UGCPolicy()
3954+
p.AllowElements("picture", "source")
3955+
p.AllowAttrs("srcset", "src", "type", "media").OnElements("source")
3956+
3957+
input := `<picture><source src="b.jpg" media="(prefers-color-scheme: dark)"></source><img src="a.jpg"></picture>`
3958+
out := p.Sanitize(input)
3959+
expected := input
3960+
if out != expected {
3961+
t.Errorf(
3962+
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
3963+
input,
3964+
out,
3965+
expected)
3966+
}
3967+
}

0 commit comments

Comments
 (0)