From 9e8f0aaf1ef867d910aaaa68ae5a5cf361182259 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 12:05:22 +0400 Subject: [PATCH 1/9] improve rawAttributes, fix setAttribute when the value is number --- src/nodes/html.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index d9210aa..3dc6cc6 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -421,7 +421,7 @@ export default 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[2] || null; @@ -441,20 +441,15 @@ export default class HTMLElement extends Node { if (this._attrs) { delete this._attrs; } - const attrs = this.rawAttributes; // ref this._rawAttrs + const attrs = this.rawAttributes; if (value === undefined || value === null) { delete attrs[key]; } else { - attrs[key] = JSON.stringify(value); - // if (typeof value === 'string') { - // attrs[key] = JSON.stringify(encode(value));//??? should we encode value here? - // } else { - // attrs[key] = JSON.stringify(value); - // } + attrs[key] = String(value); } // Update rawString this.rawAttrs = Object.keys(attrs).map((name) => { - const val = attrs[name]; + const val = JSON.stringify(attrs[name]); if (val === undefined || val === null) { return name; } else { From 1f0ebc52c75d76c9fd8e25ecec74aac438fa5bdc Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 13:19:32 +0400 Subject: [PATCH 2/9] fix setAttributes and attributes, also fix some tests, need more fixes --- src/nodes/html.ts | 9 ++------- test/html.js | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 3dc6cc6..3ec4486 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -407,7 +407,7 @@ export default class HTMLElement extends Node { const attrs = this.rawAttributes; for (const key in attrs) { const val = attrs[key] || ''; - this._attrs[key] = decode(val.replace(/^['"]/, '').replace(/['"]$/, '')); + this._attrs[key] = decode(val); } return this._attrs; } @@ -477,12 +477,7 @@ export default class HTMLElement extends Node { if (val === undefined || val === null) { return name; } else { - return name + '=' + JSON.stringify(val); - // if (typeof val === 'string') { - // return name + '=' + JSON.stringify(encode(val)); //??? should we encode value here? - // } else { - // return name + '=' + JSON.stringify(val); - // } + return name + '=' + JSON.stringify(String(val)); } }).join(' '); } diff --git a/test/html.js b/test/html.js index efa1597..b3da8ad 100644 --- a/test/html.js +++ b/test/html.js @@ -355,7 +355,7 @@ describe('HTML Parser', function () { 'b': '13', 'c': '2' }); - root.firstChild.toString().should.eql('

'); + root.firstChild.toString().should.eql('

'); }); }); @@ -370,7 +370,7 @@ describe('HTML Parser', function () { 'c': '12', d: '&&<>foo' }); - root.firstChild.toString().should.eql('

'); + root.firstChild.toString().should.eql('

'); // root.firstChild.toString().should.eql('

'); }); }); From 2315ec89a2a1773ef558e09920fd25296fa8d740 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 13:23:05 +0400 Subject: [PATCH 3/9] fix should edit the attributes of the element test --- test/html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/html.js b/test/html.js index b3da8ad..32bf39b 100644 --- a/test/html.js +++ b/test/html.js @@ -326,7 +326,7 @@ describe('HTML Parser', function () { root.firstChild.attributes.should.eql({ 'a': '13', }); - root.firstChild.toString().should.eql('

'); + root.firstChild.toString().should.eql('

'); }); it('should add an attribute to the element', function () { var root = parseHTML('

'); From 6aba0280bf88fa89e5e916b4d9e702bf60c10524 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 13:40:28 +0400 Subject: [PATCH 4/9] fix rawAttributes() --- src/nodes/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 3ec4486..3d08e44 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -424,7 +424,7 @@ export default class HTMLElement extends Node { 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[2] || null; + attrs[match[1]] = match[2] || match[3] || match[4] || null; } } this._rawAttrs = attrs; From 9257f300b94c3a9590dfe44a396d3eb889a69738 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 13:45:26 +0400 Subject: [PATCH 5/9] fix testes to expect the same behaviout as browsers --- test/html.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/html.js b/test/html.js index 32bf39b..dd34cf4 100644 --- a/test/html.js +++ b/test/html.js @@ -300,8 +300,8 @@ describe('HTML Parser', function () { var root = parseHTML('

'); root.firstChild.rawAttributes.should.eql({ 'a': '12', - 'data-id': '"!$$&"', - 'yAz': '\'1\'' + 'data-id': '!$$&', + 'yAz': '1' }); }); }); @@ -335,7 +335,7 @@ describe('HTML Parser', function () { 'a': '12', 'b': '13', }); - root.firstChild.toString().should.eql('

'); + root.firstChild.toString().should.eql('

'); }); it('should remove an attribute from the element', function () { var root = parseHTML('

'); @@ -344,7 +344,7 @@ describe('HTML Parser', function () { root.firstChild.attributes.should.eql({ 'a': '12', }); - root.firstChild.toString().should.eql('

'); + root.firstChild.toString().should.eql('

'); }); it('should keep quotes arount value', function () { var root = parseHTML('

'); From cb219a0d3a2b056bf34ca9cd522657f4108e00dc Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 15:17:49 +0400 Subject: [PATCH 6/9] add getAttribute --- src/nodes/html.ts | 8 ++++++++ test/html.js | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 3d08e44..9374a4f 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -431,6 +431,14 @@ export default class HTMLElement extends Node { return attrs; } + /** + * Get an attribute + * @return {string} value of the attribute + */ + getAttribute(key: string) { + return this.attributes[key] || null; + } + /** * Set an attribute value to the HTMLElement * @param {string} key The attribute name diff --git a/test/html.js b/test/html.js index dd34cf4..b5b0c7c 100644 --- a/test/html.js +++ b/test/html.js @@ -319,6 +319,18 @@ describe('HTML Parser', function () { }); }); + describe('#getAttribute', function () { + it('should return value of the attribute', function () { + var root = parseHTML('

'); + root.firstChild.getAttribute('a').should.eql('a1b'); + }); + + it('should return null when there is no such attribute', function () { + var root = parseHTML('

'); + should.equal(root.firstChild.getAttribute('b'), null); + }); + }); + describe('#setAttribute', function () { it('should edit the attributes of the element', function () { var root = parseHTML('

'); From a73bf56d39003115695c0ce51955cb1fea3045e0 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 15:52:15 +0400 Subject: [PATCH 7/9] improve comments --- src/nodes/html.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 9374a4f..9c2edac 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -471,11 +471,11 @@ export default class HTMLElement extends Node { * @param {Attributes} attributes the new attribute set */ setAttributes(attributes: Attributes) { - // Update the this.attributes + // Invalidate current this.attributes if (this._attrs) { delete this._attrs; } - // Update the raw attributes map + // Invalidate current this.rawAttributes if (this._rawAttrs) { delete this._rawAttrs; } From 7e15438af25b25b29889fe8229db6594ea074e96 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 15:57:17 +0400 Subject: [PATCH 8/9] improve comments --- src/nodes/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nodes/html.ts b/src/nodes/html.ts index 9c2edac..2972f35 100644 --- a/src/nodes/html.ts +++ b/src/nodes/html.ts @@ -445,7 +445,7 @@ export default class HTMLElement extends Node { * @param {string|number} value The value to set, or null / undefined to remove an attribute */ setAttribute(key: string, value: string | number) { - // Update the this.attributes + // Invalidate current this.attributes if (this._attrs) { delete this._attrs; } From b50dbedf7cc46b35ab25f36d6847a01687abbe26 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 17:19:38 +0400 Subject: [PATCH 9/9] update gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index f285cb6..217a554 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ bower_components *.sublime-* dist/ yarn.lock + +.idea +.DS_Store