Skip to content

Commit

Permalink
Merge pull request #2 from aweary/boolean-attribute-values
Browse files Browse the repository at this point in the history
Allow unquoted boolean attribute values
  • Loading branch information
aweary authored Aug 22, 2017
2 parents fc106dd + 336f814 commit 2eaf22b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
}
return parsed
}

const parseAsBoolean = (d, i, reject) => {
if (d[0] === 'true') return true;
if (d[0] === 'false') return false;
return reject;
}
%}

combinator ->
Expand Down Expand Up @@ -104,6 +110,8 @@ attributeValue ->
floatOrInt ->
int "." int {% parseAsNumber %}
| int {% parseAsNumber %}
| "false" {% parseAsBoolean %}
| "true" {% parseAsBoolean %}

int -> [0-9]:+

Expand Down
20 changes: 20 additions & 0 deletions test/selectors/attributeValueSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,23 @@ for (const invalidAttributeValue of invalidAttributeValues) {
});
});
}

for (const booleanValue of [true, false]) {
test('valid attribute boolean value: [foo=' + String(booleanValue) + ']', (t): void => {
const tokens = parse('[foo=' + String(booleanValue) + ']');

if (tokens[0].type !== 'selector') {
throw new Error('Unexpected state.');
}

t.deepEqual(
tokens[0].body[0],
{
name: 'foo',
operator: '=',
type: 'attributeValueSelector',
value: booleanValue
}
);
});
}

0 comments on commit 2eaf22b

Please sign in to comment.