-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathattr.js
More file actions
54 lines (46 loc) · 1.64 KB
/
attr.js
File metadata and controls
54 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {ATTRIBUTE_NODE} from '../shared/constants.js';
import {CHANGED, VALUE, MIME} from '../shared/symbols.js';
import {String} from '../shared/utils.js';
import {attrAsJSON} from '../shared/jsdon.js';
import {emptyAttributes} from '../shared/attributes.js';
import {attributeChangedCallback as moAttributes} from './mutation-observer.js';
import {attributeChangedCallback as ceAttributes} from './custom-element-registry.js';
import {Node} from './node.js';
const QUOTE = /"/g;
/**
* @implements globalThis.Attr
*/
export class Attr extends Node {
constructor(ownerDocument, name, value = '') {
super(ownerDocument, '#attribute', ATTRIBUTE_NODE);
this.ownerElement = null;
this.name = String(name);
this[VALUE] = String(value);
this[CHANGED] = false;
}
get value() { return this[VALUE]; }
set value(newValue) {
const {[VALUE]: oldValue, name, ownerElement} = this;
this[VALUE] = String(newValue);
this[CHANGED] = true;
if (ownerElement) {
moAttributes(ownerElement, name, oldValue);
ceAttributes(ownerElement, name, oldValue, this[VALUE]);
}
}
cloneNode() {
const {ownerDocument, name, [VALUE]: value} = this;
return new Attr(ownerDocument, name, value);
}
toString() {
const {ownerDocument, name, [VALUE]: value} = this;
const doubleQuote = ownerDocument[MIME].unquotedJsonAttributes && /^\{(.[\s\S]?)+\}$/.test(value) ? '' : '"'
return emptyAttributes.has(name) && !value ?
name : `${name}=${doubleQuote}${value.replace(QUOTE, doubleQuote ? '"' : '"')}${doubleQuote}`;
}
toJSON() {
const json = [];
attrAsJSON(this, json);
return json;
}
}