summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/inspector/debugger/script-formatter.html
blob: 79bbe8b97d33e71c9e011ac52fe04bc70d2f50d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
106
<html>
<head>
<script src="../../http/tests/inspector/inspector-test.js"></script>
<script src="../../http/tests/inspector/debugger-test.js"></script>
<script src="resources/obfuscated.js"></script>

<script>

var test = function()
{
    var panel = WebInspector.panels.sources;
    var worker = new Worker("ScriptFormatterWorker.js");

    InspectorTest.runDebuggerTestSuite([
        function testScriptFormatterWorker(next)
        {
            worker.onmessage = InspectorTest.safeWrap(function(event)
            {
                InspectorTest.assertEquals("var x = 0\n", event.data.content);
                next();
            });

            worker.onerror = function(event)
            {
                InspectorTest.addResult("Error in worker: " + event.data);
                next();
            };

            worker.postMessage({ method: "format", params: { mimeType: "text/javascript", content: "var x=0" } });
        },

        function testSourceMapping(next)
        {
            var formatter = new WebInspector.ScriptFormatter();

            InspectorTest.showScriptSource("obfuscated.js", didShowScriptSource);
            function didShowScriptSource(sourceFrame)
            {
                formatter.formatContent("text/javascript", sourceFrame._textEditor.text(), didFormatContent);
            }

            function didFormatContent(content, mapping)
            {
                var source = WebInspector.panels.sources.visibleView._textEditor.text();
                var formattedSource = content;

                function testMapping(string)
                {
                    var originalPosition = source.indexOf(string);
                    InspectorTest.assertTrue(originalPosition !== -1);
                    var originalLocation = WebInspector.Formatter.positionToLocation(source.lineEndings(), originalPosition);
                    var formattedLocation = mapping.originalToFormatted(originalLocation[0], originalLocation[1]);
                    var formattedPosition = WebInspector.Formatter.locationToPosition(formattedSource.lineEndings(), formattedLocation[0], formattedLocation[1]);
                    var expectedFormattedPosition = formattedSource.indexOf(string);
                    InspectorTest.assertEquals(expectedFormattedPosition, formattedPosition, "wrong mapping for <" + string + ">");
                }

                testMapping("function");
                testMapping("formatted1");
                testMapping("variable1");

                testMapping("    return \"functionWithComments\"");

                testMapping("onmessage");
                testMapping("indent_start");
                testMapping("function require");
                testMapping("var regexp");
                testMapping("importScripts");

                testMapping("formatted2");
                testMapping("variable2");

                next();
            }
        },

        function testFormatInlinedScripts(next)
        {
            worker.onmessage = InspectorTest.safeWrap(function(event)
            {
                InspectorTest.addResult(event.data.content);
                next();
            });

            worker.onerror = function(event)
            {
                InspectorTest.addResult("Error in worker: " + event.data);
                next();
            };

            var content = "<html><body><script>function f(){}<" + "/script><script>function g(){var a;if (a) return;}<" + "/script></body></html>";
            worker.postMessage({ method: "format", params: { mimeType: "text/html", content: content, indentString: "**" } });
        }
    ]);
}

</script>

</head>

<body onload="runTest()">
<p>Tests the script formatting functionality.
</p>

</body>
</html>