@remote-dom/polyfill 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,81 +0,0 @@
1
- 'use strict';
2
-
3
- var constants = require('./constants.cjs');
4
- var ParentNode = require('./ParentNode.cjs');
5
- var NamedNodeMap = require('./NamedNodeMap.cjs');
6
- var Attr = require('./Attr.cjs');
7
- var serialization = require('./serialization.cjs');
8
-
9
- class Element extends ParentNode.ParentNode {
10
- nodeType = constants.NodeType.ELEMENT_NODE;
11
- [constants.NS] = constants.NamespaceURI.XHTML;
12
- [constants.SLOT] = '';
13
- get namespaceURI() {
14
- return this[constants.NS];
15
- }
16
- get tagName() {
17
- return this.nodeName;
18
- }
19
- get slot() {
20
- return this[constants.SLOT];
21
- }
22
- set slot(slot) {
23
- const finalSlot = String(slot);
24
- this[constants.SLOT] = finalSlot;
25
- if (this.getAttribute('slot') !== finalSlot) {
26
- this.attributes.setNamedItem(new Attr.Attr('slot', finalSlot));
27
- }
28
- }
29
- get attributes() {
30
- let attributes = this[constants.ATTRIBUTES];
31
- if (!attributes) {
32
- attributes = new NamedNodeMap.NamedNodeMap(this);
33
- this[constants.ATTRIBUTES] = attributes;
34
- }
35
- return attributes;
36
- }
37
- setAttribute(name, value) {
38
- this.attributes.setNamedItem(new Attr.Attr(name, String(value)));
39
- }
40
- setAttributeNS(namespace, name, value) {
41
- this.attributes.setNamedItemNS(new Attr.Attr(name, String(value), namespace));
42
- }
43
- getAttribute(name) {
44
- const attr = this.attributes.getNamedItem(name);
45
- return attr && attr.value;
46
- }
47
- getAttributeNS(namespace, name) {
48
- const attr = this.attributes.getNamedItemNS(namespace, name);
49
- return attr && attr.value;
50
- }
51
- hasAttribute(name) {
52
- const attr = this.attributes.getNamedItem(name);
53
- return attr != null;
54
- }
55
- hasAttributeNS(namespace, name) {
56
- const attr = this.attributes.getNamedItemNS(namespace, name);
57
- return attr != null;
58
- }
59
- removeAttribute(name) {
60
- this.attributes.removeNamedItem(name);
61
- }
62
- removeAttributeNS(namespace, name) {
63
- this.attributes.removeNamedItemNS(namespace, name);
64
- }
65
- get outerHTML() {
66
- return serialization.serializeNode(this);
67
- }
68
- get innerHTML() {
69
- return serialization.serializeChildren(this);
70
- }
71
- set innerHTML(html) {
72
- if (html == null || html === '') {
73
- this.replaceChildren();
74
- } else {
75
- const fragment = serialization.parseHtml(String(html), this);
76
- this.replaceChildren(fragment);
77
- }
78
- }
79
- }
80
-
81
- exports.Element = Element;
@@ -1,113 +0,0 @@
1
- 'use strict';
2
-
3
- var constants = require('./constants.cjs');
4
-
5
- let EventPhase = /*#__PURE__*/function (EventPhase) {
6
- EventPhase[EventPhase["NONE"] = 0] = "NONE";
7
- EventPhase[EventPhase["CAPTURING_PHASE"] = 1] = "CAPTURING_PHASE";
8
- EventPhase[EventPhase["AT_TARGET"] = 2] = "AT_TARGET";
9
- EventPhase[EventPhase["BUBBLING_PHASE"] = 3] = "BUBBLING_PHASE";
10
- return EventPhase;
11
- }({});
12
- const CAPTURE_MARKER = '@';
13
- const now = typeof performance === 'undefined' ? Date.now : performance.now.bind(performance);
14
- class Event {
15
- static NONE = EventPhase.NONE;
16
- static CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
17
- static AT_TARGET = EventPhase.AT_TARGET;
18
- static BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
19
-
20
- // NONE = EventPhase.NONE;
21
- // CAPTURING_PHASE = EventPhase.CAPTURING_PHASE;
22
- // AT_TARGET = EventPhase.AT_TARGET;
23
- // BUBBLING_PHASE = EventPhase.BUBBLING_PHASE;
24
-
25
- timeStamp = now();
26
- target = null;
27
- currentTarget = null;
28
- srcElement = null;
29
- bubbles = false;
30
- cancelable = false;
31
- composed = false;
32
- defaultPrevented = false;
33
- cancelBubble = false;
34
- immediatePropagationStopped = false;
35
- eventPhase = 0;
36
- path = [];
37
- // private inPassiveListener = false;
38
-
39
- constructor(type, options) {
40
- this.type = type;
41
- Object.defineProperty(this, constants.IS_TRUSTED, {
42
- writable: true,
43
- value: false
44
- });
45
- if (options) {
46
- if (options.bubbles) this.bubbles = options.bubbles;
47
- if (options.cancelable) this.cancelable = options.cancelable;
48
- if (options.composed) this.composed = options.composed;
49
- }
50
- }
51
- get composedPath() {
52
- return this.path;
53
- }
54
- get isTrusted() {
55
- return this[constants.IS_TRUSTED];
56
- }
57
- stopPropagation() {
58
- this.cancelBubble = true;
59
- }
60
- stopImmediatePropagation() {
61
- this.immediatePropagationStopped = true;
62
- }
63
- preventDefault() {
64
- this.defaultPrevented = true;
65
- }
66
- set returnValue(value) {
67
- this.defaultPrevented = value;
68
- }
69
- get returnValue() {
70
- return this.defaultPrevented;
71
- }
72
-
73
- /** @deprecated */
74
- initEvent(type, bubbles, cancelable) {
75
- this.type = type;
76
- this.bubbles = Boolean(bubbles);
77
- this.cancelable = Boolean(cancelable);
78
- }
79
- }
80
- function fireEvent(event, target, phase) {
81
- const listeners = target[constants.LISTENERS];
82
- const list = listeners?.get(`${event.type}${phase === EventPhase.CAPTURING_PHASE ? CAPTURE_MARKER : ''}`);
83
- if (!list) return false;
84
- let defaultPrevented = false;
85
- for (const listener of list) {
86
- event.eventPhase = phase;
87
- event.currentTarget = target;
88
- try {
89
- if (typeof listener === 'object') {
90
- listener.handleEvent(event);
91
- } else {
92
- listener.call(target, event);
93
- }
94
- } catch (err) {
95
- setTimeout(thrower, 0, err);
96
- }
97
- if (event.defaultPrevented === true) {
98
- defaultPrevented = true;
99
- }
100
- if (event.immediatePropagationStopped) {
101
- break;
102
- }
103
- }
104
- return defaultPrevented;
105
- }
106
- function thrower(error) {
107
- throw error;
108
- }
109
-
110
- exports.CAPTURE_MARKER = CAPTURE_MARKER;
111
- exports.Event = Event;
112
- exports.EventPhase = EventPhase;
113
- exports.fireEvent = fireEvent;
@@ -1,101 +0,0 @@
1
- 'use strict';
2
-
3
- var hooks = require('./hooks.cjs');
4
- var constants = require('./constants.cjs');
5
- var Event = require('./Event.cjs');
6
-
7
- const ONCE_LISTENERS = Symbol('onceListeners');
8
- class EventTarget {
9
- addEventListener(type, listener, options) {
10
- if (listener == null) return;
11
- const capture = options === true || options && options.capture === true;
12
- const once = typeof options === 'object' && options.once === true;
13
- const signal = typeof options === 'object' ? options.signal : undefined;
14
- const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
15
- let normalizedListener = listener;
16
- if (once) {
17
- normalizedListener = function normalizedListener(...args) {
18
- this.removeEventListener(type, listener, options);
19
- return typeof listener === 'object' ? listener.handleEvent(...args) : listener.call(this, ...args);
20
- };
21
- let onceListeners = this[ONCE_LISTENERS];
22
- if (!onceListeners) {
23
- onceListeners = new WeakMap();
24
- this[ONCE_LISTENERS] = onceListeners;
25
- }
26
- onceListeners.set(listener, normalizedListener);
27
- }
28
- let listeners = this[constants.LISTENERS];
29
- if (!listeners) {
30
- listeners = new Map();
31
- this[constants.LISTENERS] = listeners;
32
- }
33
- let list = listeners.get(key);
34
- if (!list) {
35
- list = new Set();
36
- listeners.set(key, list);
37
- }
38
- list.add(normalizedListener);
39
- signal?.addEventListener('abort', () => {
40
- removeEventListener.call(this, type, listener, options);
41
- }, {
42
- once: true
43
- });
44
- hooks.hooks.addEventListener?.(this, type, listener, options);
45
- }
46
- removeEventListener(type, listener, options) {
47
- return removeEventListener.call(this, type, listener, options);
48
- }
49
-
50
- // function isChildNode(node: EventTarget): node is ChildNode {
51
- // return PARENT in node;
52
- // }
53
-
54
- dispatchEvent(event) {
55
- const path = [];
56
- // instanceof here is just to keep TypeScript happy
57
- let target = this;
58
- while (target != null) {
59
- path.push(target);
60
- target = target.parentNode;
61
- }
62
- // while (target instanceof Node && (target = target.parentNode)) {
63
- // path.push(target);
64
- // }
65
- event.target = this;
66
- event.srcElement = this;
67
- event.path = path;
68
- let defaultPrevented = false;
69
- for (let i = path.length; --i;) {
70
- if (Event.fireEvent(event, path[i], Event.EventPhase.CAPTURING_PHASE)) {
71
- defaultPrevented = true;
72
- }
73
- }
74
- if (Event.fireEvent(event, this, Event.EventPhase.AT_TARGET)) {
75
- defaultPrevented = true;
76
- }
77
- for (let i = 1; i < path.length; i++) {
78
- if (Event.fireEvent(event, path[i], Event.EventPhase.BUBBLING_PHASE)) {
79
- defaultPrevented = true;
80
- }
81
- }
82
- return !defaultPrevented;
83
- }
84
- }
85
- function removeEventListener(type, listener, options) {
86
- if (listener == null) return;
87
- const onceListeners = this[ONCE_LISTENERS];
88
- const normalizedListener = onceListeners?.get(listener) ?? listener;
89
- onceListeners?.delete(listener);
90
- const capture = options === true || options && options.capture === true;
91
- const key = `${type}${capture ? Event.CAPTURE_MARKER : ''}`;
92
- const list = this[constants.LISTENERS]?.get(key);
93
- if (list) {
94
- const deleted = list.delete(normalizedListener);
95
- if (deleted) {
96
- hooks.hooks.removeEventListener?.(this, type, listener, options);
97
- }
98
- }
99
- }
100
-
101
- exports.EventTarget = EventTarget;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- var Element = require('./Element.cjs');
4
-
5
- class HTMLElement extends Element.Element {}
6
-
7
- exports.HTMLElement = HTMLElement;
@@ -1,26 +0,0 @@
1
- 'use strict';
2
-
3
- var constants = require('./constants.cjs');
4
- var Element = require('./Element.cjs');
5
- var serialization = require('./serialization.cjs');
6
-
7
- class HTMLTemplateElement extends Element.Element {
8
- get content() {
9
- let content = this[constants.CONTENT];
10
- if (!content) {
11
- content = this.ownerDocument.createDocumentFragment();
12
- this[constants.CONTENT] = content;
13
- }
14
- return content;
15
- }
16
- set content(_) {}
17
- set innerHTML(html) {
18
- this.content.replaceChildren(serialization.parseHtml(String(html), this));
19
- }
20
- get innerHTML() {
21
- const content = this[constants.CONTENT];
22
- return content ? serialization.serializeChildren(content) : '';
23
- }
24
- }
25
-
26
- exports.HTMLTemplateElement = HTMLTemplateElement;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- class MutationObserver {
4
- // stub implementation
5
- }
6
-
7
- exports.MutationObserver = MutationObserver;
@@ -1,115 +0,0 @@
1
- 'use strict';
2
-
3
- var hooks = require('./hooks.cjs');
4
- var constants = require('./constants.cjs');
5
-
6
- class NamedNodeMap {
7
- [constants.CHILD] = null;
8
- constructor(ownerElement) {
9
- this[constants.OWNER_ELEMENT] = ownerElement;
10
- }
11
- getNamedItem(name) {
12
- return this.getNamedItemNS(null, name);
13
- }
14
- getNamedItemNS(namespaceURI, name) {
15
- let attr = this[constants.CHILD];
16
- while (attr) {
17
- if (attr.name === name && attr[constants.NS] == namespaceURI) {
18
- return attr;
19
- }
20
- attr = attr[constants.NEXT];
21
- }
22
- return null;
23
- }
24
- item(index) {
25
- let attr = this[constants.CHILD];
26
- let i = 0;
27
- while (attr) {
28
- if (i++ === index) return attr;
29
- attr = attr[constants.NEXT];
30
- }
31
- return null;
32
- }
33
- get length() {
34
- let index = 0;
35
- let attr = this[constants.CHILD];
36
- while (attr) {
37
- index++;
38
- attr = attr[constants.NEXT];
39
- }
40
- return index;
41
- }
42
- removeNamedItem(name) {
43
- return this.removeNamedItemNS(null, name);
44
- }
45
- removeNamedItemNS(namespaceURI, name) {
46
- const ownerElement = this[constants.OWNER_ELEMENT];
47
- let attr = this[constants.CHILD];
48
- let prev = null;
49
- while (attr != null) {
50
- if (attr.name === name && attr[constants.NS] == namespaceURI) {
51
- if (prev) prev[constants.NEXT] = attr[constants.NEXT];
52
- if (this[constants.CHILD] === attr) this[constants.CHILD] = attr[constants.NEXT];
53
- updateElementAttribute(ownerElement, attr.name, attr.value, null);
54
- hooks.hooks.removeAttribute?.(ownerElement, name, namespaceURI);
55
- return attr;
56
- }
57
- prev = attr;
58
- attr = attr[constants.NEXT];
59
- }
60
- return null;
61
- }
62
- setNamedItem(attr) {
63
- const ownerElement = this[constants.OWNER_ELEMENT];
64
- let old = null;
65
- let child = this[constants.CHILD];
66
- attr[constants.OWNER_ELEMENT] = ownerElement;
67
- if (child == null) {
68
- this[constants.CHILD] = attr;
69
- // return null;
70
- } else {
71
- let prev;
72
- while (child) {
73
- if (child.name === attr.name && child[constants.NS] == attr[constants.NS]) {
74
- if (prev) prev[constants.NEXT] = attr;else this[constants.CHILD] = attr;
75
- attr[constants.NEXT] = child[constants.NEXT];
76
- child[constants.NEXT] = null;
77
- old = child;
78
- break;
79
- // return child;
80
- }
81
-
82
- prev = child;
83
- child = child[constants.NEXT];
84
- }
85
- if (prev) prev[constants.NEXT] = attr;else this[constants.CHILD] = attr;
86
- // return null;
87
- }
88
- // only invoke the protocol if the value changed
89
- if (!old || old.value !== attr.value) {
90
- updateElementAttribute(ownerElement, attr.name, old?.value ?? null, attr.value);
91
- hooks.hooks.setAttribute?.(ownerElement, attr.name, attr.value, attr[constants.NS]);
92
- }
93
- return old;
94
- }
95
- setNamedItemNS(attr) {
96
- return this.setNamedItem(attr);
97
- }
98
- }
99
- function updateElementAttribute(element, name, oldValue, newValue) {
100
- const {
101
- observedAttributes
102
- } = element.constructor;
103
- const {
104
- attributeChangedCallback
105
- } = element;
106
- if (name === 'slot' && element.slot !== newValue) {
107
- element.slot = newValue ?? '';
108
- }
109
- if (attributeChangedCallback == null || observedAttributes == null || !observedAttributes.includes(name)) {
110
- return;
111
- }
112
- return attributeChangedCallback.call(element, name, oldValue, newValue);
113
- }
114
-
115
- exports.NamedNodeMap = NamedNodeMap;
@@ -1,102 +0,0 @@
1
- 'use strict';
2
-
3
- var constants = require('./constants.cjs');
4
- var EventTarget = require('./EventTarget.cjs');
5
- var shared = require('./shared.cjs');
6
-
7
- class Node extends EventTarget.EventTarget {
8
- nodeType = constants.NodeType.NODE;
9
- [constants.NAME] = '';
10
- [constants.PARENT] = null;
11
- [constants.CHILD] = null;
12
- [constants.PREV] = null;
13
- [constants.NEXT] = null;
14
- get localName() {
15
- return this[constants.NAME];
16
- }
17
- get nodeName() {
18
- return this[constants.NAME].toUpperCase();
19
- }
20
- get ownerDocument() {
21
- return this[constants.OWNER_DOCUMENT];
22
- }
23
- isDefaultNamespace(namespace) {
24
- return namespace === constants.NamespaceURI.XHTML;
25
- }
26
- get parentNode() {
27
- return this[constants.PARENT];
28
- }
29
- set parentNode(_readonly) {}
30
- get previousSibling() {
31
- return this[constants.PREV];
32
- }
33
- set previousSibling(_readonly) {}
34
- get nextSibling() {
35
- return this[constants.NEXT];
36
- }
37
- set nextSibling(_readonly) {}
38
- get previousElementSibling() {
39
- let sib = this[constants.PREV];
40
- while (sib && sib.nodeType !== 1) sib = sib[constants.PREV];
41
- return sib;
42
- }
43
- set previousElementSibling(_readonly) {}
44
- get nextElementSibling() {
45
- let sib = this[constants.NEXT];
46
- while (sib && sib.nodeType !== 1) sib = sib[constants.NEXT];
47
- return sib;
48
- }
49
- set nextElementSibling(_readonly) {}
50
- get firstChild() {
51
- return this[constants.CHILD];
52
- }
53
- set firstChild(_readonly) {}
54
- get lastChild() {
55
- let child = this[constants.CHILD];
56
- while (child) {
57
- const next = child[constants.NEXT];
58
- if (next == null) break;
59
- child = next;
60
- }
61
- return child;
62
- }
63
- set lastChild(_readonly) {}
64
- get nodeValue() {
65
- if (shared.isCharacterData(this)) return this.data;
66
- return null;
67
- }
68
- set nodeValue(data) {
69
- if (shared.isCharacterData(this)) this.data = data;
70
- }
71
- get textContent() {
72
- if (shared.isCharacterData(this)) return this.data;
73
- let text = '';
74
- function walk(node) {
75
- if (shared.isTextNode(node)) {
76
- text += node.data;
77
- }
78
- const child = node[constants.CHILD];
79
- if (child) walk(child);
80
- const sibling = node[constants.NEXT];
81
- if (sibling) walk(sibling);
82
- }
83
- walk(this);
84
- return text;
85
- }
86
- set textContent(data) {
87
- if (shared.isCharacterData(this)) {
88
- this.data = data;
89
- } else if (shared.isParentNode(this)) {
90
- let child;
91
- while (child = this[constants.CHILD]) {
92
- this.removeChild(child);
93
- }
94
- this.append(data);
95
- }
96
- }
97
- cloneNode(deep) {
98
- return shared.cloneNode(this, deep);
99
- }
100
- }
101
-
102
- exports.Node = Node;
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- class NodeList extends Array {
4
- item(index) {
5
- return this[index];
6
- }
7
- }
8
-
9
- exports.NodeList = NodeList;
@@ -1,130 +0,0 @@
1
- 'use strict';
2
-
3
- var hooks = require('./hooks.cjs');
4
- var constants = require('./constants.cjs');
5
- var ChildNode = require('./ChildNode.cjs');
6
- var NodeList = require('./NodeList.cjs');
7
- var selectors = require('./selectors.cjs');
8
-
9
- class ParentNode extends ChildNode.ChildNode {
10
- childNodes = new NodeList.NodeList();
11
- children = new NodeList.NodeList();
12
- appendChild(child) {
13
- this.insertInto(child, null);
14
- }
15
- insertBefore(child, ref) {
16
- this.insertInto(child, ref || null);
17
- }
18
- append(...nodes) {
19
- for (const child of nodes) {
20
- if (child == null) continue;
21
- this.appendChild(ChildNode.toNode(this, child));
22
- }
23
- }
24
- prepend(...nodes) {
25
- const before = this.firstChild;
26
- for (const child of nodes) {
27
- if (child == null) continue;
28
- this.insertBefore(ChildNode.toNode(this, child), before);
29
- }
30
- }
31
- replaceChildren(...nodes) {
32
- let child;
33
- while (child = this.firstChild) {
34
- this.removeChild(child);
35
- }
36
- this.append(...nodes);
37
- }
38
- removeChild(child) {
39
- if (child.parentNode !== this) throw Error(`not a child of this node`);
40
- const prev = child[constants.PREV];
41
- const next = child[constants.NEXT];
42
- if (prev) prev[constants.NEXT] = next;else this[constants.CHILD] = next;
43
- if (next) next[constants.PREV] = prev;
44
- const childNodes = this.childNodes;
45
- const childNodesIndex = childNodes.indexOf(child);
46
- childNodes.splice(childNodesIndex, 1);
47
- if (child.nodeType === 1) {
48
- const children = this.children;
49
- children.splice(children.indexOf(child), 1);
50
- }
51
- hooks.hooks.removeChild?.(this, child, childNodesIndex);
52
- }
53
- replaceChild(newChild, oldChild) {
54
- if (oldChild.parentNode !== this) {
55
- throw Error('reference node is not a child of this parent');
56
- }
57
- const next = oldChild[constants.NEXT];
58
- this.removeChild(oldChild);
59
- this.insertInto(newChild, next);
60
- }
61
- querySelectorAll(selector) {
62
- return selectors.querySelectorAll(this, selector);
63
- }
64
- querySelector(selector) {
65
- return selectors.querySelector(this, selector);
66
- }
67
- insertInto(child, before) {
68
- // append the children of a DocumentFragment:
69
- if (child.nodeType === constants.NodeType.DOCUMENT_FRAGMENT_NODE) {
70
- let node = child[constants.CHILD];
71
- while (node) {
72
- const next = node[constants.NEXT];
73
- this.insertInto(node, before);
74
- node = next;
75
- }
76
- return;
77
- }
78
- if (child.parentNode !== null) {
79
- child.parentNode.removeChild(child);
80
- }
81
- if (before) {
82
- if (before.parentNode !== this) {
83
- throw Error('reference node is not a child of this parent');
84
- }
85
- child[constants.NEXT] = before;
86
- child[constants.PREV] = before[constants.PREV];
87
- if (before[constants.PREV] === null) this[constants.CHILD] = child;
88
- before[constants.PREV] = child;
89
- } else {
90
- child[constants.NEXT] = null;
91
- let last = this[constants.CHILD];
92
- if (last) {
93
- let next;
94
- while (next = last[constants.NEXT]) last = next;
95
- last[constants.NEXT] = child;
96
- child[constants.PREV] = last;
97
- } else {
98
- this[constants.CHILD] = child;
99
- child[constants.PREV] = null;
100
- }
101
- }
102
- const ownerDocument = this[constants.OWNER_DOCUMENT];
103
- const isElement = child.nodeType === constants.NodeType.ELEMENT_NODE;
104
- child[constants.PARENT] = this;
105
- child[constants.OWNER_DOCUMENT] = ownerDocument;
106
- const childNodes = this.childNodes;
107
- let insertIndex;
108
- if (before) {
109
- insertIndex = childNodes.indexOf(before);
110
- childNodes.splice(insertIndex, 0, child);
111
- if (isElement) {
112
- const children = this.children;
113
- let ref = before;
114
- while (ref && ref.nodeType !== 1) ref = ref[constants.NEXT];
115
- if (ref) {
116
- children.splice(children.indexOf(ref), 0, child);
117
- } else {
118
- children.push(child);
119
- }
120
- }
121
- } else {
122
- insertIndex = childNodes.length;
123
- childNodes.push(child);
124
- if (isElement) this.children.push(child);
125
- }
126
- hooks.hooks.insertChild?.(this, child, insertIndex);
127
- }
128
- }
129
-
130
- exports.ParentNode = ParentNode;