Skip to content

Commit a496a53

Browse files
committed
feat: single-quoted attribute value syntax support
1 parent af681ae commit a496a53

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

README.en.md

+11
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ func SafeAttrValue(tag, name, value string) string {
189189
}
190190
```
191191

192+
### Customize output attribute value syntax for HTML
193+
By specifying a `SingleQuotedAttributeValue`. Use `true` for `'`. Otherwise default `"` will be used
194+
```golang
195+
options.SingleQuotedAttributeValue = true
196+
197+
// With the configuration specified above, the following HTML:
198+
// <a href="#">Hello</a>
199+
// would become:
200+
// <a href='#'>Hello</a>
201+
```
202+
192203

193204

194205
### Quick Start

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ func SafeAttrValue(tag, name, value string) string {
185185
}
186186
```
187187

188+
### 自定义属性值引用为单引号
189+
190+
`SingleQuotedAttributeValue`为false时默认为双引号,为true时则为单引号
191+
192+
```golang
193+
options.SingleQuotedAttributeValue = true
194+
195+
// 设置属性为true后,以下HTML
196+
// <a href="#">Hello</a>
197+
// 输出结果:
198+
// <a href='#'>Hello</a>
199+
```
200+
201+
188202
### 自定义 CSS 过滤器
189203

190204
TODO

xss.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package xss
33
import (
44
// "errors"
55
// "bytes"
6-
"github.com/feiin/pkg/arrays"
6+
"fmt"
77
"strings"
8+
9+
"github.com/feiin/pkg/arrays"
810
// "io"
911
)
1012

@@ -102,6 +104,11 @@ func (x *Xss) Process(html string) string {
102104
OnIgnoreTagAttr := x.options.OnIgnoreTagAttr
103105
whiteList := x.options.WhiteList
104106

107+
attributeWrapSign := "\""
108+
if x.options.SingleQuotedAttributeValue {
109+
attributeWrapSign = "'"
110+
}
111+
105112
//remove invisible characters
106113
if x.options.StripBlankChar {
107114
html = stripBlankChar(html)
@@ -164,7 +171,7 @@ func (x *Xss) Process(html string) string {
164171
if isWhiteAttr {
165172
value = safeAttrValue(tag, name, value)
166173
if len(value) > 0 {
167-
return name + "=\"" + value + "\""
174+
return fmt.Sprintf("%s=%s%s%s", name, attributeWrapSign, value, attributeWrapSign) // name + "=\"" + value + "\""
168175
} else {
169176
return name
170177
}

xss_option.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ type XssOption struct {
1414
// remove html comments
1515
AllowCommentTag bool
1616

17-
StripIgnoreTag bool
17+
StripIgnoreTag bool
18+
SingleQuotedAttributeValue bool
1819

1920
// StripIgnoreTagBody
2021
StripIgnoreTagBody []string

xss_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1506,3 +1506,31 @@ func TestOnTagSanitizeHtml(t *testing.T) {
15061506
t.Errorf("TestStripIngoreBodyTag4 error %s", html)
15071507
}
15081508
}
1509+
1510+
func TestSingleQuotedAttributeValue(t *testing.T) {
1511+
source := "<a title=\"xx\">single-quoted</a>"
1512+
1513+
html := FilterXSS(source, XssOption{SingleQuotedAttributeValue: false})
1514+
1515+
if html != "<a title=\"xx\">single-quoted</a>" {
1516+
t.Errorf("TestSingleQuotedAttributeValue expect: %s but:%s", source, html)
1517+
1518+
}
1519+
1520+
html = FilterXSS(source, XssOption{SingleQuotedAttributeValue: true})
1521+
1522+
expect := "<a title='xx'>single-quoted</a>"
1523+
if html != expect {
1524+
t.Errorf("TestSingleQuotedAttributeValue expect:%s but:%s", expect, html)
1525+
1526+
}
1527+
1528+
source = "<a title='xx'>single-quoted</a>"
1529+
1530+
html = FilterXSS(source, XssOption{SingleQuotedAttributeValue: false})
1531+
expect = "<a title=\"xx\">single-quoted</a>"
1532+
if html != expect {
1533+
t.Errorf("TestSingleQuotedAttributeValue expect: %s but:%s", expect, html)
1534+
1535+
}
1536+
}

0 commit comments

Comments
 (0)