Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Dec 22, 2023
1 parent 77bbacf commit 1433d72
Show file tree
Hide file tree
Showing 19 changed files with 711 additions and 13 deletions.
12 changes: 9 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ node_modules/
types/
cjs/*
!cjs/package.json
index.js
esm/init.js
init.js
keyed.js
!esm/keyed.js
!esm/dom/keyed.js
index.js
!esm/index.js
!esm/dom/index.js
node.js
init.js
esm/init.js
!esm/node.js
!esm/dom/node.js
20 changes: 20 additions & 0 deletions esm/dom/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DOCUMENT_FRAGMENT_NODE } from 'domconstants/constants';

import { nodeType, childNodes } from './symbols.js';

export const push = (array, nodes) => {
array.push(...nodes.flatMap(withoutFragments));
};

export const splice = (array, start, drop, nodes) => {
array.splice(start, drop, ...nodes.flatMap(withoutFragments));
};

export const unshift = (array, nodes) => {
array.unshift(...nodes.flatMap(withoutFragments));
};

const withoutFragments = node => (
node[nodeType] === DOCUMENT_FRAGMENT_NODE ?
node[childNodes].splice(0) : node
);
44 changes: 44 additions & 0 deletions esm/dom/attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ATTRIBUTE_NODE } from 'domconstants/constants';

import { escape } from 'html-escaper';

import Node from './node.js';

import { name, value, ownerElement, ownerDocument } from './symbols.js';

export default class Attribute extends Node {
constructor(nodeName, nodeValue = '', owner = null) {
super(ATTRIBUTE_NODE, owner?.[ownerDocument]);
this[ownerElement] = owner;
this[name] = nodeName;
this.value = nodeValue;
}

get name() {
return this[name];
}

get localName() {
return this[name];
}

get nodeName() {
return this[name];
}

get value() {
return this[value];
}
set value(any) {
this[value] = String(any);
}

get nodeValue() {
return this[value];
}

toString() {
const { [name]: key, [value]: val } = this;
return val === '' ? key : `${key}="${escape(val)}"`;
}
}
28 changes: 28 additions & 0 deletions esm/dom/character-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Node from './node.js';
import { nodeName, value } from './symbols.js';

export default class CharacterData extends Node {
constructor(type, name, data, owner) {
super(type, owner)[nodeName] = name;
this.data = data;
}

get data() {
return this[value];
}
set data(any) {
this[value] = String(any);
}

get nodeName() {
return this[nodeName];
}

get textContent() {
return this.data;
}

set textContent(data) {
this.data = data;
}
}
14 changes: 14 additions & 0 deletions esm/dom/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { COMMENT_NODE } from 'domconstants/constants';

import CharacterData from './character-data.js';
import { value } from './symbols.js';

export default class Comment extends CharacterData {
constructor(data = '', owner = null) {
super(COMMENT_NODE, '#comment', data, owner);
}

toString() {
return `<!--${this[value]}-->`;
}
}
22 changes: 22 additions & 0 deletions esm/dom/document-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DOCUMENT_TYPE_NODE } from 'domconstants/constants';

import Node from './node.js';
import { nodeName } from './symbols.js';

export default class DocumentType extends Node {
constructor(name, owner = null) {
super(DOCUMENT_TYPE_NODE, owner)[nodeName] = name;
}

get nodeName() {
return this[nodeName];
}

get name() {
return this[nodeName];
}

toString() {
return `<!DOCTYPE ${this[nodeName]}>`;
}
}
15 changes: 15 additions & 0 deletions esm/dom/document.fragment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DOCUMENT_FRAGMENT_NODE } from 'domconstants/constants';

import Parent from './parent.js';

import { childNodes, nodeName } from './symbols.js';

export default class DocumentFragment extends Parent {
constructor(owner = null) {
super(DOCUMENT_FRAGMENT_NODE, owner)[nodeName] = '#document-fragment';
}

toString() {
return this[childNodes].join('');
}
}
77 changes: 77 additions & 0 deletions esm/dom/document.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DOCUMENT_NODE } from 'domconstants/constants';

import { setParentNode } from './utils.js';

import { childNodes, nodeName, ownerDocument } from './symbols.js';

import Attribute from './attribute.js';
import Comment from './text.js';
import DocumentFragment from './document.fragment.js';
import DocumentType from './document-type.js';
import Element from './element.js';
import Parent from './parent.js';
import Text from './text.js';

const doctype = Symbol();
const documentElement = Symbol();
const head = Symbol();
const body = Symbol();

export default class Document extends Parent {
constructor(type = 'html') {
super(DOCUMENT_NODE, null)[nodeName] = '#document';
this[doctype] = setParentNode(new DocumentType(type), this);
this[documentElement] = setParentNode(new Element('html', this), this);
this[head] = setParentNode(new Element('head', this), this[documentElement]);
this[body] = setParentNode(new Element('body', this), this[documentElement]);
this[childNodes] = type ?
[this[doctype], this[documentElement]] :
[this[documentElement]]
;
this[documentElement][childNodes] = [this[head], this[body]];
}

get doctype() {
return this[doctype];
}

get documentElement() {
return this[documentElement];
}

get head() {
return this[head];
}

get body() {
return this[body];
}

createAttribute(name) {
const attribute = new Attribute(name);
attribute[ownerDocument] = this;
return attribute;
}

createComment(data) {
return new Comment(data, this);
}

createDocumentFragment() {
return new DocumentFragment(this);
}

createElement(name, options = null) {
const element = new Element(name, this);
if (options?.is) element.setAttribute('is', options.is);
return element;
}

createTextNode(data) {
return new Text(data, this);
}

toString() {
return this[childNodes].join('');
}
}
Loading

0 comments on commit 1433d72

Please sign in to comment.