UNPKG

uhtml

Version:
58 lines (45 loc) • 1.17 kB
'use strict'; const { ATTRIBUTE_NODE } = require('domconstants/constants'); const { escape } = require('html-escaper'); const Node = (require('./node.js')); const { name, value, ownerElement, ownerDocument } = require('./symbols.js'); /** @typedef {Attribute} Attribute */ module.exports = class Attribute extends Node { constructor(nodeName, nodeValue = '', owner = null) { super(ATTRIBUTE_NODE, owner?.[ownerDocument]); this[ownerElement] = owner; this[name] = nodeName; this.value = nodeValue; } /** @type {import("./element.js").default?} */ get ownerElement() { return this[ownerElement]; } /** @type {string} */ get name() { return this[name]; } /** @type {string} */ get localName() { return this[name]; } /** @type {string} */ get nodeName() { return this[name]; } /** @type {string} */ get value() { return this[value]; } set value(any) { this[value] = String(any); } /** @type {string} */ get nodeValue() { return this[value]; } toString() { const { [name]: key, [value]: val } = this; return val === '' ? key : `${key}="${escape(val)}"`; } }