summaryrefslogtreecommitdiff
path: root/Source/WebCore/Scripts/DumpEditingHistory.js
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
commit1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch)
tree46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/Scripts/DumpEditingHistory.js
parent32761a6cee1d0dee366b885b7b9c777e67885688 (diff)
downloadWebKitGtk-tarball-master.tar.gz
Diffstat (limited to 'Source/WebCore/Scripts/DumpEditingHistory.js')
-rw-r--r--Source/WebCore/Scripts/DumpEditingHistory.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/Source/WebCore/Scripts/DumpEditingHistory.js b/Source/WebCore/Scripts/DumpEditingHistory.js
new file mode 100644
index 000000000..16b1d81b2
--- /dev/null
+++ b/Source/WebCore/Scripts/DumpEditingHistory.js
@@ -0,0 +1,82 @@
+(() => {
+ let initialized = false;
+ let globalNodeMap = new EditingHistory.GlobalNodeMap();
+ let topLevelUpdates = [];
+ let currentChildUpdates = [];
+ let isProcessingTopLevelUpdate = false;
+ let lastKnownSelectionState = null;
+ let mutationObserver = new MutationObserver(records => appendDOMUpdatesFromRecords(records));
+
+ function beginProcessingTopLevelUpdate() {
+ isProcessingTopLevelUpdate = true;
+ }
+
+ function endProcessingTopLevelUpdate(topLevelUpdate) {
+ topLevelUpdates.push(topLevelUpdate);
+ currentChildUpdates = [];
+ isProcessingTopLevelUpdate = false;
+ }
+
+ function appendDOMUpdatesFromRecords(records) {
+ if (!records.length)
+ return;
+
+ let newUpdates = EditingHistory.DOMUpdate.fromRecords(records, globalNodeMap);
+ if (isProcessingTopLevelUpdate)
+ currentChildUpdates = currentChildUpdates.concat(newUpdates);
+ else
+ topLevelUpdates = topLevelUpdates.concat(newUpdates);
+ }
+
+ function appendSelectionUpdateIfNecessary() {
+ let newSelectionState = EditingHistory.SelectionState.fromSelection(getSelection(), globalNodeMap);
+ if (newSelectionState.isEqual(lastKnownSelectionState))
+ return;
+
+ let update = new EditingHistory.SelectionUpdate(globalNodeMap, newSelectionState);
+ if (isProcessingTopLevelUpdate)
+ currentChildUpdates.push(update);
+ else
+ topLevelUpdates.push(update);
+ lastKnownSelectionState = newSelectionState;
+ }
+
+ document.body.addEventListener("focus", () => {
+ if (initialized)
+ return;
+
+ initialized = true;
+
+ EditingHistory.getEditingHistoryAsJSONString = (formatted) => {
+ let record = {};
+ record.updates = topLevelUpdates.map(update => update.toObject());
+ record.globalNodeMap = globalNodeMap.toObject();
+ return formatted ? JSON.stringify(record, null, 4) : JSON.stringify(record);
+ };
+
+ document.addEventListener("selectionchange", () => {
+ appendSelectionUpdateIfNecessary();
+ });
+
+ document.addEventListener("beforeinput", event => {
+ appendDOMUpdatesFromRecords(mutationObserver.takeRecords());
+ beginProcessingTopLevelUpdate();
+ });
+
+ document.addEventListener("input", event => {
+ appendDOMUpdatesFromRecords(mutationObserver.takeRecords());
+ let eventData = event.dataTransfer ? event.dataTransfer.getData("text/html") : event.data;
+ lastKnownSelectionState = null;
+ endProcessingTopLevelUpdate(new EditingHistory.InputEventUpdate(globalNodeMap, currentChildUpdates, event.inputType, eventData, event.timeStamp));
+ });
+
+ mutationObserver.observe(document, {
+ childList: true,
+ attributes: true,
+ characterData: true,
+ subtree: true,
+ attributeOldValue: true,
+ characterDataOldValue: true,
+ });
+ });
+})();