Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add explicit option for unquoted attribute values #268

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
})
Loading