Skip to content

Commit e1adf0f

Browse files
SethFalcoisaacs
authored andcommitted
feat: add explicit option for unquoted attribute values
PR-URL: #268 Credit: @SethFalco Close: #268 Reviewed-by: @isaacs
1 parent 8e8fa71 commit e1adf0f

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/sax.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
parser.ns = Object.create(rootNS)
7272
}
7373

74+
// disallow unquoted attribute values if not otherwise configured
75+
// and strict mode is true
76+
if (parser.opt.unquotedAttributeValues === undefined) {
77+
parser.opt.unquotedAttributeValues = !strict;
78+
}
79+
7480
// mostly just for error reporting
7581
parser.trackPosition = parser.opt.position !== false
7682
if (parser.trackPosition) {
@@ -1379,7 +1385,9 @@
13791385
parser.q = c
13801386
parser.state = S.ATTRIB_VALUE_QUOTED
13811387
} else {
1382-
strictFail(parser, 'Unquoted attribute value')
1388+
if (!parser.opt.unquotedAttributeValues) {
1389+
error(parser, 'Unquoted attribute value')
1390+
}
13831391
parser.state = S.ATTRIB_VALUE_UNQUOTED
13841392
parser.attribValue = c
13851393
}

test/unquoted-attribute-values.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// strict: true
2+
require(__dirname).test({
3+
xml: '<svg width=20px height=20px />',
4+
expect: [
5+
['opentagstart', { name: 'svg', attributes: {} }],
6+
['error', 'Unquoted attribute value\nLine: 0\nColumn: 12\nChar: 2'],
7+
['attribute', { name: 'width', value: '20px' }],
8+
['error', 'Unquoted attribute value\nLine: 0\nColumn: 24\nChar: 2'],
9+
['attribute', { name: 'height', value: '20px' }],
10+
['opentag', { name: 'svg', attributes: {
11+
width: '20px',
12+
height: '20px'
13+
}, isSelfClosing: true }],
14+
['closetag', 'svg'],
15+
],
16+
strict: true
17+
})
18+
19+
// strict: false
20+
require(__dirname).test({
21+
xml: '<svg width=20px height=20px />',
22+
expect: [
23+
['opentagstart', { name: 'SVG', attributes: {} }],
24+
['attribute', { name: 'WIDTH', value: '20px' }],
25+
['attribute', { name: 'HEIGHT', value: '20px' }],
26+
['opentag', { name: 'SVG', attributes: {
27+
WIDTH: '20px',
28+
HEIGHT: '20px'
29+
}, isSelfClosing: true }],
30+
['closetag', 'SVG'],
31+
],
32+
strict: false
33+
})
34+
35+
// strict: true, opt: { unquotedAttributeValues: true }
36+
require(__dirname).test({
37+
xml: '<svg width=20px height=20px />',
38+
expect: [
39+
['opentagstart', { name: 'svg', attributes: {} }],
40+
['attribute', { name: 'width', value: '20px' }],
41+
['attribute', { name: 'height', value: '20px' }],
42+
['opentag', { name: 'svg', attributes: {
43+
width: '20px',
44+
height: '20px'
45+
}, isSelfClosing: true }],
46+
['closetag', 'svg'],
47+
],
48+
strict: true,
49+
opt: {
50+
unquotedAttributeValues: true
51+
}
52+
})
53+
54+
// strict: false, opt: { unquotedAttributeValues: true }
55+
require(__dirname).test({
56+
xml: '<svg width=20px height=20px />',
57+
expect: [
58+
['opentagstart', { name: 'SVG', attributes: {} }],
59+
['attribute', { name: 'WIDTH', value: '20px' }],
60+
['attribute', { name: 'HEIGHT', value: '20px' }],
61+
['opentag', { name: 'SVG', attributes: {
62+
WIDTH: '20px',
63+
HEIGHT: '20px'
64+
}, isSelfClosing: true }],
65+
['closetag', 'SVG'],
66+
],
67+
strict: false,
68+
opt: {
69+
unquotedAttributeValues: true
70+
}
71+
})

0 commit comments

Comments
 (0)