1 | const utils = require('./utils');
|
2 | const Node = require('./Node');
|
3 |
|
4 |
|
5 | module.exports = class Attr extends Node {
|
6 |
|
7 | constructor(
|
8 | ownerElement,
|
9 | name,
|
10 | /* istanbul ignore next */
|
11 | value = null
|
12 | ) {
|
13 | super(ownerElement.ownerDocument);
|
14 | this.ownerElement = ownerElement;
|
15 | this.name = name;
|
16 | this.nodeType = Node.ATTRIBUTE_NODE;
|
17 | this.nodeName = name;
|
18 | this._value = value;
|
19 | }
|
20 |
|
21 | get value() {
|
22 | return this.name === 'style' ? this._value.cssText : this._value;
|
23 | }
|
24 |
|
25 | set value(_value) {
|
26 | const oldValue = this._value;
|
27 | switch (this.name) {
|
28 | case 'style':
|
29 | this._value.cssText = _value;
|
30 | break;
|
31 | case 'class':
|
32 | if (this.ownerElement) {
|
33 | const cl = this.ownerElement.classList;
|
34 | if (_value == null) {
|
35 | this._value = _value;
|
36 | cl.splice(0, cl.length);
|
37 | } else {
|
38 | this._value = String(_value);
|
39 | if (oldValue !== this._value) {
|
40 | cl.value = this._value;
|
41 | }
|
42 | }
|
43 | break;
|
44 | }
|
45 | default:
|
46 | this._value = _value;
|
47 | break;
|
48 | }
|
49 | if (this.ownerElement && oldValue !== this._value) {
|
50 | utils.notifyAttributeChanged(
|
51 | this.ownerElement,
|
52 | this.name, oldValue, this._value
|
53 | );
|
54 | }
|
55 | }
|
56 |
|
57 | };
|