Merge pull request #5 from leeoniya/parse-empty-attrs

parse value-less attributes
This commit is contained in:
taoqf 2019-03-21 10:46:58 +08:00 committed by GitHub
commit 99bae248a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View file

@ -460,10 +460,10 @@ export class HTMLElement extends Node {
return this._rawAttrs;
const attrs = {} as RawAttributes;
if (this.rawAttrs) {
const re = /\b([a-z][a-z0-9\-]*)\s*=\s*("([^"]+)"|'([^']+)'|(\S+))/ig;
const re = /\b([a-z][a-z0-9\-]*)(?:\s*=\s*(?:"([^"]+)"|'([^']+)'|(\S+)))?/ig;
let match: RegExpExecArray;
while (match = re.exec(this.rawAttrs)) {
attrs[match[1]] = match[3] || match[4] || match[5];
attrs[match[1]] = match[2] || match[3] || match[4] || "";
}
}
this._rawAttrs = attrs;

View file

@ -264,11 +264,12 @@ describe('HTML Parser', function () {
describe('#attributes', function () {
it('should return attributes of the element', function () {
var root = parseHTML('<p a=12 data-id="!$$&amp;" yAz=\'1\'></p>');
var root = parseHTML('<p a=12 data-id="!$$&amp;" yAz=\'1\' disabled></p>');
root.firstChild.attributes.should.eql({
'a': '12',
'data-id': '!$$&',
'yAz': '1'
'yAz': '1',
'disabled': ''
});
});
});