Skip to content

Commit

Permalink
feat: add explicit option for unquoted attribute values
Browse files Browse the repository at this point in the history
PR-URL: #268
Credit: @SethFalco
Close: #268
Reviewed-by: @isaacs
  • Loading branch information
SethFalco authored and isaacs committed May 27, 2024
1 parent 8e8fa71 commit 30aab25
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ Settings supported:
* `strictEntities` - Boolean. If true, only parse [predefined XML
entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent)
(`&`, `'`, `>`, `<`, and `"`)
* `unquotedAttributeValues` - Boolean. If true, then unquoted
attribute values are allowed. Defaults to `false` when `strict`
is true, `true` otherwise.

## Methods

Expand Down
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 @@ -1379,7 +1385,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 30aab25

Please sign in to comment.