Web Inspector: implement undo for setOuterHTML via undo-ing nested primitive commands.
[WebKit-https.git] / LayoutTests / inspector / elements / set-outer-html-test.js
1 var initialize_SetOuterHTMLTest = function() {
2
3 InspectorTest.events = [];
4 InspectorTest.containerId;
5
6 InspectorTest.setUpTestSuite = function(next)
7 {
8     InspectorTest.expandElementsTree(step1);
9
10     function step1()
11     {
12         InspectorTest.selectNodeWithId("container", step2);
13     }
14
15     function step2(node)
16     {
17         InspectorTest.containerId = node.id;
18         DOMAgent.getOuterHTML(InspectorTest.containerId, step3);
19     }
20
21     function step3(error, text)
22     {
23         InspectorTest.containerText = text;
24
25         for (var key in WebInspector.DOMAgent.Events) {
26             var eventName = WebInspector.DOMAgent.Events[key];
27             WebInspector.domAgent.addEventListener(eventName, InspectorTest.recordEvent.bind(InspectorTest, eventName));
28         }
29
30         next();
31     }
32 }
33
34 InspectorTest.recordEvent = function(eventName, event)
35 {
36     var node = event.data.node || event.data;
37     var parent = event.data.parent;
38     for (var currentNode = parent || node; currentNode; currentNode = currentNode.parentNode) {
39         if (currentNode.getAttribute("id") === "output")
40             return;
41     }
42     InspectorTest.events.push("Event " + eventName + ": " + node.nodeName());
43 }
44
45 InspectorTest.patchOuterHTML = function(pattern, replacement, next)
46 {
47     InspectorTest.addResult("Replacing '" + pattern + "' with '" + replacement + "'\n");
48     InspectorTest.setOuterHTML(InspectorTest.containerText.replace(pattern, replacement), next);
49 }
50
51 InspectorTest.patchOuterHTMLUseUndo = function(pattern, replacement, next)
52 {
53     InspectorTest.addResult("Replacing '" + pattern + "' with '" + replacement + "'\n");
54     InspectorTest.setOuterHTMLUseUndo(InspectorTest.containerText.replace(pattern, replacement), next);
55 }
56
57 InspectorTest.setOuterHTML = function(newText, next)
58 {
59     InspectorTest.innerSetOuterHTML(newText, false, bringBack);
60
61     function bringBack()
62     {
63         InspectorTest.addResult("\nBringing things back\n");
64         InspectorTest.innerSetOuterHTML(InspectorTest.containerText, true, next);
65     }
66 }
67
68 InspectorTest.setOuterHTMLUseUndo = function(newText, next)
69 {
70     InspectorTest.innerSetOuterHTML(newText, false, bringBack);
71
72     function bringBack()
73     {
74         InspectorTest.addResult("\nBringing things back\n");
75         DOMAgent.undo(InspectorTest._dumpOuterHTML.bind(InspectorTest, true, next));
76     }
77 }
78
79 InspectorTest.innerSetOuterHTML = function(newText, last, next)
80 {
81     DOMAgent.setOuterHTML(InspectorTest.containerId, newText, InspectorTest._dumpOuterHTML.bind(InspectorTest, last, next));
82 }
83
84 InspectorTest._dumpOuterHTML = function(last, next)
85 {
86     RuntimeAgent.evaluate("document.getElementById(\"identity\").wrapperIdentity", dumpIdentity);
87     function dumpIdentity(error, result)
88     {
89         InspectorTest.addResult("Wrapper identity: " + result.value);
90         InspectorTest.events.sort();
91         for (var i = 0; i < InspectorTest.events.length; ++i)
92             InspectorTest.addResult(InspectorTest.events[i]);
93         InspectorTest.events = [];
94     }
95
96     DOMAgent.getOuterHTML(InspectorTest.containerId, callback);
97
98     function callback(error, text)
99     {
100         InspectorTest.addResult("==========8<==========");
101         InspectorTest.addResult(text);
102         InspectorTest.addResult("==========>8==========");
103         if (last)
104             InspectorTest.addResult("\n\n\n");
105         next();
106     }
107 }
108
109 };