Skip to content

Commit c506024

Browse files
authored
Merge pull request #172 from sergeyfedotov/style-fix-parsing
Fix parsing style attribute with trailing spaces
2 parents 3572176 + 2e9860b commit c506024

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

sanitize.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ func (p *Policy) sanitizeStyles(attr html.Attribute, elementName string) html.At
852852
}
853853

854854
//Add semi-colon to end to fix parsing issue
855+
attr.Val = strings.TrimRight(attr.Val, " ")
855856
if len(attr.Val) > 0 && attr.Val[len(attr.Val)-1] != ';' {
856857
attr.Val = attr.Val + ";"
857858
}

sanitize_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,3 +3965,23 @@ func TestIssue161(t *testing.T) {
39653965
expected)
39663966
}
39673967
}
3968+
3969+
func TestIssue171(t *testing.T) {
3970+
// https://github.com/microcosm-cc/bluemonday/issues/171
3971+
//
3972+
// Trailing spaces in the style attribute should not cause the value to be omitted
3973+
p := UGCPolicy()
3974+
p.AllowAttrs("style").OnElements("p")
3975+
p.AllowStyles("color", "text-align").OnElements("p")
3976+
3977+
input := `<p style="color: red; text-align: center; "></p>`
3978+
out := p.Sanitize(input)
3979+
expected := `<p style="color: red; text-align: center"></p>`
3980+
if out != expected {
3981+
t.Errorf(
3982+
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
3983+
input,
3984+
out,
3985+
expected)
3986+
}
3987+
}

0 commit comments

Comments
 (0)