properly handle attrs with zero length values

This commit is contained in:
Leon Sorokin 2019-04-02 21:32:05 -05:00
parent a37b7cc941
commit dc2e4ee6ab
2 changed files with 4 additions and 3 deletions

View file

@ -460,7 +460,7 @@ export class HTMLElement extends Node {
return this._rawAttrs; return this._rawAttrs;
const attrs = {} as RawAttributes; const attrs = {} as RawAttributes;
if (this.rawAttrs) { 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; let match: RegExpExecArray;
while (match = re.exec(this.rawAttrs)) { while (match = re.exec(this.rawAttrs)) {
attrs[match[1]] = match[2] || match[3] || match[4] || ""; attrs[match[1]] = match[2] || match[3] || match[4] || "";

View file

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