Skip to content

Commit

Permalink
feat: add explicit option for unquoted attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco committed Jan 13, 2024
1 parent b577600 commit feb19c6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/sax.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
parser.ns = Object.create(rootNS)
}

// disallow unquoted attribute values if not otherwise configured
// and strict mode is true
if (parser.opt.unquotedAttributeValues === undefined) {
parser.opt.unquotedAttributeValues = !strict;
}

// mostly just for error reporting
parser.trackPosition = parser.opt.position !== false
if (parser.trackPosition) {
Expand Down Expand Up @@ -1365,7 +1371,9 @@
parser.q = c
parser.state = S.ATTRIB_VALUE_QUOTED
} else {
strictFail(parser, 'Unquoted attribute value')
if (!parser.opt.unquotedAttributeValues) {
error(parser, 'Unquoted attribute value')
}
parser.state = S.ATTRIB_VALUE_UNQUOTED
parser.attribValue = c
}
Expand Down
71 changes: 71 additions & 0 deletions test/unquoted-attribute-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// strict: true
require(__dirname).test({
xml: '<svg width=20px height=20px />',
expect: [
['opentagstart', { name: 'svg', attributes: {} }],
['error', 'Unquoted attribute value\nLine: 0\nColumn: 12\nChar: 2'],
['attribute', { name: 'width', value: '20px' }],
['error', 'Unquoted attribute value\nLine: 0\nColumn: 24\nChar: 2'],
['attribute', { name: 'height', value: '20px' }],
['opentag', { name: 'svg', attributes: {
width: '20px',
height: '20px'
}, isSelfClosing: true }],
['closetag', 'svg'],
],
strict: true
})

// strict: false
require(__dirname).test({
xml: '<svg width=20px height=20px />',
expect: [
['opentagstart', { name: 'SVG', attributes: {} }],
['attribute', { name: 'WIDTH', value: '20px' }],
['attribute', { name: 'HEIGHT', value: '20px' }],
['opentag', { name: 'SVG', attributes: {
WIDTH: '20px',
HEIGHT: '20px'
}, isSelfClosing: true }],
['closetag', 'SVG'],
],
strict: false
})

// strict: true, opt: { unquotedAttributeValues: true }
require(__dirname).test({
xml: '<svg width=20px height=20px />',
expect: [
['opentagstart', { name: 'svg', attributes: {} }],
['attribute', { name: 'width', value: '20px' }],
['attribute', { name: 'height', value: '20px' }],
['opentag', { name: 'svg', attributes: {
width: '20px',
height: '20px'
}, isSelfClosing: true }],
['closetag', 'svg'],
],
strict: true,
opt: {
unquotedAttributeValues: true
}
})

// strict: false, opt: { unquotedAttributeValues: true }
require(__dirname).test({
xml: '<svg width=20px height=20px />',
expect: [
['opentagstart', { name: 'SVG', attributes: {} }],
['attribute', { name: 'WIDTH', value: '20px' }],
['attribute', { name: 'HEIGHT', value: '20px' }],
['opentag', { name: 'SVG', attributes: {
WIDTH: '20px',
HEIGHT: '20px'
}, isSelfClosing: true }],
['closetag', 'SVG'],
],
strict: false,
opt: {
unquotedAttributeValues: true
}
})

0 comments on commit feb19c6

Please sign in to comment.