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

properly handle attrs with zero length values
This commit is contained in:
taoqf 2019-04-03 11:57:09 +08:00 committed by GitHub
commit d464145bc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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': ''
}); });
}); });
}); });