-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathattr.js
More file actions
56 lines (48 loc) · 1.7 KB
/
attr.js
File metadata and controls
56 lines (48 loc) · 1.7 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
55
56
'use strict';
const {ATTRIBUTE_NODE} = require('../shared/constants.js');
const {CHANGED, VALUE, MIME} = require('../shared/symbols.js');
const {String} = require('../shared/utils.js');
const {attrAsJSON} = require('../shared/jsdon.js');
const {emptyAttributes} = require('../shared/attributes.js');
const {attributeChangedCallback: moAttributes} = require('./mutation-observer.js');
const {attributeChangedCallback: ceAttributes} = require('./custom-element-registry.js');
const {Node} = require('./node.js');
const QUOTE = /"/g;
/**
* @implements globalThis.Attr
*/
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;
}
}
exports.Attr = Attr