Skip to content

Commit

Permalink
Fix throwing in parseAttrs
Browse files Browse the repository at this point in the history
  • Loading branch information
matteodelabre committed Sep 4, 2016
1 parent 727070b commit 8c6db98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
24 changes: 9 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ const parse = function (input) {
};

/**
* Parse XML attributes
* Parse a string of XML attributes to a map of attribute names
* to their values
*
* @memberof Saxophone
* @param {string} input A string of XML attributes
* @throws {Error} If the string is malformed
* @return {Object} A map of attribute names to their values
*/
const parseAttrs = input => {
const attrs = {}, end = input.length;
Expand All @@ -231,26 +235,18 @@ const parseAttrs = input => {

// check that the attribute name contains valid chars
const startName = position;
let hasError = false;

while (input[position] !== '=' && position < end) {
if (isWhitespace(input[position])) {
this.emit('error', new Error('Attribute names may not contain whitespace'));
hasError = true;
break;
throw new Error('Attribute names may not contain whitespace');
}

position += 1;
}

// this is XML so we need a value for the attribute
if (position === end) {
this.emit('error', new Error('Expected a value for the attribute'));
break;
}

if (hasError) {
break;
throw new Error('Expected a value for the attribute');
}

const attrName = input.slice(startName, position);
Expand All @@ -259,15 +255,13 @@ const parseAttrs = input => {
position += 1;

if (startQuote !== '"' && startQuote !== "'") {
this.emit('error', new Error('Attribute values should be quoted'));
break;
throw new Error('Attribute values should be quoted');
}

const endQuote = input.indexOf(startQuote, position);

if (endQuote === -1) {
this.emit('error', new Error('Unclosed attribute value'));
break;
throw new Error('Unclosed attribute value');
}

const attrValue = input.slice(position, endQuote);
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,28 @@ test('should parse tag attributes', assert => {
test('should not parse attributes without a value', assert => {
assert.throws(() => {
Saxophone.parseAttrs(' first');
}, new Error('Expected a value for the attribute'));
}, /Expected a value for the attribute/);
assert.end();
});

test('should not parse invalid attribute names', assert => {
assert.throws(() => {
Saxophone.parseAttrs(' this is an attribute="value"');
}, new Error('Attribute names may not contain whitespace'));
}, /Attribute names may not contain whitespace/);
assert.end();
});

test('should not parse unquoted attribute values', assert => {
assert.throws(() => {
Saxophone.parseAttrs(' attribute=value value=invalid');
}, new Error('Attribute values should be quoted'));
}, /Attribute values should be quoted/);
assert.end();
});

test('should not parse misquoted attribute values', assert => {
assert.throws(() => {
Saxophone.parseAttrs(' attribute="value\'');
}, new Error('Unclosed attribute value'));
}, /Unclosed attribute value/);
assert.end();
});

Expand Down

0 comments on commit 8c6db98

Please sign in to comment.