|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
|
5 let {Loader} = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {}); |
|
6 let {OutputParser} = devtools.require("devtools/output-parser"); |
|
7 |
|
8 let parser; |
|
9 let doc; |
|
10 |
|
11 function test() { |
|
12 waitForExplicitFinish(); |
|
13 |
|
14 gBrowser.selectedTab = gBrowser.addTab(); |
|
15 gBrowser.selectedBrowser.addEventListener("load", function onload() { |
|
16 gBrowser.selectedBrowser.removeEventListener("load", onload, true); |
|
17 waitForFocus(init, content); |
|
18 doc = content.document; |
|
19 }, true); |
|
20 |
|
21 content.location = "data:text/html,<h1>browser_outputParser.js</h1>" + |
|
22 "<div></div>"; |
|
23 } |
|
24 |
|
25 function init() { |
|
26 parser = new OutputParser(); |
|
27 testParseCssProperty(); |
|
28 } |
|
29 |
|
30 function testParseCssProperty() { |
|
31 let frag = parser.parseCssProperty("border", "1px solid red", { |
|
32 colorSwatchClass: "test-colorswatch" |
|
33 }); |
|
34 |
|
35 let target = doc.querySelector("div"); |
|
36 ok(target, "captain, we have the div"); |
|
37 target.appendChild(frag); |
|
38 |
|
39 is(target.innerHTML, |
|
40 '1px solid <span style="background-color:red" class="test-colorswatch"></span><span>#F00</span>', |
|
41 "CSS property correctly parsed"); |
|
42 |
|
43 target.innerHTML = ""; |
|
44 |
|
45 let frag = parser.parseCssProperty("background-image", "linear-gradient(to right, #F60 10%, rgba(0,0,0,1))", { |
|
46 colorSwatchClass: "test-colorswatch", |
|
47 colorClass: "test-color" |
|
48 }); |
|
49 target.appendChild(frag); |
|
50 is(target.innerHTML, |
|
51 'linear-gradient(to right, <span style="background-color:#F60" class="test-colorswatch"></span><span class="test-color">#F60</span> 10%, ' + |
|
52 '<span style="background-color:rgba(0,0,0,1)" class="test-colorswatch"></span><span class="test-color">#000</span>)', |
|
53 "Gradient CSS property correctly parsed"); |
|
54 |
|
55 target.innerHTML = ""; |
|
56 |
|
57 testParseHTMLAttribute(); |
|
58 } |
|
59 |
|
60 function testParseHTMLAttribute() { |
|
61 let attrib = "color:red; font-size: 12px; background-image: " + |
|
62 "url(chrome://branding/content/about-logo.png)"; |
|
63 let frag = parser.parseHTMLAttribute(attrib, { |
|
64 urlClass: "theme-link", |
|
65 colorClass: "theme-color" |
|
66 }); |
|
67 |
|
68 let target = doc.querySelector("div"); |
|
69 ok(target, "captain, we have the div"); |
|
70 target.appendChild(frag); |
|
71 |
|
72 let expected = 'color:<span class="theme-color">#F00</span>; font-size: 12px; ' + |
|
73 'background-image: url(\'<a href="chrome://branding/content/about-logo.png" ' + |
|
74 'class="theme-link" ' + |
|
75 'target="_blank">chrome://branding/content/about-logo.png</a>\')'; |
|
76 |
|
77 is(target.innerHTML, expected, "HTML Attribute correctly parsed"); |
|
78 target.innerHTML = ""; |
|
79 testParseNonCssHTMLAttribute(); |
|
80 } |
|
81 |
|
82 function testParseNonCssHTMLAttribute() { |
|
83 let attrib = "someclass background someotherclass red"; |
|
84 let frag = parser.parseHTMLAttribute(attrib); |
|
85 |
|
86 let target = doc.querySelector("div"); |
|
87 ok(target, "captain, we have the div"); |
|
88 target.appendChild(frag); |
|
89 |
|
90 let expected = 'someclass background someotherclass red'; |
|
91 |
|
92 is(target.innerHTML, expected, "Non-CSS HTML Attribute correctly parsed"); |
|
93 target.innerHTML = ""; |
|
94 finishUp(); |
|
95 } |
|
96 |
|
97 |
|
98 function finishUp() { |
|
99 Services = Loader = OutputParser = parser = doc = null; |
|
100 gBrowser.removeCurrentTab(); |
|
101 finish(); |
|
102 } |