corrade-nucleus-nucleons – Blame information for rev 24
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
20 | office | 1 | /* |
2 | {{&header_text}} |
||
3 | |||
4 | The MIT License (MIT) |
||
5 | |||
6 | Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. |
||
7 | |||
8 | Permission is hereby granted, free of charge, to any person |
||
9 | obtaining a copy of this software and associated documentation files |
||
10 | (the "Software"), to deal in the Software without restriction, |
||
11 | including without limitation the rights to use, copy, modify, merge, |
||
12 | publish, distribute, sublicense, and/or sell copies of the Software, |
||
13 | and to permit persons to whom the Software is furnished to do so, |
||
14 | subject to the following conditions: |
||
15 | |||
16 | The above copyright notice and this permission notice shall be |
||
17 | included in all copies or substantial portions of the Software. |
||
18 | |||
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
||
23 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
||
24 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
||
25 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
26 | SOFTWARE. |
||
27 | */ |
||
28 | /*jshint unused:false */ |
||
29 | |||
30 | function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify) |
||
31 | { |
||
32 | |||
33 | var default_opts = { |
||
34 | indent_size: 4, |
||
35 | indent_char: ' ', |
||
36 | preserve_newlines: true, |
||
37 | jslint_happy: false, |
||
38 | keep_array_indentation: false, |
||
39 | brace_style: 'collapse', |
||
40 | space_before_conditional: true, |
||
41 | break_chained_methods: false, |
||
42 | selector_separator: '\n', |
||
43 | end_with_newline: false |
||
44 | }; |
||
45 | var opts; |
||
46 | |||
47 | {{#default_options}} default_opts.{{name}} = {{&value}}; |
||
48 | {{/default_options}} |
||
49 | |||
50 | function reset_options() |
||
51 | { |
||
52 | opts = JSON.parse(JSON.stringify(default_opts)); |
||
53 | } |
||
54 | |||
55 | function test_css_beautifier(input) |
||
56 | { |
||
57 | return css_beautify(input, opts); |
||
58 | } |
||
59 | |||
60 | var sanitytest; |
||
61 | |||
62 | // test the input on beautifier with the current flag settings |
||
63 | // does not check the indentation / surroundings as bt() does |
||
64 | function test_fragment(input, expected) |
||
65 | { |
||
66 | expected = expected || expected === '' ? expected : input; |
||
67 | sanitytest.expect(input, expected); |
||
68 | // if the expected is different from input, run it again |
||
69 | // expected output should be unchanged when run twice. |
||
70 | if (expected !== input) { |
||
71 | sanitytest.expect(expected, expected); |
||
72 | } |
||
73 | |||
74 | // Everywhere we do newlines, they should be replaced with opts.eol |
||
75 | opts.eol = '\r\\n'; |
||
76 | expected = expected.replace(/[\n]/g, '\r\n'); |
||
77 | sanitytest.expect(input, expected); |
||
78 | if (input.indexOf('\n') !== -1) { |
||
79 | input = input.replace(/[\n]/g, '\r\n'); |
||
80 | sanitytest.expect(input, expected); |
||
81 | // Ensure support for auto eol detection |
||
82 | opts.eol = 'auto'; |
||
83 | sanitytest.expect(input, expected); |
||
84 | } |
||
85 | opts.eol = '\n'; |
||
86 | } |
||
87 | |||
88 | // test css |
||
89 | function t(input, expectation) |
||
90 | { |
||
91 | var wrapped_input, wrapped_expectation; |
||
92 | |||
93 | expectation = expectation || expectation === '' ? expectation : input; |
||
94 | sanitytest.test_function(test_css_beautifier, 'css_beautify'); |
||
95 | test_fragment(input, expectation); |
||
96 | } |
||
97 | |||
98 | function unicode_char(value) { |
||
99 | return String.fromCharCode(value); |
||
100 | } |
||
101 | |||
102 | function beautifier_tests() |
||
103 | { |
||
104 | sanitytest = test_obj; |
||
105 | |||
106 | reset_options(); |
||
107 | //============================================================ |
||
108 | t(".tabs {}"); |
||
109 | |||
110 | {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}} |
||
111 | //============================================================ |
||
112 | {{^matrix}} |
||
113 | // {{&name}} |
||
114 | reset_options(); |
||
115 | {{#options}} |
||
116 | opts.{{name}} = {{&value}}; |
||
117 | {{/options}} |
||
118 | {{#tests}} |
||
119 | {{#test_line}}.{{/test_line}}; |
||
120 | {{/tests}} |
||
121 | |||
122 | {{/matrix}} |
||
123 | {{#matrix}} |
||
124 | // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}}) |
||
125 | reset_options(); |
||
126 | {{#options}} |
||
127 | opts.{{name}} = {{&value}}; |
||
128 | {{/options}} |
||
129 | {{#tests}} |
||
130 | {{#test_line}}.{{/test_line}}; |
||
131 | {{/tests}} |
||
132 | |||
133 | {{/matrix}} |
||
134 | {{#unset_mustache_tags}}.{{/unset_mustache_tags}}{{/groups}} |
||
135 | } |
||
136 | |||
137 | function beautifier_unconverted_tests() |
||
138 | { |
||
139 | sanitytest = test_obj; |
||
140 | |||
141 | reset_options(); |
||
142 | //============================================================ |
||
143 | // test basic css beautifier |
||
144 | t(".tabs {}"); |
||
145 | t(".tabs{color:red;}", ".tabs {\n\tcolor: red;\n}"); |
||
146 | t(".tabs{color:rgb(255, 255, 0)}", ".tabs {\n\tcolor: rgb(255, 255, 0)\n}"); |
||
147 | t(".tabs{background:url('back.jpg')}", ".tabs {\n\tbackground: url('back.jpg')\n}"); |
||
148 | t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}"); |
||
149 | t("@media print {.tab{}}", "@media print {\n\t.tab {}\n}"); |
||
150 | t("@media print {.tab{background-image:url(foo@2x.png)}}", "@media print {\n\t.tab {\n\t\tbackground-image: url(foo@2x.png)\n\t}\n}"); |
||
151 | |||
152 | t("a:before {\n" + |
||
153 | "\tcontent: 'a{color:black;}\"\"\\'\\'\"\\n\\n\\na{color:black}\';\n" + |
||
154 | "}"); |
||
155 | |||
156 | //lead-in whitespace determines base-indent. |
||
157 | // lead-in newlines are stripped. |
||
158 | t("\n\na, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}"); |
||
159 | t(" a, img {padding: 0.2px}", " a,\n img {\n \tpadding: 0.2px\n }"); |
||
160 | t(" \t \na, img {padding: 0.2px}", " \t a,\n \t img {\n \t \tpadding: 0.2px\n \t }"); |
||
161 | t("\n\n a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}"); |
||
162 | |||
163 | // separate selectors |
||
164 | t("#bla, #foo{color:red}", "#bla,\n#foo {\n\tcolor: red\n}"); |
||
165 | t("a, img {padding: 0.2px}", "a,\nimg {\n\tpadding: 0.2px\n}"); |
||
166 | |||
167 | // block nesting |
||
168 | t("#foo {\n\tbackground-image: url(foo@2x.png);\n\t@font-face {\n\t\tfont-family: 'Bitstream Vera Serif Bold';\n\t\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n\t}\n}"); |
||
169 | t("@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo@2x.png);\n\t}\n\t@font-face {\n\t\tfont-family: 'Bitstream Vera Serif Bold';\n\t\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n\t}\n}"); |
||
170 | /* |
||
171 | @font-face { |
||
172 | font-family: 'Bitstream Vera Serif Bold'; |
||
173 | src: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf'); |
||
174 | } |
||
175 | @media screen { |
||
176 | #foo:hover { |
||
177 | background-image: url(foo.png); |
||
178 | } |
||
179 | @media screen and (min-device-pixel-ratio: 2) { |
||
180 | @font-face { |
||
181 | font-family: 'Helvetica Neue' |
||
182 | } |
||
183 | #foo:hover { |
||
184 | background-image: url(foo@2x.png); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | */ |
||
189 | t("@font-face {\n\tfont-family: 'Bitstream Vera Serif Bold';\n\tsrc: url('http://developer.mozilla.org/@api/deki/files/2934/=VeraSeBd.ttf');\n}\n@media screen {\n\t#foo:hover {\n\t\tbackground-image: url(foo.png);\n\t}\n\t@media screen and (min-device-pixel-ratio: 2) {\n\t\t@font-face {\n\t\t\tfont-family: 'Helvetica Neue'\n\t\t}\n\t\t#foo:hover {\n\t\t\tbackground-image: url(foo@2x.png);\n\t\t}\n\t}\n}"); |
||
190 | |||
191 | // less-css cases |
||
192 | t('.well{@well-bg:@bg-color;@well-fg:@fg-color;}','.well {\n\t@well-bg: @bg-color;\n\t@well-fg: @fg-color;\n}'); |
||
193 | t('.well {&.active {\nbox-shadow: 0 1px 1px @border-color, 1px 0 1px @border-color;}}', |
||
194 | '.well {\n' + |
||
195 | '\t&.active {\n' + |
||
196 | '\t\tbox-shadow: 0 1px 1px @border-color, 1px 0 1px @border-color;\n' + |
||
197 | '\t}\n' + |
||
198 | '}'); |
||
199 | t('a {\n' + |
||
200 | '\tcolor: blue;\n' + |
||
201 | '\t&:hover {\n' + |
||
202 | '\t\tcolor: green;\n' + |
||
203 | '\t}\n' + |
||
204 | '\t& & &&&.active {\n' + |
||
205 | '\t\tcolor: green;\n' + |
||
206 | '\t}\n' + |
||
207 | '}'); |
||
208 | |||
209 | // Not sure if this is sensible |
||
210 | // but I believe it is correct to not remove the space in "&: hover". |
||
211 | t('a {\n' + |
||
212 | '\t&: hover {\n' + |
||
213 | '\t\tcolor: green;\n' + |
||
214 | '\t}\n' + |
||
215 | '}'); |
||
216 | |||
217 | // import |
||
218 | t('@import "test";'); |
||
219 | |||
220 | // don't break nested pseudo-classes |
||
221 | t("a:first-child{color:red;div:first-child{color:black;}}", |
||
222 | "a:first-child {\n\tcolor: red;\n\tdiv:first-child {\n\t\tcolor: black;\n\t}\n}"); |
||
223 | |||
224 | // handle SASS/LESS parent reference |
||
225 | t("div{&:first-letter {text-transform: uppercase;}}", |
||
226 | "div {\n\t&:first-letter {\n\t\ttext-transform: uppercase;\n\t}\n}"); |
||
227 | |||
228 | //nested modifiers (&:hover etc) |
||
229 | t(".tabs{&:hover{width:10px;}}", ".tabs {\n\t&:hover {\n\t\twidth: 10px;\n\t}\n}"); |
||
230 | t(".tabs{&.big{width:10px;}}", ".tabs {\n\t&.big {\n\t\twidth: 10px;\n\t}\n}"); |
||
231 | t(".tabs{&>big{width:10px;}}", ".tabs {\n\t&>big {\n\t\twidth: 10px;\n\t}\n}"); |
||
232 | t(".tabs{&+.big{width:10px;}}", ".tabs {\n\t&+.big {\n\t\twidth: 10px;\n\t}\n}"); |
||
233 | |||
234 | //nested rules |
||
235 | t(".tabs{.child{width:10px;}}", ".tabs {\n\t.child {\n\t\twidth: 10px;\n\t}\n}"); |
||
236 | |||
237 | //variables |
||
238 | t("@myvar:10px;.tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}"); |
||
239 | t("@myvar:10px; .tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}"); |
||
240 | |||
241 | // test options |
||
242 | opts.indent_size = 2; |
||
243 | opts.indent_char = ' '; |
||
244 | opts.selector_separator_newline = false; |
||
245 | |||
246 | // pseudo-classes and pseudo-elements |
||
247 | t("#foo:hover {\n background-image: url(foo@2x.png)\n}"); |
||
248 | t("#foo *:hover {\n color: purple\n}"); |
||
249 | t("::selection {\n color: #ff0000;\n}"); |
||
250 | |||
251 | // TODO: don't break nested pseduo-classes |
||
252 | t("@media screen {.tab,.bat:hover {color:red}}", "@media screen {\n .tab, .bat:hover {\n color: red\n }\n}"); |
||
253 | |||
254 | // particular edge case with braces and semicolons inside tags that allows custom text |
||
255 | t("a:not(\"foobar\\\";{}omg\"){\ncontent: 'example\\';{} text';\ncontent: \"example\\\";{} text\";}", |
||
256 | "a:not(\"foobar\\\";{}omg\") {\n content: 'example\\';{} text';\n content: \"example\\\";{} text\";\n}"); |
||
257 | |||
258 | // may not eat the space before "[" |
||
259 | t('html.js [data-custom="123"] {\n opacity: 1.00;\n}'); |
||
260 | t('html.js *[data-custom="123"] {\n opacity: 1.00;\n}'); |
||
261 | } |
||
262 | |||
263 | beautifier_tests(); |
||
264 | beautifier_unconverted_tests(); |
||
265 | } |
||
266 | |||
267 | if (typeof exports !== "undefined") { |
||
268 | exports.run_css_tests = run_css_tests; |
||
269 | } |