OLD | NEW |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 package com.google.caja.plugin; | 15 package com.google.caja.plugin; |
16 | 16 |
17 import com.google.caja.lexer.ParseException; | 17 import com.google.caja.lexer.ParseException; |
18 import com.google.caja.parser.js.Statement; | 18 import com.google.caja.parser.ParseTreeNode; |
| 19 import com.google.caja.parser.css.CssTree; |
| 20 import com.google.caja.parser.js.ArrayConstructor; |
19 import com.google.caja.render.Concatenator; | 21 import com.google.caja.render.Concatenator; |
20 import com.google.caja.render.JsPrettyPrinter; | 22 import com.google.caja.render.JsPrettyPrinter; |
21 import com.google.caja.reporting.RenderContext; | 23 import com.google.caja.reporting.RenderContext; |
22 import com.google.caja.util.CajaTestCase; | 24 import com.google.caja.util.CajaTestCase; |
23 | 25 |
24 public class CssCompilerTest extends CajaTestCase { | 26 public class CssRuleRewriterTest extends CajaTestCase { |
25 public void testSimpleRule() { | 27 public void testSimpleRule() { |
26 assertCompiledCss( | 28 assertCompiledCss( |
27 "p {color:purple}", | 29 "p {color:purple}", |
28 "[ '.', ' p {\\n color: purple\\n}' ]"); | 30 "[ '.', ' p {\\n color: purple\\n}' ]"); |
29 } | 31 } |
30 | 32 |
31 public void testClassRule() { | 33 public void testClassRule() { |
32 assertCompiledCss( | 34 assertCompiledCss( |
33 ".foo .bar {color:blue}", | 35 ".foo .bar {color:blue}", |
34 "[ '.', ' .foo .bar {\\n color: blue\\n}' ]"); | 36 "[ '.', ' .foo .bar {\\n color: blue\\n}' ]"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 /** | 72 /** |
71 * "*" selectors should rewrite properly. | 73 * "*" selectors should rewrite properly. |
72 * <a href="http://code.google.com/p/google-caja/issues/detail?id=57">bug</a> | 74 * <a href="http://code.google.com/p/google-caja/issues/detail?id=57">bug</a> |
73 */ | 75 */ |
74 public void testWildcardSelectors() throws Exception { | 76 public void testWildcardSelectors() throws Exception { |
75 assertCompiledCss( | 77 assertCompiledCss( |
76 "div * { margin: 0; }", | 78 "div * { margin: 0; }", |
77 "[ '.', ' div * {\\n margin: 0;\\n}' ]"); | 79 "[ '.', ' div * {\\n margin: 0;\\n}' ]"); |
78 } | 80 } |
79 | 81 |
| 82 public void testStaticIdClass() { |
| 83 assertCompiledCss( |
| 84 "#a > #b, .c { color: blue }", |
| 85 "[ '.xyz___ #a-xyz___ > #b-xyz___, .xyz___ .c {\\n color: blue\\n}' ]", |
| 86 false); |
| 87 } |
| 88 |
80 private void assertCompiledCss(String input, String golden) { | 89 private void assertCompiledCss(String input, String golden) { |
| 90 assertCompiledCss(input, golden, true); |
| 91 } |
| 92 |
| 93 private void assertCompiledCss(String input, String golden, boolean dynamic) { |
81 try { | 94 try { |
82 Statement s = new CssCompiler().compileCss(css(fromString(input))); | 95 PluginMeta pm = new PluginMeta(); |
83 assertEquals(golden, stripBoilerPlate(render(s, 160))); | 96 if (!dynamic) { pm.setIdClass("xyz___"); } |
| 97 CssTree.StyleSheet css = css(fromString(input)); |
| 98 new CssRuleRewriter(pm).rewriteCss(css); |
| 99 ArrayConstructor ac = CssRuleRewriter.cssToJs(css); |
| 100 assertEquals(golden, render(ac, 160)); |
84 } catch (ParseException ex) { | 101 } catch (ParseException ex) { |
85 fail(input); | 102 fail(input); |
86 } | 103 } |
87 } | 104 } |
88 | 105 |
89 private String stripBoilerPlate(String s) { | 106 private static String render(ParseTreeNode node, int limit) { |
90 String pre = "IMPORTS___.emitCss___("; | |
91 String post = ".join(IMPORTS___.getIdClass___()))"; | |
92 if (s.startsWith(pre) && s.endsWith(post)) { | |
93 return s.substring(pre.length(), s.length() - post.length()); | |
94 } | |
95 return s; | |
96 } | |
97 | |
98 private String render(Statement node, int limit) { | |
99 StringBuilder sb = new StringBuilder(); | 107 StringBuilder sb = new StringBuilder(); |
100 JsPrettyPrinter pp = new JsPrettyPrinter(new Concatenator(sb)); | 108 JsPrettyPrinter pp = new JsPrettyPrinter(new Concatenator(sb)); |
101 pp.setLineLengthLimit(limit); | 109 pp.setLineLengthLimit(limit); |
102 node.render(new RenderContext(pp)); | 110 node.render(new RenderContext(pp)); |
103 pp.noMoreTokens(); | 111 pp.noMoreTokens(); |
104 return sb.toString(); | 112 return sb.toString(); |
105 } | 113 } |
106 } | 114 } |
OLD | NEW |