Merge pull request #135 from sveltejs/uniq-elem-attrs

Make sure parser only allows unique attribute names
pull/138/head
Rich Harris 8 years ago committed by GitHub
commit 384e724c4c

@ -74,9 +74,10 @@ export default function tag ( parser ) {
} }
const attributes = []; const attributes = [];
const uniqueNames = new Map();
let attribute; let attribute;
while ( attribute = readAttribute( parser ) ) { while ( attribute = readAttribute( parser, uniqueNames ) ) {
attributes.push( attribute ); attributes.push( attribute );
parser.allowWhitespace(); parser.allowWhitespace();
} }
@ -133,11 +134,16 @@ function readTagName ( parser ) {
return name; return name;
} }
function readAttribute ( parser ) { function readAttribute ( parser, uniqueNames ) {
const start = parser.index; const start = parser.index;
const name = parser.readUntil( /(\s|=|\/|>)/ ); const name = parser.readUntil( /(\s|=|\/|>)/ );
if ( !name ) return null; if ( !name ) return null;
if ( uniqueNames.has(name) ) {
parser.error( 'Attributes need to be unique', start );
}
uniqueNames.set(name, true);
parser.allowWhitespace(); parser.allowWhitespace();

@ -0,0 +1,8 @@
{
"message": "Attributes need to be unique",
"loc": {
"line": 1,
"column": 17
},
"pos": 17
}

@ -0,0 +1 @@
<div class='foo' class='bar'></div>
Loading…
Cancel
Save