Skip to content

Commit

Permalink
Merge pull request #172 from sergeyfedotov/style-fix-parsing
Browse files Browse the repository at this point in the history
Fix parsing style attribute with trailing spaces
  • Loading branch information
grafana-dee authored May 9, 2023
2 parents 3572176 + 2e9860b commit c506024
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ func (p *Policy) sanitizeStyles(attr html.Attribute, elementName string) html.At
}

//Add semi-colon to end to fix parsing issue
attr.Val = strings.TrimRight(attr.Val, " ")
if len(attr.Val) > 0 && attr.Val[len(attr.Val)-1] != ';' {
attr.Val = attr.Val + ";"
}
Expand Down
20 changes: 20 additions & 0 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3965,3 +3965,23 @@ func TestIssue161(t *testing.T) {
expected)
}
}

func TestIssue171(t *testing.T) {
// https://github.com/microcosm-cc/bluemonday/issues/171
//
// Trailing spaces in the style attribute should not cause the value to be omitted
p := UGCPolicy()
p.AllowAttrs("style").OnElements("p")
p.AllowStyles("color", "text-align").OnElements("p")

input := `<p style="color: red; text-align: center; "></p>`
out := p.Sanitize(input)
expected := `<p style="color: red; text-align: center"></p>`
if out != expected {
t.Errorf(
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
input,
out,
expected)
}
}

0 comments on commit c506024

Please sign in to comment.