mirror of
https://github.com/danbulant/node-html-parser
synced 2026-06-19 22:51:29 +00:00
improve rawAttributes, fix setAttribute when the value is number
This commit is contained in:
parent
4686ba46b2
commit
9e8f0aaf1e
1 changed files with 4 additions and 9 deletions
|
|
@ -421,7 +421,7 @@ export default 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] || null;
|
attrs[match[1]] = match[2] || null;
|
||||||
|
|
@ -441,20 +441,15 @@ export default class HTMLElement extends Node {
|
||||||
if (this._attrs) {
|
if (this._attrs) {
|
||||||
delete this._attrs;
|
delete this._attrs;
|
||||||
}
|
}
|
||||||
const attrs = this.rawAttributes; // ref this._rawAttrs
|
const attrs = this.rawAttributes;
|
||||||
if (value === undefined || value === null) {
|
if (value === undefined || value === null) {
|
||||||
delete attrs[key];
|
delete attrs[key];
|
||||||
} else {
|
} else {
|
||||||
attrs[key] = JSON.stringify(value);
|
attrs[key] = String(value);
|
||||||
// if (typeof value === 'string') {
|
|
||||||
// attrs[key] = JSON.stringify(encode(value));//??? should we encode value here?
|
|
||||||
// } else {
|
|
||||||
// attrs[key] = JSON.stringify(value);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
// Update rawString
|
// Update rawString
|
||||||
this.rawAttrs = Object.keys(attrs).map((name) => {
|
this.rawAttrs = Object.keys(attrs).map((name) => {
|
||||||
const val = attrs[name];
|
const val = JSON.stringify(attrs[name]);
|
||||||
if (val === undefined || val === null) {
|
if (val === undefined || val === null) {
|
||||||
return name;
|
return name;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue