|
4 | 4 | <meta charset="utf-8"> |
5 | 5 | <title>CSSOM.js parse method</title> |
6 | 6 | <script src="../src/loader.js"></script> |
7 | | - <style type="text/css"> |
8 | | - html, body { |
9 | | - background: #333; |
10 | | - color: #EEE; |
11 | | - font: 12px sans-serif; |
12 | | - margin: 0; |
13 | | - height: 100%; |
14 | | - } |
15 | | - body { |
16 | | - padding-bottom: 1.7em; |
17 | | - -webkit-box-sizing: border-box; |
18 | | - -moz-box-sizing: border-box; |
19 | | - box-sizing: border-box; |
20 | | - } |
21 | | - table { |
22 | | - width: 100%; |
23 | | - table-layout: fixed; |
24 | | - margin: 0 auto; |
25 | | - } |
26 | | - td { |
27 | | - vertical-align: top; |
28 | | - } |
29 | | - h1 { |
30 | | - font: normal 1em sans-serif; |
31 | | - display: inline; |
32 | | - } |
33 | | - #labels { |
34 | | - color: #FFE992; |
35 | | - width: 66%; |
36 | | - } |
37 | | - #labels td { |
38 | | - width: 50%; |
39 | | - text-align: center; |
40 | | - } |
41 | | - #labels td::before { |
42 | | - content: '↱ '; |
43 | | - color: #998e62; |
44 | | - position: relative; |
45 | | - top: .4em; |
46 | | - } |
47 | | - #labels td::after { |
48 | | - content: ' ↴'; |
49 | | - color: #998e62; |
50 | | - position: relative; |
51 | | - top: .4em; |
52 | | - } |
53 | | - #content { |
54 | | - width: 100%; |
55 | | - height: 100%; |
56 | | - } |
57 | | - #content td { |
58 | | - width: 33%; |
59 | | - } |
60 | | - #content td + td { |
61 | | - padding-left: 1%; |
62 | | - } |
63 | | - #output span { |
64 | | - color: #666; |
65 | | - } |
66 | | - .style-cell textarea { |
67 | | - width: 99%; |
68 | | - height: 100%; |
69 | | - font: 12px monospace; |
70 | | - white-space: pre-wrap; |
71 | | - } |
72 | | - .serialized-cell { |
73 | | - border-left: 1px solid #363636; |
74 | | - } |
75 | | - #message { |
76 | | - visibility: hidden; |
77 | | - } |
78 | | - .error #message { |
79 | | - visibility: visible; |
80 | | - position: absolute; |
81 | | - top: 0; |
82 | | - left: 34%; |
83 | | - padding: 1em; |
84 | | - background: black; |
85 | | - color: #e34343; |
86 | | - font-size: 24px; |
87 | | - } |
88 | | - </style> |
| 7 | + <link rel="stylesheet" href="parse.css"> |
89 | 8 | </head> |
90 | 9 | <body> |
91 | 10 | <table id="labels"> |
|
102 | 21 | </tr> |
103 | 22 | </table> |
104 | 23 | <div id="message"></div> |
105 | | -<script defer> |
106 | | -/** |
107 | | - * @param {number} depth |
108 | | - * @return {string} |
109 | | - */ |
110 | | -function makeIndent(depth) { |
111 | | - var INDENT = ' '; |
112 | | - if (depth == 1) { |
113 | | - return INDENT; |
114 | | - } else if (depth < 1) { |
115 | | - return ''; |
116 | | - } |
117 | | - |
118 | | - if (depth in makeIndent.cache) { |
119 | | - return makeIndent.cache[depth]; |
120 | | - } else { |
121 | | - var result = INDENT; |
122 | | - for (var i = depth; --i;) { |
123 | | - result += INDENT; |
124 | | - } |
125 | | - makeIndent.cache[depth] = result; |
126 | | - return result; |
127 | | - } |
128 | | -} |
129 | | -makeIndent.cache = {}; |
130 | | - |
131 | | - |
132 | | -/** |
133 | | - * buildPath(2) -> '../..' |
134 | | - * @param {number} level |
135 | | - * @return {string} |
136 | | - */ |
137 | | -function buildPath(level) { |
138 | | - if (level == 0) { |
139 | | - return '.'; |
140 | | - } else { |
141 | | - var result = '..'; |
142 | | - for (var i = 1; i < level; i++) { |
143 | | - result += '/..'; |
144 | | - } |
145 | | - return result; |
146 | | - } |
147 | | -} |
148 | | - |
149 | | - |
150 | | -/** |
151 | | - * stringifyObjectKey('color') -> 'color' |
152 | | - * stringifyObjectKey('background-color') -> '"background-color"' |
153 | | - * @param {string} key |
154 | | - * @return {string} |
155 | | - */ |
156 | | -function stringifyObjectKey(key) { |
157 | | - return /^[a-zA-Z_$][A-Za-z0-9_$]*$/.test(key) ? |
158 | | - key : |
159 | | - '"' + escapeDoubleQuotes(key) + '"'; |
160 | | -} |
161 | | - |
162 | | - |
163 | | -/** |
164 | | - * @param {string} string |
165 | | - * @return {string} |
166 | | - * @see http://stackoverflow.com/questions/7382115/escape-quotes-in-a-string-with-backslash |
167 | | - */ |
168 | | -function escapeDoubleQuotes(string) { |
169 | | - return string.replace(/(\\*)(")/g, function(all, backslashes, quote) { |
170 | | - return backslashes.length % 2 ? |
171 | | - all : |
172 | | - backslashes + '\\' + quote; |
173 | | - }); |
174 | | -} |
175 | | - |
176 | | - |
177 | | -/** |
178 | | - * @param {Object} object |
179 | | - * @param {number} [depth] |
180 | | - * @param {Array} [stack] |
181 | | - * @return {string} |
182 | | - */ |
183 | | -function inspect(object, depth, stack) { |
184 | | - depth ? depth++ : (depth = 1); |
185 | | - stack = stack || (stack = []); |
186 | | - |
187 | | - switch (typeof object) { |
188 | | - case 'object': |
189 | | - var level = stack.indexOf(object); |
190 | | - if (level != -1) { |
191 | | - return buildPath(level); |
192 | | - } |
193 | | - stack = [object].concat(stack); |
194 | | - |
195 | | - var properties = []; |
196 | | - var indent = makeIndent(depth); |
197 | | - for (var key in object) { |
198 | | - if (object.hasOwnProperty(key)) { |
199 | | - properties.push(indent + stringifyObjectKey(key) + '<span>: </span>' + inspect(object[key], depth, stack)); |
200 | | - } |
201 | | - } |
202 | | - var indentInside = makeIndent(depth - 1); |
203 | | - return '<span>{</span>\n' + properties.join('<span>,</span>\n') + '\n' + indentInside + '<span>}</span>'; |
204 | | - |
205 | | - case 'string': |
206 | | - return '"' + object + '"'; |
207 | | - |
208 | | - default: |
209 | | - return object.toString(); |
210 | | - } |
211 | | -} |
212 | | - |
213 | | - |
214 | | - |
215 | | -var errors = []; |
216 | | -if (!("__defineGetter__" in {})) { |
217 | | - errors.push("Object.prototype.__defineGetter__ isn’t supported"); |
218 | | -} |
219 | | -if (errors.length) { |
220 | | - document.getElementById("message").innerHTML = errors.join("<br>"); |
221 | | - document.body.className = "error"; |
222 | | - throw errors.join("\n\n"); |
223 | | -} |
224 | | - |
225 | | -var style = document.getElementById("style"); |
226 | | -var output = document.getElementById("output"); |
227 | | -var serialized = document.getElementById("serialized"); |
228 | | - |
229 | | -function outputUpdated(){ |
230 | | - var value = style.value; |
231 | | - if (value != style.prevValue) { |
232 | | - var css = CSSOM.parse(value); |
233 | | - output.innerHTML = inspect(css); |
234 | | - serialized.innerHTML = css.toString(); |
235 | | - style.prevValue = value; |
236 | | - } |
237 | | -} |
238 | | - |
239 | | -function hashChanged(){ |
240 | | - var hash = location.hash; |
241 | | - var splitted = hash.split("="); |
242 | | - if (splitted.length < 2) { |
243 | | - return; |
244 | | - } |
245 | | - var name = splitted[0]; |
246 | | - var value = splitted[1]; |
247 | | - if (name == "#css") { |
248 | | - style.value = decodeURIComponent(value); |
249 | | - outputUpdated(); |
250 | | - } |
251 | | -} |
252 | | - |
253 | | -window.onload = function() { |
254 | | - hashChanged(); |
255 | | - outputUpdated(); |
256 | | -}; |
257 | | - |
258 | | -window.onhashchange = hashChanged; |
259 | | -style.onkeyup = style.onpaste = function changed(){ |
260 | | - outputUpdated(); |
261 | | -}; |
262 | | -style.onchange = function updateLocation(){ |
263 | | - location.hash = "css=" + encodeURIComponent(style.value); |
264 | | -}; |
265 | | -</script> |
| 24 | +<script src="parse.js" defer></script> |
266 | 25 | </body> |
267 | 26 | </html> |
0 commit comments