pseudo-dom 0.12.0 → 0.15.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,70 +1,82 @@
1
- 'use strict'
2
-
3
- const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
- return mod && mod.__esModule
5
- ? mod
6
- : {
7
- default: mod
8
- }
9
- }
10
- Object.defineProperty(exports, '__esModule', {
11
- value: true
12
- })
13
- const EventTargetService_1 = __importDefault(require('../services/EventTargetService'))
14
- const NodeService_1 = require('../services/NodeService')
15
- const ElementService_1 = require('../services/ElementService')
16
- const HTMLElementService_1 = require('../services/HTMLElementService')
17
- const PseudoHTMLDocument_1 = __importDefault(require('../classes/PseudoHTMLDocument'))
18
- /**
19
- * Construct the Pseudo Dom to provide access to Dom objects which are otherwise not available outside the browser
20
- * context.
21
- * @function generateDocument
22
- * @param {Object} root
23
- * @param {Object} context
24
- * @returns {Window|PseudoEventTarget}
25
- */
26
- const generateDocument = (root, context = {}) => {
27
- /**
28
- *
29
- * @type {Window|PseudoEventTarget}
30
- */
31
- const newWindow = typeof root.document === 'undefined' ? root : new EventTargetService_1.default()
32
- /**
33
- * @type {Node|PseudoNode}
34
- */
35
- const Node = root.Node || new NodeService_1.NodeService()
36
- if (typeof newWindow.Node === 'undefined') {
37
- newWindow.Node = Node
38
- }
39
- /**
40
- *
41
- * @type {Element|PseudoElement}
42
- */
43
- const Element = root.Element || new ElementService_1.ElementService()
44
- if (typeof newWindow.Element === 'undefined') {
45
- newWindow.Element = Element
46
- }
47
- /**
48
- * Create an instance of HTMLElement if not available
49
- * @type {HTMLElement|PseudoHTMLElement}
50
- */
51
- const HTMLElement = root.HTMLElement || new HTMLElementService_1.HTMLElementService()
52
- if (typeof newWindow.HTMLElement === 'undefined') {
53
- newWindow.HTMLElement = HTMLElement
54
- }
55
- /**
56
- * Define document when not available
57
- * @type {Document|PseudoHTMLDocument}
58
- */
59
- const document = root.document || new PseudoHTMLDocument_1.default()
60
- if (typeof newWindow.document === 'undefined') {
61
- newWindow.document = document
62
- }
63
- // When there was no real document, newWindow is root itself (see above), so every assignment above already
64
- // mutated it directly. If context is also that same object (root and context given as one and the same, as
65
- // installGlobal does) merging is already done - doing it again would be Object.assign(target, target), which
66
- // throws on any getter-only own property target already has (globalThis.crypto, in Node).
67
- const mergeInto = context || root
68
- return newWindow === mergeInto ? newWindow : Object.assign(mergeInto, newWindow)
69
- }
70
- exports.default = generateDocument
1
+ 'use strict'
2
+
3
+ const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule
5
+ ? mod
6
+ : {
7
+ default: mod
8
+ }
9
+ }
10
+ Object.defineProperty(exports, '__esModule', {
11
+ value: true
12
+ })
13
+ const EventTargetService_1 = __importDefault(require('../services/EventTargetService'))
14
+ const NodeService_1 = require('../services/NodeService')
15
+ const ElementService_1 = require('../services/ElementService')
16
+ const HTMLElementService_1 = require('../services/HTMLElementService')
17
+ const PseudoHTMLDocument_1 = __importDefault(require('../classes/PseudoHTMLDocument'))
18
+ /**
19
+ * Construct the Pseudo Dom to provide access to Dom objects which are otherwise not available outside the browser
20
+ * context.
21
+ * @function generateDocument
22
+ * @param {Object} root
23
+ * @param {Object} context
24
+ * @returns {Window|PseudoEventTarget}
25
+ */
26
+ const generateDocument = (root, context = {}) => {
27
+ /**
28
+ *
29
+ * @type {Window|PseudoEventTarget}
30
+ */
31
+ const newWindow = typeof root.document === 'undefined' ? root : new EventTargetService_1.default()
32
+ /**
33
+ * The Node class itself (matching the DOM's window.Node), not an instance - the right-hand side of `instanceof`
34
+ * must be a constructor, so `x instanceof Node` needs this, not `new PseudoNode()`.
35
+ * @type {Function}
36
+ */
37
+ const Node = root.Node || NodeService_1.NodeService
38
+ if (typeof newWindow.Node === 'undefined') {
39
+ newWindow.Node = Node
40
+ }
41
+ /**
42
+ * The Element class itself, for the same reason as Node.
43
+ * @type {Function}
44
+ */
45
+ const Element = root.Element || ElementService_1.ElementService
46
+ if (typeof newWindow.Element === 'undefined') {
47
+ newWindow.Element = Element
48
+ }
49
+ /**
50
+ * The HTMLElement class itself, for the same reason as Node.
51
+ * @type {Function}
52
+ */
53
+ const HTMLElement = root.HTMLElement || HTMLElementService_1.HTMLElementService
54
+ if (typeof newWindow.HTMLElement === 'undefined') {
55
+ newWindow.HTMLElement = HTMLElement
56
+ }
57
+ /**
58
+ * The HTMLDocument class itself, for the same reason as Node (so `document instanceof HTMLDocument`, a common
59
+ * real-DOM-detection check, works).
60
+ * @type {Function}
61
+ */
62
+ const HTMLDocument = root.HTMLDocument || PseudoHTMLDocument_1.default
63
+ if (typeof newWindow.HTMLDocument === 'undefined') {
64
+ newWindow.HTMLDocument = HTMLDocument
65
+ }
66
+ /**
67
+ * Define document when not available - a real instance, unlike the classes above (window.document IS an object,
68
+ * not a constructor).
69
+ * @type {Document|PseudoHTMLDocument}
70
+ */
71
+ const document = root.document || new PseudoHTMLDocument_1.default()
72
+ if (typeof newWindow.document === 'undefined') {
73
+ newWindow.document = document
74
+ }
75
+ // When there was no real document, newWindow is root itself (see above), so every assignment above already
76
+ // mutated it directly. If context is also that same object (root and context given as one and the same, as
77
+ // installGlobal does) merging is already done - doing it again would be Object.assign(target, target), which
78
+ // throws on any getter-only own property target already has (globalThis.crypto, in Node).
79
+ const mergeInto = context || root
80
+ return newWindow === mergeInto ? newWindow : Object.assign(mergeInto, newWindow)
81
+ }
82
+ exports.default = generateDocument
@@ -1 +1 @@
1
- "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const EventTargetService_1=__importDefault(require("../services/EventTargetService")),NodeService_1=require("../services/NodeService"),ElementService_1=require("../services/ElementService"),HTMLElementService_1=require("../services/HTMLElementService"),PseudoHTMLDocument_1=__importDefault(require("../classes/PseudoHTMLDocument")),generateDocument=(e,t={})=>{const r=void 0===e.document?e:new EventTargetService_1.default,n=e.Node||new NodeService_1.NodeService;void 0===r.Node&&(r.Node=n);const c=e.Element||new ElementService_1.ElementService;void 0===r.Element&&(r.Element=c);const o=e.HTMLElement||new HTMLElementService_1.HTMLElementService;void 0===r.HTMLElement&&(r.HTMLElement=o);const i=e.document||new PseudoHTMLDocument_1.default;void 0===r.document&&(r.document=i);const u=t||e;return r===u?r:Object.assign(u,r)};exports.default=generateDocument;
1
+ "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const EventTargetService_1=__importDefault(require("../services/EventTargetService")),NodeService_1=require("../services/NodeService"),ElementService_1=require("../services/ElementService"),HTMLElementService_1=require("../services/HTMLElementService"),PseudoHTMLDocument_1=__importDefault(require("../classes/PseudoHTMLDocument")),generateDocument=(e,t={})=>{const n=void 0===e.document?e:new EventTargetService_1.default,r=e.Node||NodeService_1.NodeService;void 0===n.Node&&(n.Node=r);const o=e.Element||ElementService_1.ElementService;void 0===n.Element&&(n.Element=o);const c=e.HTMLElement||HTMLElementService_1.HTMLElementService;void 0===n.HTMLElement&&(n.HTMLElement=c);const i=e.HTMLDocument||PseudoHTMLDocument_1.default;void 0===n.HTMLDocument&&(n.HTMLDocument=i);const u=e.document||new PseudoHTMLDocument_1.default;void 0===n.document&&(n.document=u);const m=t||e;return n===m?n:Object.assign(m,n)};exports.default=generateDocument;
@@ -1,14 +1,15 @@
1
- /**
2
- * When there is no real DOM already available (a real browser, or a jsdom-based test environment - isBrowser is
3
- * true for both, since jsdom's window has a document too, and a jsdom document should never be overwritten, real
4
- * DOM behaviour is always closer to the real thing), populate the given global-like target with document / Node /
5
- * Element / HTMLElement, generated by generateDocument, so code written against a real DOM's globals works as-is.
6
- * Does nothing when a real DOM is already there, matching generateDocument's own only-fill-what-is-missing
7
- * behaviour - calling this is always safe, in any environment.
8
- * @memberOf module:factories
9
- * @param {*} [target] The object to add document / Node / Element / HTMLElement onto - defaults to globalThis, so
10
- * bare `document`, `Node`, etc. resolve from anywhere once this has run
11
- * @returns {*} The same target, for convenience
12
- */
13
- declare const installGlobal: (target?: any) => any;
14
- export default installGlobal;
1
+ /**
2
+ * When there is no real DOM already available (a real browser, or a jsdom-based test environment - isBrowser is
3
+ * true for both, since jsdom's window has a document too, and a jsdom document should never be overwritten, real
4
+ * DOM behaviour is always closer to the real thing), populate the given global-like target with document / Node /
5
+ * Element / HTMLElement / HTMLDocument, generated by generateDocument, plus window (a self-reference, matching a
6
+ * real browser's global scope - some code checks for a bare `window` before falling back to `global`), so code
7
+ * written against a real DOM's globals works as-is. Does nothing when a real DOM is already there, matching
8
+ * generateDocument's own only-fill-what-is-missing behaviour - calling this is always safe, in any environment.
9
+ * @memberOf module:factories
10
+ * @param {*} [target] The object to add document / Node / Element / HTMLElement / window onto - defaults to
11
+ * globalThis, so bare `document`, `Node`, `window`, etc. resolve from anywhere once this has run
12
+ * @returns {*} The same target, for convenience
13
+ */
14
+ declare const installGlobal: (target?: any) => any;
15
+ export default installGlobal;
@@ -1,40 +1,44 @@
1
- 'use strict'
2
-
3
- const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
- return mod && mod.__esModule
5
- ? mod
6
- : {
7
- default: mod
8
- }
9
- }
10
- Object.defineProperty(exports, '__esModule', {
11
- value: true
12
- })
13
- /**
14
- * @file Makes pseudo-dom a drop-in swap for a real DOM: code written against bare globals (document.createElement,
15
- * new Node(), ...) works unmodified, in the browser or in Node, without an if (typeof document === 'undefined')
16
- * check at every call site.
17
- * @author Joshua Heagle <joshuaheagle@gmail.com>
18
- * @version 1.0.0
19
- */
20
- const browser_or_node_1 = require('browser-or-node')
21
- const generateDocument_1 = __importDefault(require('./generateDocument'))
22
- /**
23
- * When there is no real DOM already available (a real browser, or a jsdom-based test environment - isBrowser is
24
- * true for both, since jsdom's window has a document too, and a jsdom document should never be overwritten, real
25
- * DOM behaviour is always closer to the real thing), populate the given global-like target with document / Node /
26
- * Element / HTMLElement, generated by generateDocument, so code written against a real DOM's globals works as-is.
27
- * Does nothing when a real DOM is already there, matching generateDocument's own only-fill-what-is-missing
28
- * behaviour - calling this is always safe, in any environment.
29
- * @memberOf module:factories
30
- * @param {*} [target] The object to add document / Node / Element / HTMLElement onto - defaults to globalThis, so
31
- * bare `document`, `Node`, etc. resolve from anywhere once this has run
32
- * @returns {*} The same target, for convenience
33
- */
34
- const installGlobal = (target = typeof globalThis !== 'undefined' ? globalThis : {}) => {
35
- if (!browser_or_node_1.isBrowser) {
36
- (0, generateDocument_1.default)(target, target)
37
- }
38
- return target
39
- }
40
- exports.default = installGlobal
1
+ 'use strict'
2
+
3
+ const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
4
+ return mod && mod.__esModule
5
+ ? mod
6
+ : {
7
+ default: mod
8
+ }
9
+ }
10
+ Object.defineProperty(exports, '__esModule', {
11
+ value: true
12
+ })
13
+ /**
14
+ * @file Makes pseudo-dom a drop-in swap for a real DOM: code written against bare globals (document.createElement,
15
+ * new Node(), ...) works unmodified, in the browser or in Node, without an if (typeof document === 'undefined')
16
+ * check at every call site.
17
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
18
+ * @version 1.0.0
19
+ */
20
+ const browser_or_node_1 = require('browser-or-node')
21
+ const generateDocument_1 = __importDefault(require('./generateDocument'))
22
+ /**
23
+ * When there is no real DOM already available (a real browser, or a jsdom-based test environment - isBrowser is
24
+ * true for both, since jsdom's window has a document too, and a jsdom document should never be overwritten, real
25
+ * DOM behaviour is always closer to the real thing), populate the given global-like target with document / Node /
26
+ * Element / HTMLElement / HTMLDocument, generated by generateDocument, plus window (a self-reference, matching a
27
+ * real browser's global scope - some code checks for a bare `window` before falling back to `global`), so code
28
+ * written against a real DOM's globals works as-is. Does nothing when a real DOM is already there, matching
29
+ * generateDocument's own only-fill-what-is-missing behaviour - calling this is always safe, in any environment.
30
+ * @memberOf module:factories
31
+ * @param {*} [target] The object to add document / Node / Element / HTMLElement / window onto - defaults to
32
+ * globalThis, so bare `document`, `Node`, `window`, etc. resolve from anywhere once this has run
33
+ * @returns {*} The same target, for convenience
34
+ */
35
+ const installGlobal = (target = typeof globalThis !== 'undefined' ? globalThis : {}) => {
36
+ if (!browser_or_node_1.isBrowser) {
37
+ (0, generateDocument_1.default)(target, target)
38
+ if (typeof target.window === 'undefined') {
39
+ target.window = target
40
+ }
41
+ }
42
+ return target
43
+ }
44
+ exports.default = installGlobal
@@ -1 +1 @@
1
- "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const browser_or_node_1=require("browser-or-node"),generateDocument_1=__importDefault(require("./generateDocument")),installGlobal=(e=("undefined"!=typeof globalThis?globalThis:{}))=>(browser_or_node_1.isBrowser||(0,generateDocument_1.default)(e,e),e);exports.default=installGlobal;
1
+ "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});const browser_or_node_1=require("browser-or-node"),generateDocument_1=__importDefault(require("./generateDocument")),installGlobal=(e=("undefined"!=typeof globalThis?globalThis:{}))=>(browser_or_node_1.isBrowser||((0,generateDocument_1.default)(e,e),void 0===e.window&&(e.window=e)),e);exports.default=installGlobal;
@@ -1,9 +1,9 @@
1
- /**
2
- * Print a node's markup to the console, indented for readability (see prettyPrint).
3
- * @memberOf module:factories
4
- * @param {*} node
5
- * @param {string} [label=''] A label printed above the markup, to identify this log call
6
- * @returns {undefined}
7
- */
8
- declare const logElement: (node: any, label?: string) => void;
9
- export default logElement;
1
+ /**
2
+ * Print a node's markup to the console, indented for readability (see prettyPrint).
3
+ * @memberOf module:factories
4
+ * @param {*} node
5
+ * @param {string} [label=''] A label printed above the markup, to identify this log call
6
+ * @returns {undefined}
7
+ */
8
+ declare const logElement: (node: any, label?: string) => void;
9
+ export default logElement;
@@ -1,23 +1,23 @@
1
- 'use strict'
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- })
6
- /**
7
- * @file Prints an element (or any node) to the console, readably - the point of pseudo-dom running headlessly is
8
- * usually to watch what code does to a tree without a real browser to look at.
9
- * @author Joshua Heagle <joshuaheagle@gmail.com>
10
- * @version 1.0.0
11
- */
12
- const serializeHTML_1 = require('./serializeHTML')
13
- /**
14
- * Print a node's markup to the console, indented for readability (see prettyPrint).
15
- * @memberOf module:factories
16
- * @param {*} node
17
- * @param {string} [label=''] A label printed above the markup, to identify this log call
18
- * @returns {undefined}
19
- */
20
- const logElement = (node, label = '') => {
21
- console.log(`${label ? `${label}\n` : ''}${(0, serializeHTML_1.prettyPrint)(node)}`)
22
- }
23
- exports.default = logElement
1
+ 'use strict'
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ })
6
+ /**
7
+ * @file Prints an element (or any node) to the console, readably - the point of pseudo-dom running headlessly is
8
+ * usually to watch what code does to a tree without a real browser to look at.
9
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
10
+ * @version 1.0.0
11
+ */
12
+ const serializeHTML_1 = require('./serializeHTML')
13
+ /**
14
+ * Print a node's markup to the console, indented for readability (see prettyPrint).
15
+ * @memberOf module:factories
16
+ * @param {*} node
17
+ * @param {string} [label=''] A label printed above the markup, to identify this log call
18
+ * @returns {undefined}
19
+ */
20
+ const logElement = (node, label = '') => {
21
+ console.log(`${label ? `${label}\n` : ''}${(0, serializeHTML_1.prettyPrint)(node)}`)
22
+ }
23
+ exports.default = logElement
@@ -1,25 +1,25 @@
1
- /**
2
- * A node's children, serialized in order (this is what innerHTML returns).
3
- * @memberOf module:factories
4
- * @param {*} node
5
- * @returns {string}
6
- */
7
- declare const serializeChildren: (node: any) => string;
8
- /**
9
- * An element itself, serialized with its children (this is what outerHTML returns).
10
- * @memberOf module:factories
11
- * @param {*} element
12
- * @returns {string}
13
- */
14
- declare const serializeOuter: (element: any) => string;
15
- /**
16
- * A node's markup, indented one level per level of nesting - a real DOM's outerHTML / innerHTML has no line breaks
17
- * at all, which does not read well for a whole tree (a board full of cells, say). Useful for watching pseudo-dom-
18
- * driven code run headlessly, printed to a terminal - see also logElement, which does the printing too.
19
- * @memberOf module:factories
20
- * @param {*} node
21
- * @param {string} [indent=' '] The indentation used per level of nesting
22
- * @returns {string}
23
- */
24
- declare const prettyPrint: (node: any, indent?: string) => string;
25
- export { serializeChildren, serializeOuter, prettyPrint };
1
+ /**
2
+ * A node's children, serialized in order (this is what innerHTML returns).
3
+ * @memberOf module:factories
4
+ * @param {*} node
5
+ * @returns {string}
6
+ */
7
+ declare const serializeChildren: (node: any) => string;
8
+ /**
9
+ * An element itself, serialized with its children (this is what outerHTML returns).
10
+ * @memberOf module:factories
11
+ * @param {*} element
12
+ * @returns {string}
13
+ */
14
+ declare const serializeOuter: (element: any) => string;
15
+ /**
16
+ * A node's markup, indented one level per level of nesting - a real DOM's outerHTML / innerHTML has no line breaks
17
+ * at all, which does not read well for a whole tree (a board full of cells, say). Useful for watching pseudo-dom-
18
+ * driven code run headlessly, printed to a terminal - see also logElement, which does the printing too.
19
+ * @memberOf module:factories
20
+ * @param {*} node
21
+ * @param {string} [indent=' '] The indentation used per level of nesting
22
+ * @returns {string}
23
+ */
24
+ declare const prettyPrint: (node: any, indent?: string) => string;
25
+ export { serializeChildren, serializeOuter, prettyPrint };