diff --git a/src/undom.js b/src/undom.js index af8988b..036239a 100644 --- a/src/undom.js +++ b/src/undom.js @@ -151,9 +151,32 @@ class Element extends Node { } +class Event { + constructor(type, opts) { + this.type = type; + this.bubbles = !!(opts && opts.bubbles); + this.cancelable = !!(opts && opts.cancelable); + } + stopPropagation() { + this._stop = true; + } + stopImmediatePropagation() { + this._end = this._stop = true; + } + preventDefault() { + this.defaultPrevented = true; + } +} + + class Document extends Element { constructor() { super(9, '#document'); // DOCUMENT_NODE + this.defaultView = new DefaultView(this); + } + + get document() { + return this; } createElement(type) { @@ -166,37 +189,28 @@ class Document extends Element { return element; } - createTextNode(text) { return new Text(text); } } +assign(Document.prototype, { Document, Node, Text, Element, SVGElement: Element, Event }); -class Event { - constructor(type, opts) { - this.type = type; - this.bubbles = !!(opts && opts.bubbles); - this.cancelable = !!(opts && opts.cancelable); - } - stopPropagation() { - this._stop = true; - } - stopImmediatePropagation() { - this._end = this._stop = true; - } - preventDefault() { - this.defaultPrevented = true; + +class DefaultView { + constructor(document) { + this.document = document; } } +assign(DefaultView.prototype, { Document, Node, Text, Element, SVGElement: Element, Event }); + /** Create a minimally viable DOM Document * @returns {Document} document */ export default function createDocument() { let document = new Document(); - assign(document, document.defaultView = { document, Document, Node, Text, Element, SVGElement: Element, Event }); document.appendChild( document.documentElement = document.createElement('html') );