aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/debug/devcss/devcss_test.html
blob: f5f7f807d85558f71542bd64113a09e82ab5c5cc (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
  <!--
  -->
  <head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Closure Unit Tests - CSS Object Model helper</title>
    <script src="../../base.js"></script>
    <script>
      goog.require('goog.testing.jsunit');
      goog.require('goog.userAgent');
      goog.require('goog.style');
      goog.require('goog.cssom');
      goog.require('goog.debug');
      goog.require('goog.debug.DevCss');
    </script>

    <style>
      @import "devcss_test_import_1.css";

      .css-style-1 {
        background-color: rgb(173,216,230); /* lightblue */
        padding: 10px;
      }
      .css-style-2 {
        display: block;
        background-color: transparent;
        margin-top: 1px;
        margin-left: 1px;
        padding: 10px;
      }
      .css-style-3 {
        background-color: orange;
        padding: 10px;
      }

      .compound1.compound2 {
        -goog-ie6-selector: ".compound1_compound2";
        background-color: rgb(255,192,203); /* aka pink */
      }

      .USERAGENT-OPERA .css-style-2,
      .USERAGENT-GECKO .css-style-2,
      .USERAGENT-MOBILE .css-style-2 {
        background-color: rgb(255,192,203); /* aka pink */
      }

      .USERAGENT-OPERA .css-style-2,
      .USERAGENT-WEBKIT-GTE255 .css-style-2,
      .USERAGENT-MOBILE .css-style-2 {
        margin-top: 20px;
      }

      .USERAGENT-OPERA .css-style-2,
      .USERAGENT-WEBKIT-LTE200 .css-style-2,
      .USERAGENT-MOBILE .css-style-2 {
        margin-left: 15px;
      }
      .USERAGENT-GECKO #devcss-gecko-1,
      .USERAGENT-GECKO #devcss-gecko-2 {
        background-color: rgb(255,192,203); /* aka pink */
      }
      .USERAGENT-IE-8 #devcss-ie8-1,
      .USERAGENT-IE-8 #devcss-ie8-2 {
        background-color: rgb(255,192,203); /* aka pink */
      }
      #devcss-test-ie6 {
        background-color: purple;
      }
      .USERAGENT-IE-6 #devcss-test-ie6 {
        background-color: rgb(255,192,203); /* aka pink */
      }
    </style>
    <style>
      @import "devcss_test_import_2.css";
    </style>

  </head>
  <body>

    <div id="devcss-test-2" class="css-style-2">
      <ul>
        <li>
          Should end up background-color:rgb(255, 192, 203) aka pink.
        </li>
        <li>
          Should end up with margin-top: 20px; margin-left: 15px;
        </li>
      </ul>
    </div>

    <br><br>

    <div id="devcss-test-ie6">
      <ul>
        <li>
          Should end up background-color:rgb(255, 192, 203) aka pink, not black or purple.
        </li>
      </ul>
    </div>

    <!--
      TODO(user): Re-enable if we ever come up with a way to make imports
          work.
    <div id="devcss-test-importfixer-1" class="css-style-3">
      <ul>
        <li>
          Should end up yellow from the first import of
          devcss_test_import_2.css, overriding the previous orange setting and
          ignoring the second import of devcss_test_import_1.css which would set
          it grey.
        </li>
      </ul>
    </div>
    <div id="devcss-test-importfixer-2" class="css-style-1">
      <ul>
        <li>
          Should end up lightblue, which is a revert back to the setting in this
          test file, after disabling the second import of
          devcss_test_import_1.css
        </li>
      </ul>
    </div>
    -->

    <br><br>

    <div id="devcss-ie8-1">
      This element should be pink for IE8.
    </div>
    <div id="devcss-ie8-2">
      This element should also be pink for IE8.
    </div>

    <br><br>

    <div id="devcss-gecko-1">
      This element should be pink for Gecko.
    </div>
    <div id="devcss-gecko-2">
      This element should also be pink for Gecko.
    </div>

    <br><br>

    <div id="devcss-test-compound1" class="compound1">
      This (class="compound1") should have no bg.
    </div>
    <div id="devcss-test-compound2" class="compound2">
      This (class="compound2") should have no bg.
    </div>
    <div id="devcss-test-compound1-2"
         class="compound1 compound2 compound1_compound2">
      This (class="compound1 compound2 compound1_compound2") should have
      a pink bg for IE6.
    </div>

    <br><br>
    <input id="devcss-test-combined1" type="hidden" class="ie6-1 ie6-2">
    <input id="devcss-test-combined2" type="hidden" class="ie6-3 ie6-1 ie6-2">
    <input id="devcss-test-notcombined1" type="hidden" class="ie6-1">
    <input id="devcss-test-notcombined2" type="hidden" class="ie6-2">
    <input id="devcss-test-notcombined3" type="hidden" class="ie6-3">

    <script type="text/javascript">
    var el = document.getElementById('devcss-test-2');

    // Since background color sometimes comes back like rgb(xxx, xxx, xxx)
    // or rgb(xxx,xxx,xxx) depending on browser.
    function spaceless(foo) {
      return foo.replace(/\s/g, '')
    }

    function testGetIe6CombinedSelectorText() {
      var devcssInstance = new goog.debug.DevCss();
      devcssInstance.ie6CombinedMatches_ = [];
      var css = '.class2 { -goog-ie6-selector:".class1_class2"; prop: val; }';
      var newCss = devcssInstance.getIe6CombinedSelectorText_(css);
      assertEquals('.class1_class2', newCss);
      assertArrayEquals(['class1', 'class2'],
          devcssInstance.ie6CombinedMatches_[0].classNames);
      assertEquals('class1_class2',
          devcssInstance.ie6CombinedMatches_[0].combinedClassName);

      devcssInstance = new goog.debug.DevCss();
      devcssInstance.ie6CombinedMatches_ = [];
      css = '.class3 { prop: val; -goog-ie6-selector:".class1_class2_class3";' +
          'prop: val; }';
      newCss = devcssInstance.getIe6CombinedSelectorText_(css);
      assertEquals('.class1_class2_class3', newCss);
      assertArrayEquals(['class1', 'class2', 'class3'],
          devcssInstance.ie6CombinedMatches_[0].classNames);
      assertEquals('class1_class2_class3',
          devcssInstance.ie6CombinedMatches_[0].combinedClassName);

      devcssInstance = new goog.debug.DevCss();
      devcssInstance.ie6CombinedMatches_ = [];
      css = '.class3, .class5 {' +
          '-goog-ie6-selector:".class1_class2_class3, .class4_class5";' +
          'prop: val; }';
      newCss = devcssInstance.getIe6CombinedSelectorText_(css);
      assertEquals('.class1_class2_class3, .class4_class5', newCss);
      assertArrayEquals(['class1', 'class2', 'class3'],
          devcssInstance.ie6CombinedMatches_[0].classNames);
      assertEquals('class1_class2_class3',
          devcssInstance.ie6CombinedMatches_[0].combinedClassName);
      assertArrayEquals(['class4', 'class5'],
          devcssInstance.ie6CombinedMatches_[1].classNames);
      assertEquals('class4_class5',
          devcssInstance.ie6CombinedMatches_[1].combinedClassName);
    }

    function testAddIe6CombinedClassNames() {
      var el_combined1 = document.getElementById('devcss-test-combined1');
      var el_combined2 = document.getElementById('devcss-test-combined2');
      var el_notcombined1 = document.getElementById('devcss-test-notcombined1');
      var el_notcombined2 = document.getElementById('devcss-test-notcombined2');
      var el_notcombined3 = document.getElementById('devcss-test-notcombined3');

      var devcssInstance = new goog.debug.DevCss();
      devcssInstance.ie6CombinedMatches_ = [
          {
            classNames: ['ie6-2', 'ie6-1'],
            combinedClassName: 'ie6-1_ie6-2',
            els: []
          },
          {
            classNames: ['ie6-2', 'ie6-3', 'ie6-1'],
            combinedClassName: 'ie6-1_ie6-2_ie6-3',
            els: []
          }];

      devcssInstance.addIe6CombinedClassNames_();
      assertEquals(-1, el_notcombined1.className.indexOf('ie6-1_ie6-2'));
      assertEquals(-1, el_notcombined2.className.indexOf('ie6-1_ie6-2'));
      assertEquals(-1, el_notcombined3.className.indexOf('ie6-1_ie6-2_ie6-3'));
      assertTrue(el_combined1.className.indexOf('ie6-1_ie6-2') != -1);
      assertTrue(el_combined2.className.indexOf('ie6-1_ie6-2_ie6-3') != -1);
    }

    function testActivateBrowserSpecificCssALL() {
      // equals GECKO
      var devcssInstance = new goog.debug.DevCss('GECKO');
      devcssInstance.activateBrowserSpecificCssRules(false);
      var backgroundColor = goog.style.getBackgroundColor(el);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));

      // GECKO test case w/ two selectors joined by a commma.
      var elGeckoOne = document.getElementById('devcss-gecko-1');
      backgroundColor = goog.style.getBackgroundColor(elGeckoOne);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
      var elGeckoTwo = document.getElementById('devcss-gecko-2');
      backgroundColor = goog.style.getBackgroundColor(elGeckoTwo);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
    }


    function testActivateBrowserSpecificCssWithVersion() {
      // equals IE 6
      var devcssInstance = new goog.debug.DevCss('IE', '6');
      devcssInstance.activateBrowserSpecificCssRules(false);
      var elIe6 = document.getElementById('devcss-test-ie6');
      var backgroundColor = goog.style.getBackgroundColor(elIe6);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));

      // IE8 test case w/ two selectors joined by a commma.
      var devCssInstanceTwo = new goog.debug.DevCss('IE', '8');
      devCssInstanceTwo.activateBrowserSpecificCssRules(false);
      var elIe8One = document.getElementById('devcss-ie8-1');
      backgroundColor = goog.style.getBackgroundColor(elIe8One);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
      var elIe8Two = document.getElementById('devcss-ie8-2');
      backgroundColor = goog.style.getBackgroundColor(elIe8Two);
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
    }

    function testActivateBrowserSpecificCssGteInvalid() {
      // WEBKIT_GTE255
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(1, marginBox.top); // should still be 1

      var devcssInstance = new goog.debug.DevCss('WEBKIT', 254);
      devcssInstance.activateBrowserSpecificCssRules(false);
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(1, marginBox.top); // should still be 1
    }

    function testActivateBrowserSpecificCssGteValid() {
      var devcssInstance = new goog.debug.DevCss('WEBKIT', 255);
      devcssInstance.activateBrowserSpecificCssRules(false);
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(20, marginBox.top);
    }

    function testActivateBrowserSpecificCssLteInvalid() {
      // IE_LTE6
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(1, marginBox.left); // should still be 1

      var devcssInstance = new goog.debug.DevCss('WEBKIT', 202);
      devcssInstance.activateBrowserSpecificCssRules(false);
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(1, marginBox.left); // should still be 1
    }

    function testActivateBrowserSpecificCssLteValid() {
      var devcssInstance = new goog.debug.DevCss('WEBKIT', 199);
      devcssInstance.activateBrowserSpecificCssRules(false);
      var marginBox = goog.style.getMarginBox(el);
      assertEquals(15, marginBox.left);
    }

    function testReplaceIe6Selectors() {
      var devcssInstance = new goog.debug.DevCss('IE', 6);
      devcssInstance.activateBrowserSpecificCssRules(false);

      // It should correctly be transparent, even in IE6.
      var compound2El = document.getElementById('devcss-test-compound2');
      var backgroundColor = spaceless(
          goog.style.getBackgroundColor(compound2El));

      assertTrue('Unexpected background color: ' + backgroundColor,
          'transparent' == backgroundColor ||
          'rgba(0,0,0,0)' == backgroundColor);

      // And this one should have the combined selector working, even in
      // IE6.
      backgroundColor = goog.style.getBackgroundColor(
          document.getElementById('devcss-test-compound1-2'));
      assertEquals('rgb(255,192,203)', spaceless(backgroundColor));
    }

    /*
     * TODO(user): Re-enable if we ever come up with a way to make imports
     * work.
    function testDisableDuplicateStyleSheetImports() {
      var el1 = document.getElementById('devcss-test-importfixer-1');
      var el2 = document.getElementById('devcss-test-importfixer-2');

      var backgroundColor = goog.style.getBackgroundColor(el1);
      assertEquals('rgb(255,255,0)', spaceless(backgroundColor));

      var backgroundColor = goog.style.getBackgroundColor(el2);
      assertEquals('rgb(255,0,0)', spaceless(backgroundColor));

      // This should disable the second coming of devcss_test_import_1.css.
      var devcssInstance = new goog.debug.DevCss();
      devcssInstance.disableDuplicateStyleSheetImports();

      var backgroundColor = goog.style.getBackgroundColor(el1);
      assertEquals('rgb(255,255,0)', spaceless(backgroundColor));

      var backgroundColor = goog.style.getBackgroundColor(el2);
      assertEquals('rgb(173,216,230)', spaceless(backgroundColor));
    }
    */
    </script>
  </body>
</html>