Skip to content

Commit

Permalink
chore(parser): don't return true on empty attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikku committed Nov 10, 2017
1 parent 8aabf9b commit f7360b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
9 changes: 1 addition & 8 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function Saxen(options) {
* Parse attributes on demand and returns the parsed attributes.
*
* Return semantics: (1) `false` on attribute parse error,
* (2) true on no attributes, (3) object hash on extracted attrs.
* (2) object hash on extracted attrs.
*
* @return {boolean|Object}
*/
Expand All @@ -347,7 +347,6 @@ function Saxen(options) {
alias,
name,
attrs = {},
ok,
w,
j;

Expand Down Expand Up @@ -385,7 +384,6 @@ function Saxen(options) {
}

name = s.substring(i, j);
ok = true;

if (name === 'xmlns:xmlns') {
handleWarning('illegal declaration of xmlns');
Expand Down Expand Up @@ -531,11 +529,6 @@ function Saxen(options) {
}


if (!ok) {
// could not parse attributes, skipping
return cachedAttrs = true;
}

// handle deferred, possibly namespaced attributes
if (maybeNS) {

Expand Down
40 changes: 20 additions & 20 deletions test/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,47 @@ var test = require('./test');
test({
xml: '<div/>',
expect: [
['openTag', 'div', true, true],
['openTag', 'div', {}, true],
['closeTag', 'div', true],
],
});

test({
xml: '<div />',
expect: [
['openTag', 'div', true, true],
['openTag', 'div', {}, true],
['closeTag', 'div', true],
],
});

test({
xml: '<div></div>',
expect: [
['openTag', 'div', true, false ],
['openTag', 'div', {}, false ],
['closeTag', 'div', false ],
]
});

test({
xml: '<DIV/>',
expect: [
['openTag', 'DIV', true, true],
['openTag', 'DIV', {}, true],
['closeTag', 'DIV', true],
],
});

test({
xml: '<DIV />',
expect: [
['openTag', 'DIV', true, true],
['openTag', 'DIV', {}, true],
['closeTag', 'DIV', true],
],
});

test({
xml: '<DIV></DIV>',
expect: [
['openTag', 'DIV', true, false ],
['openTag', 'DIV', {}, false ],
['closeTag', 'DIV', false ],
]
});
Expand Down Expand Up @@ -128,8 +128,8 @@ test({
test({
xml: '<a><b/></a>',
expect: [
['openTag', 'a', true, false],
['openTag', 'b', true, true],
['openTag', 'a', {}, false],
['openTag', 'b', {}, true],
['closeTag', 'b', true],
['closeTag', 'a', false],
],
Expand Down Expand Up @@ -166,17 +166,17 @@ test({
test({
xml: '<a><b></c></b></a>',
expect: [
['openTag', 'a', true, false],
['openTag', 'b', true, false],
['openTag', 'a', {}, false],
['openTag', 'b', {}, false],
['error', 'closing tag mismatch', { data: '</c>', line: 0, column: 6 } ],
],
});

test({
xml: '<_a><:b></:b></_a>',
expect: [
['openTag', '_a', true, false],
['openTag', ':b', true, false],
['openTag', '_a', {}, false],
['openTag', ':b', {}, false],
['closeTag', ':b', false],
['closeTag', '_a', false],
],
Expand All @@ -185,7 +185,7 @@ test({
test({
xml: '<a><!--comment text--></a>',
expect: [
['openTag', 'a', true, false],
['openTag', 'a', {}, false],
['comment', 'comment text'],
['closeTag', 'a', false],
],
Expand All @@ -203,7 +203,7 @@ test({
test({
xml: '<root/><f',
expect: [
['openTag', 'root', true, true],
['openTag', 'root', {}, true],
['closeTag', 'root', true],
['error', 'unclosed tag', { data: '<f', line: 0, column: 7 } ],
],
Expand All @@ -212,15 +212,15 @@ test({
test({
xml: '<root></rof',
expect: [
['openTag', 'root', true, false],
['openTag', 'root', {}, false],
['error', 'unclosed tag', { data: '</rof', line: 0, column: 6 } ]
],
});

test({
xml: '<root></rof</root>',
expect: [
['openTag', 'root', true, false],
['openTag', 'root', {}, false],
['error', 'closing tag mismatch', { data: '</rof</root>', line: 0, column: 6 } ]
],
});
Expand Down Expand Up @@ -344,7 +344,7 @@ test({
xml: '<root length=\'12345\'><item/></root>',
expect: [
['openTag', 'root', { length: '12345' }, false],
['openTag', 'item', true, true],
['openTag', 'item', {}, true],
['closeTag', 'item', true],
['closeTag', 'root', false]
],
Expand Down Expand Up @@ -512,7 +512,7 @@ test({
data: '<feed xmlns="http://www.w3.org/2005/Atom" \r\n xmlns:="http://search.yahoo.com/mrss/" id="aa" :title="bb">'
}],
['text', '\r '],
['openTag', 'media:text', true, true, { line: 2, column: 2, data: '<:text/>' }],
['openTag', 'media:text', {}, true, { line: 2, column: 2, data: '<:text/>' }],
['closeTag', 'media:text', true, { line: 2, column: 2, data: '<:text/>' }],
['text', '\n'],
['closeTag', 'atom:feed', false, {
Expand All @@ -537,7 +537,7 @@ test({
column: 0,
data: '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:="http://search.yahoo.com/mrss/" id="aa" :title="bb">'
}],
['openTag', 'media:text', true, true, { line: 0, column: 101, data: '<:text/>' }],
['openTag', 'media:text', {}, true, { line: 0, column: 101, data: '<:text/>' }],
['closeTag', 'media:text', true, { line: 0, column: 101, data: '<:text/>' }],
['closeTag', 'atom:feed', false, {
line: 0,
Expand Down Expand Up @@ -576,7 +576,7 @@ test({
),
ns: true,
expect: [
['openTag', 'ns0:foo', true, false],
['openTag', 'ns0:foo', {}, false],
['openTag', 'ns1:t', { id: 'aa', 'bar:title': 'bb' }, true],
['closeTag', 'ns1:t', true],
['closeTag', 'ns0:foo', false],
Expand Down
9 changes: 4 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function test(options) {
return 'record ' + actualIdx + (typeof columnIdx !== 'undefined' ? ',' + columnIdx : '') + ' ';
}

assert.ok(expected, prefix() + 'unexpected: ' + util.inspect(actual));
assert.ok(expected, prefix() + 'unexpected: ' + inspect(actual));

var name = actual[0];

Expand All @@ -63,9 +63,8 @@ function test(options) {

if (name === 'openTag' && idx === 2) {
// be able to skip attrs check
if (!expectedValue || expectedValue === true) {
assert.equal(!!actualValue, expectedValue, prefix(idx) + ' attrs equal ' + expectedValue);

if (expectedValue === false) {
assert.equal(actualValue, expectedValue, prefix(idx) + ' attrs to equal ' + expectedValue);
} else {

obj = {};
Expand Down Expand Up @@ -111,7 +110,7 @@ function test(options) {
assert.equal(
recordedEntries.length,
expectedEntries.length,
'expected ' + expectedEntries.length + ' records, got ' + recordedEntries.length + ': \n' + util.inspect(recordedEntries)
'expected ' + expectedEntries.length + ' records, got ' + recordedEntries.length + ': \n' + inspect(recordedEntries)
);

for (var idx = 0; idx < recordedEntries.length; idx++) {
Expand Down

0 comments on commit f7360b1

Please sign in to comment.