From 9e8f0aaf1ef867d910aaaa68ae5a5cf361182259 Mon Sep 17 00:00:00 2001 From: Minas Keshishyan Date: Mon, 3 Feb 2020 12:05:22 +0400 Subject: [PATCH] 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 {