@mml-io/observable-dom 0.2.0 → 0.4.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.
@@ -0,0 +1,24 @@
1
+ import { RemoteEvent } from "@mml-io/observable-dom-common";
2
+ import { DOMWindow } from "jsdom";
3
+ import { DOMRunnerFactory, DOMRunnerMessage } from "./ObservableDOM";
4
+ export declare const JSDOMRunnerFactory: DOMRunnerFactory;
5
+ export declare class JSDOMRunner {
6
+ private monkeyPatchMutationRecordCallback;
7
+ domWindow: DOMWindow | null;
8
+ private jsdom;
9
+ private callback;
10
+ private mutationObserver;
11
+ private htmlPath;
12
+ private documentStartTime;
13
+ private isLoaded;
14
+ private logBuffer;
15
+ constructor(htmlPath: string, htmlContents: string, params: object, callback: (domRunnerMessage: DOMRunnerMessage) => void);
16
+ private flushLogBuffer;
17
+ private log;
18
+ getDocument(): Document;
19
+ getWindow(): any;
20
+ dispose(): void;
21
+ getDocumentTime(): number;
22
+ dispatchRemoteEventFromConnectionId(connectionId: number, domNode: Element, remoteEvent: RemoteEvent): void;
23
+ private createVirtualConsole;
24
+ }
@@ -0,0 +1,51 @@
1
+ import { LogMessage, ObservableDOMInterface, ObservableDOMMessage, ObservableDOMParameters, RemoteEvent, StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
2
+ export type DOMRunnerMessage = {
3
+ loaded?: boolean;
4
+ mutationList?: Array<MutationRecord>;
5
+ logMessage?: LogMessage;
6
+ };
7
+ export type DOMRunnerInterface = {
8
+ getDocument(): Document;
9
+ getWindow(): Window & {
10
+ CustomEvent: typeof CustomEvent;
11
+ Text: typeof Text;
12
+ HTMLScriptElement: typeof HTMLScriptElement;
13
+ Comment: typeof Comment;
14
+ };
15
+ dispatchRemoteEventFromConnectionId(connectionId: number, realElement: Element, remoteEvent: RemoteEvent): void;
16
+ dispose(): void;
17
+ getDocumentTime(): number;
18
+ };
19
+ export type DOMRunnerFactory = (htmlPath: string, htmlContents: string, params: object, callback: (domRunnerMessage: DOMRunnerMessage) => void) => DOMRunnerInterface;
20
+ export type LiveVirtualDOMElement = Omit<StaticVirtualDOMElement, "childNodes"> & {
21
+ realElement: Element | Text;
22
+ childNodes: Array<LiveVirtualDOMElement>;
23
+ parent: LiveVirtualDOMElement | null;
24
+ };
25
+ export declare class ObservableDOM implements ObservableDOMInterface {
26
+ private nodeToNodeId;
27
+ private nodeIdToNode;
28
+ private realElementToVirtualElement;
29
+ private ignoreTextNodes;
30
+ private callback;
31
+ private nextNodeId;
32
+ private htmlPath;
33
+ private domRunner;
34
+ private documentTimeIntervalTimer;
35
+ constructor(observableDOMParameters: ObservableDOMParameters, callback: (message: ObservableDOMMessage, observableDOM: ObservableDOMInterface) => void, runnerFactory: DOMRunnerFactory);
36
+ addConnectedUserId(connectionId: number): void;
37
+ removeConnectedUserId(connectionId: number): void;
38
+ private processModificationList;
39
+ private addKnownNodesInMutation;
40
+ private removeKnownNodesInMutation;
41
+ private removeVirtualDOMElement;
42
+ private createVirtualDOMElementWithChildren;
43
+ private createVirtualDOMElement;
44
+ private getFirstNonIgnoredPreviousSibling;
45
+ private getVirtualDOMElementForRealElementOrThrow;
46
+ private isIgnoredElement;
47
+ private isIgnoredAttribute;
48
+ dispatchRemoteEventFromConnectionId(connectionId: number, remoteEvent: RemoteEvent): void;
49
+ dispose(): void;
50
+ private getDocumentTime;
51
+ }
package/build/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export * from "../src/index";
1
+ export * from "./ObservableDOM";
2
+ export * from "./JSDOMRunner";
@@ -0,0 +1 @@
1
+ {"program":{"fileNames":["../../../node_modules/typescript/lib/lib.es5.d.ts","../../../node_modules/typescript/lib/lib.es2015.d.ts","../../../node_modules/typescript/lib/lib.es2016.d.ts","../../../node_modules/typescript/lib/lib.es2017.d.ts","../../../node_modules/typescript/lib/lib.es2018.d.ts","../../../node_modules/typescript/lib/lib.es2019.d.ts","../../../node_modules/typescript/lib/lib.es2020.d.ts","../../../node_modules/typescript/lib/lib.dom.d.ts","../../../node_modules/typescript/lib/lib.dom.iterable.d.ts","../../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../../node_modules/typescript/lib/lib.es2019.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../../node_modules/typescript/lib/lib.es2020.date.d.ts","../../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../../node_modules/typescript/lib/lib.es2020.number.d.ts","../../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../../node_modules/typescript/lib/lib.decorators.d.ts","../../../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../networked-dom-protocol/build/networked-dom-v0.1/from-client.d.ts","../../networked-dom-protocol/build/networked-dom-v0.1/from-server.d.ts","../../networked-dom-protocol/build/networked-dom-v0.1/index.d.ts","../../networked-dom-protocol/build/index.d.ts","../../observable-dom-common/build/ObservableDOMInterface.d.ts","../../observable-dom-common/build/messages.d.ts","../../observable-dom-common/build/index.d.ts","../../../node_modules/@types/node/assert.d.ts","../../../node_modules/@types/node/assert/strict.d.ts","../../../node_modules/@types/node/globals.d.ts","../../../node_modules/@types/node/async_hooks.d.ts","../../../node_modules/@types/node/buffer.d.ts","../../../node_modules/@types/node/child_process.d.ts","../../../node_modules/@types/node/cluster.d.ts","../../../node_modules/@types/node/console.d.ts","../../../node_modules/@types/node/constants.d.ts","../../../node_modules/@types/node/crypto.d.ts","../../../node_modules/@types/node/dgram.d.ts","../../../node_modules/@types/node/diagnostics_channel.d.ts","../../../node_modules/@types/node/dns.d.ts","../../../node_modules/@types/node/dns/promises.d.ts","../../../node_modules/@types/node/domain.d.ts","../../../node_modules/@types/node/dom-events.d.ts","../../../node_modules/@types/node/events.d.ts","../../../node_modules/@types/node/fs.d.ts","../../../node_modules/@types/node/fs/promises.d.ts","../../../node_modules/@types/node/http.d.ts","../../../node_modules/@types/node/http2.d.ts","../../../node_modules/@types/node/https.d.ts","../../../node_modules/@types/node/inspector.d.ts","../../../node_modules/@types/node/module.d.ts","../../../node_modules/@types/node/net.d.ts","../../../node_modules/@types/node/os.d.ts","../../../node_modules/@types/node/path.d.ts","../../../node_modules/@types/node/perf_hooks.d.ts","../../../node_modules/@types/node/process.d.ts","../../../node_modules/@types/node/punycode.d.ts","../../../node_modules/@types/node/querystring.d.ts","../../../node_modules/@types/node/readline.d.ts","../../../node_modules/@types/node/readline/promises.d.ts","../../../node_modules/@types/node/repl.d.ts","../../../node_modules/@types/node/stream.d.ts","../../../node_modules/@types/node/stream/promises.d.ts","../../../node_modules/@types/node/stream/consumers.d.ts","../../../node_modules/@types/node/stream/web.d.ts","../../../node_modules/@types/node/string_decoder.d.ts","../../../node_modules/@types/node/test.d.ts","../../../node_modules/@types/node/timers.d.ts","../../../node_modules/@types/node/timers/promises.d.ts","../../../node_modules/@types/node/tls.d.ts","../../../node_modules/@types/node/trace_events.d.ts","../../../node_modules/@types/node/tty.d.ts","../../../node_modules/@types/node/url.d.ts","../../../node_modules/@types/node/util.d.ts","../../../node_modules/@types/node/v8.d.ts","../../../node_modules/@types/node/vm.d.ts","../../../node_modules/@types/node/wasi.d.ts","../../../node_modules/@types/node/worker_threads.d.ts","../../../node_modules/@types/node/zlib.d.ts","../../../node_modules/@types/node/globals.global.d.ts","../../../node_modules/@types/node/index.d.ts","../../../node_modules/parse5/dist/common/html.d.ts","../../../node_modules/parse5/dist/common/token.d.ts","../../../node_modules/parse5/dist/common/error-codes.d.ts","../../../node_modules/parse5/dist/tokenizer/preprocessor.d.ts","../../../node_modules/parse5/dist/tokenizer/index.d.ts","../../../node_modules/parse5/dist/tree-adapters/interface.d.ts","../../../node_modules/parse5/dist/parser/open-element-stack.d.ts","../../../node_modules/parse5/dist/parser/formatting-element-list.d.ts","../../../node_modules/parse5/dist/parser/index.d.ts","../../../node_modules/parse5/dist/tree-adapters/default.d.ts","../../../node_modules/parse5/dist/serializer/index.d.ts","../../../node_modules/parse5/dist/common/foreign-content.d.ts","../../../node_modules/parse5/dist/index.d.ts","../../../node_modules/@types/tough-cookie/index.d.ts","../node_modules/@types/jsdom/base.d.ts","../node_modules/@types/jsdom/index.d.ts","../../../node_modules/form-data/index.d.ts","../../../node_modules/@types/node-fetch/externals.d.ts","../../../node_modules/@types/node-fetch/index.d.ts","../src/utils.ts","../src/ObservableDOM.ts","../src/JSDOMRunner.ts","../src/index.ts"],"fileInfos":[{"version":"6a6b471e7e43e15ef6f8fe617a22ce4ecb0e34efa6c3dfcfe7cebd392bcca9d2","affectsGlobalScope":true},"45b7ab580deca34ae9729e97c13cfd999df04416a79116c3bfb483804f85ded4","dc48272d7c333ccf58034c0026162576b7d50ea0e69c3b9292f803fc20720fd5","27147504487dc1159369da4f4da8a26406364624fa9bc3db632f7d94a5bae2c3","5e1c4c362065a6b95ff952c0eab010f04dcd2c3494e813b493ecfd4fcb9fc0d8","68d73b4a11549f9c0b7d352d10e91e5dca8faa3322bfb77b661839c42b1ddec7","5efce4fc3c29ea84e8928f97adec086e3dc876365e0982cc8479a07954a3efd4",{"version":"fcd3ecc9f764f06f4d5c467677f4f117f6abf49dee6716283aa204ff1162498b","affectsGlobalScope":true},{"version":"9a60b92bca4c1257db03b349d58e63e4868cfc0d1c8d0ba60c2dbc63f4e6c9f6","affectsGlobalScope":true},{"version":"f296963760430fb65b4e5d91f0ed770a91c6e77455bacf8fa23a1501654ede0e","affectsGlobalScope":true},{"version":"5114a95689b63f96b957e00216bc04baf9e1a1782aa4d8ee7e5e9acbf768e301","affectsGlobalScope":true},{"version":"4443e68b35f3332f753eacc66a04ac1d2053b8b035a0e0ac1d455392b5e243b3","affectsGlobalScope":true},{"version":"ab22100fdd0d24cfc2cc59d0a00fc8cf449830d9c4030dc54390a46bd562e929","affectsGlobalScope":true},{"version":"f7bd636ae3a4623c503359ada74510c4005df5b36de7f23e1db8a5c543fd176b","affectsGlobalScope":true},{"version":"ce691fb9e5c64efb9547083e4a34091bcbe5bdb41027e310ebba8f7d96a98671","affectsGlobalScope":true},{"version":"8d697a2a929a5fcb38b7a65594020fcef05ec1630804a33748829c5ff53640d0","affectsGlobalScope":true},{"version":"0c20f4d2358eb679e4ae8a4432bdd96c857a2960fd6800b21ec4008ec59d60ea","affectsGlobalScope":true},{"version":"36ae84ccc0633f7c0787bc6108386c8b773e95d3b052d9464a99cd9b8795fbec","affectsGlobalScope":true},{"version":"82d0d8e269b9eeac02c3bd1c9e884e85d483fcb2cd168bccd6bc54df663da031","affectsGlobalScope":true},{"version":"b8deab98702588840be73d67f02412a2d45a417a3c097b2e96f7f3a42ac483d1","affectsGlobalScope":true},{"version":"4738f2420687fd85629c9efb470793bb753709c2379e5f85bc1815d875ceadcd","affectsGlobalScope":true},{"version":"2f11ff796926e0832f9ae148008138ad583bd181899ab7dd768a2666700b1893","affectsGlobalScope":true},{"version":"376d554d042fb409cb55b5cbaf0b2b4b7e669619493c5d18d5fa8bd67273f82a","affectsGlobalScope":true},{"version":"9fc46429fbe091ac5ad2608c657201eb68b6f1b8341bd6d670047d32ed0a88fa","affectsGlobalScope":true},{"version":"61c37c1de663cf4171e1192466e52c7a382afa58da01b1dc75058f032ddf0839","affectsGlobalScope":true},{"version":"c4138a3dd7cd6cf1f363ca0f905554e8d81b45844feea17786cdf1626cb8ea06","affectsGlobalScope":true},{"version":"6ff3e2452b055d8f0ec026511c6582b55d935675af67cdb67dd1dc671e8065df","affectsGlobalScope":true},{"version":"03de17b810f426a2f47396b0b99b53a82c1b60e9cba7a7edda47f9bb077882f4","affectsGlobalScope":true},{"version":"8184c6ddf48f0c98429326b428478ecc6143c27f79b79e85740f17e6feb090f1","affectsGlobalScope":true},{"version":"261c4d2cf86ac5a89ad3fb3fafed74cbb6f2f7c1d139b0540933df567d64a6ca","affectsGlobalScope":true},{"version":"6af1425e9973f4924fca986636ac19a0cf9909a7e0d9d3009c349e6244e957b6","affectsGlobalScope":true},{"version":"576711e016cf4f1804676043e6a0a5414252560eb57de9faceee34d79798c850","affectsGlobalScope":true},{"version":"89c1b1281ba7b8a96efc676b11b264de7a8374c5ea1e6617f11880a13fc56dc6","affectsGlobalScope":true},{"version":"15a630d6817718a2ddd7088c4f83e4673fde19fa992d2eae2cf51132a302a5d3","affectsGlobalScope":true},{"version":"b7e9f95a7387e3f66be0ed6db43600c49cec33a3900437ce2fd350d9b7cb16f2","affectsGlobalScope":true},{"version":"01e0ee7e1f661acedb08b51f8a9b7d7f959e9cdb6441360f06522cc3aea1bf2e","affectsGlobalScope":true},{"version":"ac17a97f816d53d9dd79b0d235e1c0ed54a8cc6a0677e9a3d61efb480b2a3e4e","affectsGlobalScope":true},{"version":"bf14a426dbbf1022d11bd08d6b8e709a2e9d246f0c6c1032f3b2edb9a902adbe","affectsGlobalScope":true},{"version":"ec0104fee478075cb5171e5f4e3f23add8e02d845ae0165bfa3f1099241fa2aa","affectsGlobalScope":true},{"version":"2b72d528b2e2fe3c57889ca7baef5e13a56c957b946906d03767c642f386bbc3","affectsGlobalScope":true},{"version":"9cc66b0513ad41cb5f5372cca86ef83a0d37d1c1017580b7dace3ea5661836df","affectsGlobalScope":true},{"version":"368af93f74c9c932edd84c58883e736c9e3d53cec1fe24c0b0ff451f529ceab1","affectsGlobalScope":true},{"version":"307c8b7ebbd7f23a92b73a4c6c0a697beca05b06b036c23a34553e5fe65e4fdc","affectsGlobalScope":true},{"version":"189c0703923150aa30673fa3de411346d727cc44a11c75d05d7cf9ef095daa22","affectsGlobalScope":true},{"version":"782dec38049b92d4e85c1585fbea5474a219c6984a35b004963b00beb1aab538","affectsGlobalScope":true},"328c09d87a15cde5936230f085c6888e6dc5f31a7c600a8a222419e28ea02812","e46f9c26b401a050e372280c49fd564d27dca4ca2b0e3e8585b07578661b2279","660b97032e7c3a5a094b60e3de0bf602626931fe0167dbad07e9ada9f683161e","28e083d1a14be292588f05b70cf7852deb63a3e0da35a0156bcbe361b5072562","4e9e7984ad6e180c5c3363d3478d71558d7d3949c45685951aef7e06e4d6b6a4","ffe0e53edfbab16e0c9bacf0d61b8091d2cf46fbca310d2602036793fda5a058","a63268b15ba72e389b1eaf9752ca501a2965923096ea28c2c98a4db1ec0e8a22","587f13f1e8157bd8cec0adda0de4ef558bb8573daa9d518d1e2af38e87ecc91f","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"bce910d9164785c9f0d4dcea4be359f5f92130c7c7833dea6138ab1db310a1f9","affectsGlobalScope":true},"7a435e0c814f58f23e9a0979045ec0ef5909aac95a70986e8bcce30c27dff228",{"version":"a7534271773a27ff7d136d550e86b41894d8090fa857ba4c02b5bb18d2eb1c8e","affectsGlobalScope":true},"db71be322f07f769200108aa19b79a75dd19a187c9dca2a30c4537b233aa2863","57135ce61976a8b1dadd01bb412406d1805b90db6e8ecb726d0d78e0b5f76050",{"version":"49479e21a040c0177d1b1bc05a124c0383df7a08a0726ad4d9457619642e875a","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","b8e431e9b9bb2dc832b23d4e3e02774e953d5537998923f215ea446169e9a61e","3690133deae19c8127c5505fcb67b04bdc9eb053796008538a9b9abbb70d85aa","5b1c0a23f464f894e7c2b2b6c56df7b9afa60ed48c5345f8618d389a636b2108","be2b092f2765222757c6441b86c53a5ea8dfed47bbc43eab4c5fe37942c866b3","8e6b05abc98adba15e1ac78e137c64576c74002e301d682e66feb77a23907ab8","1ca735bb3d407b2af4fbee7665f3a0a83be52168c728cc209755060ba7ed67bd",{"version":"6b526a5ec4a401ca7c26cfe6a48e641d8f30af76673bad3b06a1b4504594a960","affectsGlobalScope":true},{"version":"b85c02e14ecb2a873dad5a1de72319b265160ba48f1b83661aeb3bba1366c1bc","affectsGlobalScope":true},"7a2ba0c9af860ac3e77b35ed01fd96d15986f17aa22fe40f188ae556fb1070df","fc3764040518a1008dd04bdc80964591b566b896283e00df85c95851c1f46237","55709608060f77965c270ac10ac646286589f1bd1cb174fff1778a2dd9a7ef31","790623a47c5eda62910098884ecb154dc0e5f3a23fc36c1bfb3b5b9ed44e2c2d","42b40e40f2a358cda332456214fad311e1806a6abf3cebaaac72496e07556642","354612fe1d49ecc9551ea3a27d94eef2887b64ef4a71f72ca444efe0f2f0ba80",{"version":"125af9d85cb9d5e508353f10a8d52f01652d2d48b2cea54789a33e5b4d289c1c","affectsGlobalScope":true},"f5490f53d40291cc8607f5463434d1ac6c5564bc4fbb03abceb03a8f6b014457","5e2b91328a540a0933ab5c2203f4358918e6f0fe7505d22840a891a6117735f1","3abc3512fa04aa0230f59ea1019311fd8667bd935d28306311dccc8b17e79d5d",{"version":"14a50dafe3f45713f7f27cb6320dff07c6ac31678f07959c2134260061bf91ff","affectsGlobalScope":true},{"version":"19da7150ca062323b1db6311a6ef058c9b0a39cc64d836b5e9b75d301869653b","affectsGlobalScope":true},"1349077576abb41f0e9c78ec30762ff75b710208aff77f5fdcc6a8c8ce6289dd","e2ce82603102b5c0563f59fb40314cc1ff95a4d521a66ad14146e130ea80d89c","a3e0395220255a350aa9c6d56f882bfcb5b85c19fddf5419ec822cf22246a26d","c27b01e8ddff5cd280711af5e13aecd9a3228d1c256ea797dd64f8fdec5f7df5","898840e876dfd21843db9f2aa6ae38ba2eab550eb780ff62b894b9fbfebfae6b","0cab4d7d4edc40cd3af9eea7c3ed6d1016910c0954c49c4297e479bf3822a625","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff","785e5be57d4f20f290a20e7b0c6263f6c57fd6e51283050756cef07d6d651c68","44b8b584a338b190a59f4f6929d072431950c7bd92ec2694821c11bce180c8a5","164deb2409ac5f4da3cd139dbcee7f7d66753d90363a4d7e2db8d8874f272270","1fb6c5ec52332a8b531a8d7a5300ac9301f98c4fe62f68e744e0841ccba65e7e",{"version":"ab294c4b7279318ee2a8fdf681305457ecc05970c94108d304933f18823eeac1","affectsGlobalScope":true},"ad08154d9602429522cac965a715fde27d421d69b24756c5d291877dda75353e","bbda6ea452a2386093a1eda18a6e26a989e98869f1b9f37e46f510a986d2e740","812b25f798033c202baedf386a1ccc41f9191b122f089bffd10fdccce99fba11","993325544790073f77e945bee046d53988c0bc3ac5695c9cf8098166feb82661",{"version":"75dd741ca6a6c8d2437a6ca8349b64b816421dbf9fe82dd026afaba965576962","affectsGlobalScope":true},{"version":"8799401a7ab57764f0d464513a7fa7c72e1d70a226b172ec60fff534ea94d108","affectsGlobalScope":true},"2ce2210032ccaff7710e2abf6a722e62c54960458e73e356b6a365c93ab6ca66","92db194ef7d208d5e4b6242a3434573fd142a621ff996d84cc9dbba3553277d0","16a3080e885ed52d4017c902227a8d0d8daf723d062bec9e45627c6fdcd6699b",{"version":"0bd9543cd8fc0959c76fb8f4f5a26626c2ed62ef4be98fd857bce268066db0a2","affectsGlobalScope":true},"1ca6858a0cbcd74d7db72d7b14c5360a928d1d16748a55ecfa6bfaff8b83071b",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"247aa3419c98713231952b33801d4f46563fe542e03604acd8c63ac45a32409c","3411c785dbe8fd42f7d644d1e05a7e72b624774a08a9356479754999419c3c5a","8fb8fdda477cd7382477ffda92c2bb7d9f7ef583b1aa531eb6b2dc2f0a206c10","66995b0c991b5c5d42eff1d950733f85482c7419f7296ab8952e03718169e379","33f3795a4617f98b1bb8dac36312119d02f31897ae75436a1e109ce042b48ee8","2850c9c5dc28d34ad5f354117d0419f325fc8932d2a62eadc4dc52c018cd569b","c753948f7e0febe7aa1a5b71a714001a127a68861309b2c4127775aa9b6d4f24","3e7a40e023e1d4a9eef1a6f08a3ded8edacb67ae5fce072014205d730f717ba5","a77be6fc44c876bc10c897107f84eaba10790913ebdcad40fcda7e47469b2160","382100b010774614310d994bbf16cc9cd291c14f0d417126c7a7cfad1dc1d3f8","91f5dbcdb25d145a56cffe957ec665256827892d779ef108eb2f3864faff523b","4fdf56315340bd1770eb52e1601c3a98e45b1d207202831357e99ce29c35b55c","927955a3de5857e0a1c575ced5a4245e74e6821d720ed213141347dd1870197f","be6fd74528b32986fbf0cd2cfa9192a5ed7f369060b32a7adcb0c8d055708e61","cc256fd958b33576ed32c7338c64adb0d08fc0c2c6525010202fab83f32745da","768ccf11f71d2131a09a558604432211227d8fffd886b5755d18031f8381656e",{"version":"227a50113c72f6566f4adcc9deca7b5c5c9f865cb85af0eec561b8465240e62a","affectsGlobalScope":true},"736097ddbb2903bef918bb3b5811ef1c9c5656f2a73bd39b22a91b9cc2525e50","626bccaba2f61f03abe558a39501631565389a748bc47dd52b305c80176333c1","3663d1b50f356656a314e5df169bb51cb9d5fd75905fa703f75db6bb32030568","9489ab4bd4c50c5593c80ca3134c8975386d54f3ebee47ba2ba4689d15a5b320","dc5850ccd49e5bcc218816fca42296381883fa3e92958e1419fdc82bc00eab43","e70a6c2bfdae652426436d529870d3122d455c33e68728d08e5dcec83d7a0351","9b38307f1f74c1a4babfc4bb957bd86736847b89f63c4df126c638afe8c6dd25"],"root":[[126,129]],"options":{"allowSyntheticDefaultImports":true,"downlevelIteration":true,"jsx":2,"module":99,"noImplicitAny":true,"outDir":"./","sourceMap":true,"strictNullChecks":true,"target":2},"fileIdsList":[[99],[72,98,99,106,123,124],[53,99],[56,99],[57,62,90,99],[58,69,70,77,87,98,99],[58,59,69,77,99],[60,99],[61,62,70,78,99],[62,87,95,99],[63,65,69,77,99],[64,99],[65,66,99],[69,99],[67,69,99],[69,70,71,87,98,99],[69,70,71,84,87,90,99],[99,103],[65,69,72,77,87,98,99],[69,70,72,73,77,87,95,98,99],[72,74,87,95,98,99],[53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105],[69,75,99],[76,98,99],[65,69,77,87,99],[78,99],[79,99],[56,80,99],[81,97,99,103],[82,99],[83,99],[69,84,85,99],[84,86,99,101],[57,69,87,88,89,90,99],[57,87,89,99],[87,88,99],[90,99],[91,99],[87,99],[69,93,94,99],[93,94,99],[62,77,87,95,99],[96,99],[77,97,99],[57,72,83,98,99],[62,99],[87,99,100],[99,101],[99,102],[57,62,69,71,80,87,98,99,101,103],[87,99,104],[72,87,99,106],[99,108],[99,107,108],[99,107],[99,107,108,109,111,112,115,116,117,118],[99,108,112],[99,107,108,109,111,112,113,114],[99,107,112],[99,112,116],[99,108,109,110],[99,109],[99,107,108,112],[48,99],[46,47,99],[49,99],[49,50,51,99],[49,50,99],[69,99,101,106,119,120,122],[99,121],[52,99,101,121,122,125,127],[52,99,126],[99,127,128],[52,99,127]],"referencedMap":[[124,1],[125,2],[53,3],[54,3],[56,4],[57,5],[58,6],[59,7],[60,8],[61,9],[62,10],[63,11],[64,12],[65,13],[66,13],[68,14],[67,15],[69,14],[70,16],[71,17],[55,18],[105,1],[72,19],[73,20],[74,21],[106,22],[75,23],[76,24],[77,25],[78,26],[79,27],[80,28],[81,29],[82,30],[83,31],[84,32],[85,32],[86,33],[87,34],[89,35],[88,36],[90,37],[91,38],[92,39],[93,40],[94,41],[95,42],[96,43],[97,44],[98,45],[99,46],[100,47],[101,48],[102,49],[103,50],[104,51],[120,1],[123,52],[109,53],[118,54],[107,1],[108,55],[119,56],[114,57],[115,58],[113,59],[117,60],[111,61],[110,62],[116,63],[112,54],[44,1],[45,1],[8,1],[9,1],[11,1],[10,1],[2,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[3,1],[4,1],[23,1],[20,1],[21,1],[22,1],[24,1],[25,1],[26,1],[5,1],[27,1],[28,1],[29,1],[30,1],[6,1],[34,1],[31,1],[32,1],[33,1],[35,1],[7,1],[36,1],[41,1],[42,1],[37,1],[38,1],[39,1],[40,1],[1,1],[43,1],[49,64],[46,1],[47,1],[48,65],[50,66],[52,67],[51,68],[121,69],[122,70],[128,71],[127,72],[129,73],[126,74]],"exportedModulesMap":[[124,1],[125,2],[53,3],[54,3],[56,4],[57,5],[58,6],[59,7],[60,8],[61,9],[62,10],[63,11],[64,12],[65,13],[66,13],[68,14],[67,15],[69,14],[70,16],[71,17],[55,18],[105,1],[72,19],[73,20],[74,21],[106,22],[75,23],[76,24],[77,25],[78,26],[79,27],[80,28],[81,29],[82,30],[83,31],[84,32],[85,32],[86,33],[87,34],[89,35],[88,36],[90,37],[91,38],[92,39],[93,40],[94,41],[95,42],[96,43],[97,44],[98,45],[99,46],[100,47],[101,48],[102,49],[103,50],[104,51],[120,1],[123,52],[109,53],[118,54],[107,1],[108,55],[119,56],[114,57],[115,58],[113,59],[117,60],[111,61],[110,62],[116,63],[112,54],[44,1],[45,1],[8,1],[9,1],[11,1],[10,1],[2,1],[12,1],[13,1],[14,1],[15,1],[16,1],[17,1],[18,1],[19,1],[3,1],[4,1],[23,1],[20,1],[21,1],[22,1],[24,1],[25,1],[26,1],[5,1],[27,1],[28,1],[29,1],[30,1],[6,1],[34,1],[31,1],[32,1],[33,1],[35,1],[7,1],[36,1],[41,1],[42,1],[37,1],[38,1],[39,1],[40,1],[1,1],[43,1],[49,64],[46,1],[47,1],[48,65],[50,66],[52,67],[51,68],[121,69],[122,70],[128,71],[127,72],[129,73],[126,74]],"semanticDiagnosticsPerFile":[124,125,53,54,56,57,58,59,60,61,62,63,64,65,66,68,67,69,70,71,55,105,72,73,74,106,75,76,77,78,79,80,81,82,83,84,85,86,87,89,88,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,120,123,109,118,107,108,119,114,115,113,117,111,110,116,112,44,45,8,9,11,10,2,12,13,14,15,16,17,18,19,3,4,23,20,21,22,24,25,26,5,27,28,29,30,6,34,31,32,33,35,7,36,41,42,37,38,39,40,1,43,49,46,47,48,50,52,51,121,122,128,127,129,126],"affectedFilesPendingEmit":[128,127,129,126]},"version":"5.0.4"}
@@ -0,0 +1,3 @@
1
+ import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
2
+ import { LiveVirtualDOMElement } from "./ObservableDOM";
3
+ export declare function virtualDOMElementToStatic(el: LiveVirtualDOMElement): StaticVirtualDOMElement;
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "@mml-io/observable-dom",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [
7
- "/build",
8
- "/src"
7
+ "/build"
9
8
  ],
10
9
  "scripts": {
11
10
  "type-check": "tsc --noEmit",
@@ -16,7 +15,7 @@
16
15
  "lint-fix": "eslint \"./src/**/*.{js,jsx,ts,tsx}\" --fix"
17
16
  },
18
17
  "dependencies": {
19
- "@mml-io/observable-dom-common": "^0.2.0",
18
+ "@mml-io/observable-dom-common": "^0.4.0",
20
19
  "jsdom": "22.1.0",
21
20
  "node-fetch": "2.6.11"
22
21
  },
@@ -1,274 +0,0 @@
1
- import vm from "vm";
2
-
3
- import { LogMessage, RemoteEvent } from "@mml-io/observable-dom-common";
4
- import { AbortablePromise, DOMWindow, JSDOM, ResourceLoader, VirtualConsole } from "jsdom";
5
- import * as nodeFetch from "node-fetch";
6
- import nodeFetchFn from "node-fetch";
7
-
8
- import { DOMRunnerFactory, DOMRunnerInterface, DOMRunnerMessage } from "./ObservableDOM";
9
-
10
- const ErrDOMWindowNotInitialized = "DOMWindow not initialized";
11
-
12
- // TODO - remove this monkeypatching if it's possible to avoid the race conditions in naive MutationObserver usage
13
- const monkeyPatchedMutationRecordCallbacks = new Set<() => void>();
14
- function installMutationObserverMonkeyPatch() {
15
- /*
16
- This monkey patch replaces the `createImpl` exported function implementation in the `MutationRecord` class in JSDOM
17
- to insert an iteration through callbacks that are therefore fired before a subsequent MutationRecord is created.
18
- This provides an opportunity to invoke the MutationObservers with a single MutationRecord rather than multiple.
19
-
20
- This is necessary as (at least intuitive) usage of the MutationObserver API does not enable creating accurate
21
- incremental diffs as the handling of all-but-the-last MutationRecord in a list requires collecting state from the
22
- DOM that has been since been mutated further. (e.g. if an attribute is changed twice in a row the first event cannot
23
- discover the intermediate value of the attribute as it can only query the latest DOM state). Whilst this simple case
24
- is solvable by walking backwards through the list of MutationRecords and using `oldValue` there are cases where adding
25
- child elements with the correct attributes is not possible when handling intermediate diffs.
26
- */
27
- // eslint-disable-next-line @typescript-eslint/no-var-requires
28
- const MutationRecordExports = require("jsdom/lib/jsdom/living/generated/MutationRecord");
29
- const originalCreateImpl = MutationRecordExports.createImpl;
30
- // This overwrites the function property on the exports that mutation-observers.js uses to create MutationRecords.
31
- MutationRecordExports.createImpl = (...args: any[]) => {
32
- for (const callback of monkeyPatchedMutationRecordCallbacks) {
33
- callback();
34
- }
35
- return originalCreateImpl.call(null, ...args);
36
- };
37
- }
38
- let monkeyPatchInstalled = false;
39
-
40
- export const JSDOMRunnerFactory: DOMRunnerFactory = (
41
- htmlPath: string,
42
- htmlContents: string,
43
- params: object,
44
- callback: (mutationList: DOMRunnerMessage) => void,
45
- ): DOMRunnerInterface => {
46
- return new JSDOMRunner(htmlPath, htmlContents, params, callback);
47
- };
48
-
49
- // This is used to stop JSDOM trying to load resources
50
- class RejectionResourceLoader extends ResourceLoader {
51
- public fetch(url: string): AbortablePromise<Buffer> | null {
52
- console.error("RejectionResourceLoader.fetch", url);
53
- return null;
54
- }
55
- }
56
-
57
- export class JSDOMRunner {
58
- private monkeyPatchMutationRecordCallback: () => void;
59
-
60
- public domWindow: DOMWindow | null = null;
61
- private jsdom: JSDOM;
62
-
63
- private callback: (message: DOMRunnerMessage) => void;
64
- private mutationObserver: MutationObserver | null = null;
65
- private htmlPath: string;
66
-
67
- private documentStartTime = Date.now();
68
-
69
- private isLoaded = false;
70
- private logBuffer: LogMessage[] = [];
71
-
72
- constructor(
73
- htmlPath: string,
74
- htmlContents: string,
75
- params: object,
76
- callback: (domRunnerMessage: DOMRunnerMessage) => void,
77
- ) {
78
- this.htmlPath = htmlPath;
79
- this.callback = callback;
80
-
81
- if (!monkeyPatchInstalled) {
82
- installMutationObserverMonkeyPatch();
83
- monkeyPatchInstalled = true;
84
- }
85
-
86
- this.monkeyPatchMutationRecordCallback = () => {
87
- /*
88
- This is called before every creation of a MutationRecord so that it can be used to process an existing record to
89
- avoid handling multiple MutationRecords at a time (see comment at the top of this file).
90
- */
91
- const records = this.mutationObserver?.takeRecords();
92
- if (records && records.length > 1) {
93
- throw new Error(
94
- "The monkey patching should have prevented more than one record being handled at a time",
95
- );
96
- } else if (records && records.length > 0) {
97
- this.callback({
98
- mutationList: records,
99
- });
100
- }
101
- };
102
- monkeyPatchedMutationRecordCallbacks.add(this.monkeyPatchMutationRecordCallback);
103
-
104
- this.jsdom = new JSDOM(htmlContents, {
105
- runScripts: "dangerously",
106
- resources: new RejectionResourceLoader(),
107
- url: this.htmlPath,
108
- virtualConsole: this.createVirtualConsole(),
109
- beforeParse: (window) => {
110
- this.domWindow = window;
111
-
112
- this.domWindow.fetch = nodeFetchFn as unknown as typeof fetch;
113
- this.domWindow.Headers = nodeFetch.Headers as unknown as typeof Headers;
114
- this.domWindow.Request = nodeFetch.Request as unknown as typeof Request;
115
- this.domWindow.Response = nodeFetch.Response as unknown as typeof Response;
116
-
117
- // This is a polyfill for https://developer.mozilla.org/en-US/docs/Web/API/Document/timeline
118
- const timeline = {};
119
- Object.defineProperty(timeline, "currentTime", {
120
- get: () => {
121
- return this.getDocumentTime();
122
- },
123
- });
124
- (window.document as any).timeline = timeline;
125
-
126
- // JSON stringify and parse to avoid potential reference leaks from the params object
127
- window.params = JSON.parse(JSON.stringify(params));
128
-
129
- this.mutationObserver = new window.MutationObserver((mutationList) => {
130
- this.callback({
131
- mutationList,
132
- });
133
- });
134
-
135
- window.addEventListener("load", () => {
136
- this.mutationObserver?.observe(window.document, {
137
- attributes: true,
138
- childList: true,
139
- subtree: true,
140
- characterData: true,
141
- });
142
-
143
- this.isLoaded = true;
144
-
145
- this.callback({
146
- loaded: true,
147
- });
148
-
149
- this.flushLogBuffer();
150
- });
151
- },
152
- });
153
- }
154
-
155
- private flushLogBuffer() {
156
- for (const logMessage of this.logBuffer) {
157
- this.callback({
158
- logMessage,
159
- });
160
- }
161
-
162
- this.logBuffer = [];
163
- }
164
-
165
- private log(message: LogMessage) {
166
- if (!this.isLoaded) {
167
- this.logBuffer.push(message);
168
- return;
169
- }
170
-
171
- this.callback({
172
- logMessage: message,
173
- });
174
- }
175
-
176
- public getDocument(): Document {
177
- if (!this.domWindow) {
178
- throw new Error(ErrDOMWindowNotInitialized);
179
- }
180
-
181
- return this.domWindow.document;
182
- }
183
-
184
- public getWindow(): any {
185
- return this.domWindow;
186
- }
187
-
188
- public dispose() {
189
- const records = this.mutationObserver?.takeRecords();
190
- this.callback({
191
- mutationList: records,
192
- });
193
- monkeyPatchedMutationRecordCallbacks.delete(this.monkeyPatchMutationRecordCallback);
194
- this.mutationObserver?.disconnect();
195
- this.jsdom.window.close();
196
- }
197
-
198
- public getDocumentTime() {
199
- return Date.now() - this.documentStartTime;
200
- }
201
-
202
- public dispatchRemoteEventFromConnectionId(
203
- connectionId: number,
204
- domNode: Element,
205
- remoteEvent: RemoteEvent,
206
- ) {
207
- if (!this.domWindow) {
208
- throw new Error(ErrDOMWindowNotInitialized);
209
- }
210
-
211
- const bubbles = remoteEvent.bubbles || false;
212
- const remoteEventObject = new this.domWindow.CustomEvent(remoteEvent.name, {
213
- bubbles,
214
- detail: { ...remoteEvent.params, connectionId },
215
- });
216
-
217
- const eventTypeLowerCase = remoteEvent.name.toLowerCase();
218
-
219
- // TODO - check if there are other events that automatically wire up similarly to click->onclick and avoid those too
220
- if (eventTypeLowerCase !== "click") {
221
- const handlerAttributeName = "on" + eventTypeLowerCase;
222
- const handlerAttributeValue = domNode.getAttribute(handlerAttributeName);
223
- if (handlerAttributeValue) {
224
- // This event is defined as an HTML event attribute.
225
- const script = handlerAttributeValue;
226
- const vmContext = this.jsdom.getInternalVMContext();
227
- try {
228
- const invoke = vm.runInContext(`(function(event){ ${script} })`, vmContext);
229
- Reflect.apply(invoke, domNode, [remoteEventObject]);
230
- } catch (e) {
231
- console.error("Error running event handler:", e);
232
- }
233
- }
234
- }
235
-
236
- // Dispatch the event via JavaScript.
237
- domNode.dispatchEvent(remoteEventObject);
238
- }
239
-
240
- private createVirtualConsole(): VirtualConsole {
241
- const virtualConsole = new VirtualConsole();
242
- virtualConsole.on("jsdomError", (...args) => {
243
- this.log({
244
- level: "system",
245
- content: args,
246
- });
247
- });
248
- virtualConsole.on("error", (...args) => {
249
- this.log({
250
- level: "error",
251
- content: args,
252
- });
253
- });
254
- virtualConsole.on("warn", (...args) => {
255
- this.log({
256
- level: "warn",
257
- content: args,
258
- });
259
- });
260
- virtualConsole.on("log", (...args) => {
261
- this.log({
262
- level: "log",
263
- content: args,
264
- });
265
- });
266
- virtualConsole.on("info", (...args) => {
267
- this.log({
268
- level: "info",
269
- content: args,
270
- });
271
- });
272
- return virtualConsole;
273
- }
274
- }
@@ -1,454 +0,0 @@
1
- import {
2
- LogMessage,
3
- ObservableDOMInterface,
4
- ObservableDOMMessage,
5
- ObservableDOMParameters,
6
- RemoteEvent,
7
- StaticVirtualDOMElement,
8
- StaticVirtualDOMMutationIdsRecord,
9
- } from "@mml-io/observable-dom-common";
10
-
11
- import { virtualDOMElementToStatic } from "./utils";
12
-
13
- export type DOMRunnerMessage = {
14
- loaded?: boolean;
15
- mutationList?: Array<MutationRecord>;
16
- logMessage?: LogMessage;
17
- };
18
-
19
- export type DOMRunnerInterface = {
20
- getDocument(): Document;
21
- getWindow(): Window & {
22
- CustomEvent: typeof CustomEvent;
23
- Text: typeof Text;
24
- HTMLScriptElement: typeof HTMLScriptElement;
25
- Comment: typeof Comment;
26
- }; // TODO - Define this without using JSDOM types
27
- dispatchRemoteEventFromConnectionId(
28
- connectionId: number,
29
- realElement: Element,
30
- remoteEvent: RemoteEvent,
31
- ): void;
32
- dispose(): void;
33
- getDocumentTime(): number;
34
- };
35
-
36
- export type DOMRunnerFactory = (
37
- htmlPath: string,
38
- htmlContents: string,
39
- params: object,
40
- callback: (domRunnerMessage: DOMRunnerMessage) => void,
41
- ) => DOMRunnerInterface;
42
-
43
- export type LiveVirtualDOMElement = Omit<StaticVirtualDOMElement, "childNodes"> & {
44
- realElement: Element | Text;
45
- childNodes: Array<LiveVirtualDOMElement>;
46
- parent: LiveVirtualDOMElement | null;
47
- };
48
-
49
- export class ObservableDOM implements ObservableDOMInterface {
50
- private nodeToNodeId = new Map<LiveVirtualDOMElement, number>();
51
- private nodeIdToNode = new Map<number, LiveVirtualDOMElement>();
52
- private realElementToVirtualElement = new Map<Element | Text, LiveVirtualDOMElement>();
53
- private ignoreTextNodes = true;
54
- private callback: (message: ObservableDOMMessage, observableDOM: ObservableDOMInterface) => void;
55
- private nextNodeId = 1;
56
- private htmlPath: string;
57
- private domRunner: DOMRunnerInterface;
58
-
59
- private documentTimeIntervalTimer: NodeJS.Timer;
60
-
61
- constructor(
62
- observableDOMParameters: ObservableDOMParameters,
63
- callback: (message: ObservableDOMMessage, observableDOM: ObservableDOMInterface) => void,
64
- runnerFactory: DOMRunnerFactory,
65
- ) {
66
- this.htmlPath = observableDOMParameters.htmlPath;
67
- this.ignoreTextNodes = observableDOMParameters.ignoreTextNodes;
68
- this.callback = callback;
69
-
70
- this.documentTimeIntervalTimer = setInterval(() => {
71
- this.callback(
72
- {
73
- documentTime: this.getDocumentTime(),
74
- },
75
- this,
76
- );
77
- }, observableDOMParameters.pingIntervalMilliseconds || 5000);
78
-
79
- this.domRunner = runnerFactory(
80
- observableDOMParameters.htmlPath,
81
- observableDOMParameters.htmlContents,
82
- observableDOMParameters.params,
83
- (domRunnerMessage: DOMRunnerMessage) => {
84
- if (domRunnerMessage.loaded) {
85
- this.createVirtualDOMElementWithChildren(
86
- this.domRunner.getDocument() as unknown as Element,
87
- null,
88
- );
89
-
90
- const snapshot = virtualDOMElementToStatic(
91
- this.getVirtualDOMElementForRealElementOrThrow(
92
- this.domRunner.getDocument() as unknown as Element,
93
- ),
94
- );
95
-
96
- this.callback(
97
- {
98
- snapshot,
99
- documentTime: this.getDocumentTime(),
100
- },
101
- this,
102
- );
103
- } else if (domRunnerMessage.mutationList) {
104
- this.processModificationList(domRunnerMessage.mutationList);
105
- } else if (domRunnerMessage.logMessage) {
106
- this.callback(
107
- {
108
- logMessage: domRunnerMessage.logMessage,
109
- documentTime: this.getDocumentTime(),
110
- },
111
- this,
112
- );
113
- }
114
- },
115
- );
116
- }
117
-
118
- public addConnectedUserId(connectionId: number): void {
119
- this.domRunner.getWindow().dispatchEvent(
120
- new (this.domRunner.getWindow().CustomEvent)("connected", {
121
- detail: { connectionId },
122
- }),
123
- );
124
- }
125
-
126
- public removeConnectedUserId(connectionId: number): void {
127
- this.domRunner.getWindow().dispatchEvent(
128
- new (this.domRunner.getWindow().CustomEvent)("disconnected", {
129
- detail: { connectionId },
130
- }),
131
- );
132
- }
133
-
134
- private processModificationList(mutationList: Array<MutationRecord>): void {
135
- const documentEl = this.domRunner.getDocument() as unknown as Element;
136
- const documentVirtualDOMElement = this.realElementToVirtualElement.get(documentEl);
137
- if (!documentVirtualDOMElement) {
138
- throw new Error(`document not created in processModificationList`);
139
- }
140
-
141
- if (mutationList.length > 1) {
142
- // TODO - walk back through the records to derive the intermediate states (e.g. if an attribute is later added to
143
- // an element created in an earlier record then it should not have that attribute when the element is added.
144
- // This is important as incorrect attribute sets can affect visibility and expected client performance.
145
- console.error(
146
- "More than one mutation record received. It is possible that intermediate states are incorrect.",
147
- );
148
- }
149
-
150
- for (const mutation of mutationList) {
151
- if (this.isIgnoredElement(mutation.target as Element | Text)) {
152
- continue;
153
- }
154
-
155
- if (
156
- mutation.type === "attributes" &&
157
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
158
- this.isIgnoredAttribute(mutation.target as Element | Text, mutation.attributeName!)
159
- ) {
160
- continue;
161
- }
162
-
163
- this.addKnownNodesInMutation(mutation);
164
-
165
- // Convert the "real" DOM MutationRecord into a "virtual" DOM MutationRecord that references the VirtualDOMElements
166
- // This is done so that the same process for handling mutations can be used for both changes to a live DOM and also
167
- // to diffs between DOM snapshots when reloading
168
- const firstNonIgnoredPreviousSibling = mutation.previousSibling
169
- ? this.getFirstNonIgnoredPreviousSibling(mutation.previousSibling as Element | Text)
170
- : null;
171
- const targetElement = this.getVirtualDOMElementForRealElementOrThrow(
172
- mutation.target as Element | Text,
173
- );
174
- const addedNodes: Array<StaticVirtualDOMElement> = [];
175
- for (const node of mutation.addedNodes) {
176
- if (this.isIgnoredElement(node as Element | Text)) {
177
- continue;
178
- }
179
- const virtualDOMElement = this.getVirtualDOMElementForRealElementOrThrow(
180
- node as Element | Text,
181
- );
182
- addedNodes.push(virtualDOMElementToStatic(virtualDOMElement));
183
- }
184
-
185
- const removedNodeIds: Array<number> = [];
186
- for (const node of mutation.removedNodes) {
187
- if (this.isIgnoredElement(node as Element | Text)) {
188
- continue;
189
- }
190
- const virtualDOMElement = this.getVirtualDOMElementForRealElementOrThrow(
191
- node as Element | Text,
192
- );
193
- removedNodeIds.push(virtualDOMElement.nodeId);
194
- }
195
-
196
- const mutationRecord: StaticVirtualDOMMutationIdsRecord = {
197
- type: mutation.type,
198
- targetId: targetElement.nodeId,
199
- addedNodes,
200
- removedNodeIds,
201
- previousSiblingId:
202
- firstNonIgnoredPreviousSibling !== null
203
- ? this.getVirtualDOMElementForRealElementOrThrow(firstNonIgnoredPreviousSibling).nodeId
204
- : null,
205
- attribute: mutation.attributeName
206
- ? {
207
- attributeName: mutation.attributeName,
208
- value: (mutation.target as Element).getAttribute(mutation.attributeName),
209
- }
210
- : null,
211
- };
212
-
213
- this.callback(
214
- {
215
- mutation: mutationRecord,
216
- documentTime: this.getDocumentTime(),
217
- },
218
- this,
219
- );
220
-
221
- this.removeKnownNodesInMutation(mutation);
222
- }
223
- }
224
-
225
- private addKnownNodesInMutation(mutation: MutationRecord): void {
226
- const targetNode = mutation.target as Element | Text;
227
- const virtualDOMElement = this.realElementToVirtualElement.get(targetNode);
228
- if (!virtualDOMElement) {
229
- throw new Error(
230
- "Unknown node in addKnownNodesInMutation:" + targetNode + "," + mutation.type,
231
- );
232
- }
233
- if (mutation.type === "childList") {
234
- let previousSibling = mutation.previousSibling;
235
- let index = 0;
236
- while (previousSibling && this.isIgnoredElement(previousSibling as Element | Text)) {
237
- previousSibling = previousSibling.previousSibling;
238
- }
239
- if (previousSibling) {
240
- const previousSiblingElement = this.realElementToVirtualElement.get(
241
- previousSibling as Element | Text,
242
- );
243
- if (!previousSiblingElement) {
244
- throw new Error("Unknown previous sibling");
245
- }
246
- index = virtualDOMElement.childNodes.indexOf(previousSiblingElement);
247
- if (index === -1) {
248
- throw new Error("Previous sibling is not currently a child of the parent element");
249
- }
250
- index += 1;
251
- }
252
- mutation.addedNodes.forEach((node: Node) => {
253
- const asElementOrText = node as Element | Text;
254
- const childVirtualDOMElement = this.createVirtualDOMElementWithChildren(
255
- asElementOrText,
256
- virtualDOMElement,
257
- );
258
- if (childVirtualDOMElement) {
259
- if (virtualDOMElement.childNodes.indexOf(childVirtualDOMElement) === -1) {
260
- virtualDOMElement.childNodes.splice(index, 0, childVirtualDOMElement);
261
- index++;
262
- }
263
- }
264
- });
265
- } else if (mutation.type === "attributes") {
266
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
267
- const attributeName = mutation.attributeName!;
268
- if (this.isIgnoredAttribute(targetNode, attributeName)) {
269
- return;
270
- }
271
- const attributeValue = (targetNode as Element).getAttribute(attributeName);
272
- if (attributeValue === null) {
273
- delete virtualDOMElement.attributes[attributeName];
274
- } else {
275
- virtualDOMElement.attributes[attributeName] = attributeValue;
276
- }
277
- } else if (mutation.type === "characterData") {
278
- virtualDOMElement.textContent = targetNode.textContent ? targetNode.textContent : undefined;
279
- }
280
- }
281
-
282
- private removeKnownNodesInMutation(mutation: MutationRecord): void {
283
- const targetNode = mutation.target as Element | Text;
284
- const virtualDOMElement = this.realElementToVirtualElement.get(targetNode);
285
- if (!virtualDOMElement) {
286
- throw new Error("Unknown node in mutation list:" + targetNode + ", " + mutation.type);
287
- }
288
- if (mutation.type === "childList") {
289
- for (const node of mutation.removedNodes) {
290
- const asElementOrText = node as Element | Text;
291
- if (this.isIgnoredElement(asElementOrText)) {
292
- continue;
293
- }
294
- const childDOMElement = this.realElementToVirtualElement.get(asElementOrText);
295
- if (!childDOMElement) {
296
- console.warn(this.htmlPath, "Unknown node in removeKnownNodesInMutation");
297
- continue;
298
- } else {
299
- this.removeVirtualDOMElement(childDOMElement);
300
- const index = virtualDOMElement.childNodes.indexOf(childDOMElement);
301
- virtualDOMElement.childNodes.splice(index, 1);
302
- }
303
- }
304
- return;
305
- }
306
- }
307
-
308
- private removeVirtualDOMElement(virtualDOMElement: LiveVirtualDOMElement): void {
309
- this.nodeIdToNode.delete(virtualDOMElement.nodeId);
310
- this.nodeToNodeId.delete(virtualDOMElement);
311
- this.realElementToVirtualElement.delete(virtualDOMElement.realElement);
312
- for (const child of virtualDOMElement.childNodes) {
313
- this.removeVirtualDOMElement(child);
314
- }
315
- }
316
-
317
- private createVirtualDOMElementWithChildren(
318
- node: Element | Text,
319
- parent: LiveVirtualDOMElement | null,
320
- ): LiveVirtualDOMElement | null {
321
- const virtualElement = this.createVirtualDOMElement(node, parent);
322
- if (!virtualElement) {
323
- return null;
324
- }
325
- if ((node as Element).childNodes) {
326
- for (let i = 0; i < (node as Element).childNodes.length; i++) {
327
- const child = (node as Element).childNodes[i];
328
- const childVirtualElement = this.createVirtualDOMElementWithChildren(
329
- child as Element | Text,
330
- virtualElement,
331
- );
332
- if (childVirtualElement) {
333
- virtualElement.childNodes.push(childVirtualElement);
334
- }
335
- }
336
- }
337
-
338
- return virtualElement;
339
- }
340
-
341
- private createVirtualDOMElement(
342
- node: Element | Text,
343
- parent: LiveVirtualDOMElement | null,
344
- ): LiveVirtualDOMElement | null {
345
- if (this.isIgnoredElement(node)) {
346
- return null;
347
- }
348
- const existingValue = this.realElementToVirtualElement.get(node);
349
- if (existingValue !== undefined) {
350
- throw new Error("Node already has a virtual element: " + node.nodeName);
351
- }
352
- if (!node) {
353
- throw new Error("Cannot assign node id to null");
354
- }
355
-
356
- const attributes: { [key: string]: string } = {};
357
- if ((node as any).attributes) {
358
- const asHTMLElement = node as HTMLElement;
359
- for (const key of asHTMLElement.getAttributeNames()) {
360
- const value = asHTMLElement.getAttribute(key);
361
- if (value === null) {
362
- throw new Error("Null attribute value for key: " + key);
363
- }
364
- if (!this.isIgnoredAttribute(node, key)) {
365
- attributes[key] = value;
366
- }
367
- }
368
- }
369
-
370
- const nodeId = this.nextNodeId++;
371
- const virtualElement: LiveVirtualDOMElement = {
372
- nodeId,
373
- tag: node.nodeName,
374
- attributes,
375
- childNodes: [],
376
- realElement: node,
377
- parent,
378
- };
379
- if (node instanceof this.domRunner.getWindow().Text && node.textContent) {
380
- virtualElement.textContent = node.textContent;
381
- }
382
- this.nodeToNodeId.set(virtualElement, nodeId);
383
- this.nodeIdToNode.set(nodeId, virtualElement);
384
- this.realElementToVirtualElement.set(node, virtualElement);
385
- return virtualElement;
386
- }
387
-
388
- private getFirstNonIgnoredPreviousSibling(node: Element | Text): Element | Text | null {
389
- let currentNode = node;
390
- if (!this.isIgnoredElement(currentNode)) {
391
- return currentNode;
392
- }
393
- while (currentNode && currentNode.previousSibling) {
394
- currentNode = currentNode.previousSibling as Element | Text;
395
- if (!this.isIgnoredElement(currentNode)) {
396
- return currentNode;
397
- }
398
- }
399
- return null;
400
- }
401
-
402
- private getVirtualDOMElementForRealElementOrThrow(
403
- realElement: Element | Text,
404
- ): LiveVirtualDOMElement {
405
- const virtualElement = this.realElementToVirtualElement.get(realElement);
406
- if (!virtualElement) {
407
- throw new Error(`Virtual element not found for real element`);
408
- }
409
- return virtualElement;
410
- }
411
-
412
- private isIgnoredElement(node: Element | Text): boolean {
413
- if (this.ignoreTextNodes && node instanceof this.domRunner.getWindow().Text) {
414
- return true;
415
- } else if (node instanceof this.domRunner.getWindow().HTMLScriptElement) {
416
- return true;
417
- } else if (node instanceof this.domRunner.getWindow().Comment) {
418
- return true;
419
- }
420
- return false;
421
- }
422
-
423
- private isIgnoredAttribute(node: Element | Text, attributeName: string): boolean {
424
- return attributeName.startsWith("on");
425
- }
426
-
427
- public dispatchRemoteEventFromConnectionId(connectionId: number, remoteEvent: RemoteEvent): void {
428
- const domNode = this.nodeIdToNode.get(remoteEvent.nodeId);
429
- if (!domNode) {
430
- console.error("Unknown node ID in remote event: " + remoteEvent.nodeId);
431
- return;
432
- }
433
-
434
- if (domNode instanceof this.domRunner.getWindow().Text) {
435
- console.warn("Cannot dispatch remote event to text node");
436
- return;
437
- }
438
-
439
- this.domRunner.dispatchRemoteEventFromConnectionId(
440
- connectionId,
441
- domNode.realElement as Element,
442
- remoteEvent,
443
- );
444
- }
445
-
446
- public dispose() {
447
- clearInterval(this.documentTimeIntervalTimer);
448
- this.domRunner.dispose();
449
- }
450
-
451
- private getDocumentTime() {
452
- return this.domRunner.getDocumentTime();
453
- }
454
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./ObservableDOM";
2
- export * from "./JSDOMRunner";
package/src/utils.ts DELETED
@@ -1,13 +0,0 @@
1
- import { StaticVirtualDOMElement } from "@mml-io/observable-dom-common";
2
-
3
- import { LiveVirtualDOMElement } from "./ObservableDOM";
4
-
5
- export function virtualDOMElementToStatic(el: LiveVirtualDOMElement): StaticVirtualDOMElement {
6
- return {
7
- nodeId: el.nodeId,
8
- tag: el.tag,
9
- attributes: el.attributes,
10
- childNodes: el.childNodes.map((child) => virtualDOMElementToStatic(child)),
11
- textContent: el.textContent,
12
- };
13
- }