summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_rule_insertion.html
blob: 90886973a2f1b9edb13c07939cc2d1910d023d04 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=816720
-->
<head>
  <title>Test for Bug 816720</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <style type="text/css" id="style"></style>
</head>
<body>

<pre id="test"></pre>

<p><span id=control-serif>........</span></p>
<p><span id=control-monospace>........</span></p>
<p><span id=test-font>........</span></p>

<style id=other-styles>
  #test { font-size: 16px; animation: test 1s both }
  #control-serif { font: 16px serif }
  #test-font { font: 16px UnlikelyFontName, serif }
</style>

<p><span id=control-decimal></span></p>
<p><span id=control-cjk-decimal></span></p>
<p><span id=test-counter-style></span></p>

<style>
  #control-decimal::before { content: counter(a, decimal); }
  #control-cjk-decimal::before { content: counter(a, cjk-decimal); }
  #test-counter-style::before { content: counter(a, unlikely-counter-style); }
</style>

<script type="application/javascript">

// Monospace fonts available on all the platforms we're testing on.
//
// XXX Once bug 817220 is fixed we could instead use the value of
// font.name.monospace.x-western as the monospace font to use.
var MONOSPACE_FONTS = [
  "Courier",
  "Courier New",
  "Monaco",
  "DejaVu Sans Mono",
  "Droid Sans Mono"
];

var test = document.getElementById("test");
var controlSerif = document.getElementById("control-serif");
var controlMonospace = document.getElementById("control-monospace");
var testFont = document.getElementById("test-font");
var otherStyles = document.getElementById("other-styles");

otherStyles.sheet.insertRule("#control-monospace { font: 16px " +
                             MONOSPACE_FONTS + ", serif }", 0);

var monospaceWidth = controlMonospace.getBoundingClientRect().width;
var serifWidth = controlSerif.getBoundingClientRect().width;

var controlDecimal = document.getElementById("control-decimal");
var controlCJKDecimal = document.getElementById("control-cjk-decimal");
var testCounterStyle = document.getElementById("test-counter-style");

var decimalWidth = controlDecimal.getBoundingClientRect().width;
var cjkDecimalWidth = controlCJKDecimal.getBoundingClientRect().width;

// [at-rule type, passing condition, failing condition]
var outerRuleInfo = [
  ["@media", "all", "not all"],
  ["@-moz-document", "url-prefix('')", "url-prefix('zzz')"],
  ["@supports", "(color: green)", "(unknown: unknown)"]
];

// [rule, function to test whether the rule was successfully inserted and applied]
var innerRuleInfo = [
  ["#test { text-decoration: underline; }",
   function(aApplied, aParent, aException) {
     return !aException &&
            window.getComputedStyle(test, "").textDecoration ==
               (aApplied ? "underline" : "none");
   }],
  ["@page { margin: 4cm; }",
   function(aApplied, aParent, aException) {
     // just test whether it threw
     return !aException;
   }],
  ["@keyframes test { from { font-size: 100px; } to { font-size: 100px; } }",
   function(aApplied, aParent, aException) {
     return !aException &&
            window.getComputedStyle(test, "").fontSize ==
                (aApplied ? "100px" : "16px")
   }],
  ["@font-face { font-family: UnlikelyFontName; src: " +
     MONOSPACE_FONTS.map(function(s) { return "local('" + s + "')" }).join(", ") + "; }",
   function(aApplied, aParent, aException) {
     var width = testFont.getBoundingClientRect().width;
     if (aException) {
       return false;
     }
     if (navigator.oscpu.match(/Linux/)) {
       return true;
    }
    return Math.abs(width - (aApplied ? monospaceWidth : serifWidth)) <= 1; // bug 769194 prevents local()
                                             // fonts working on Android
   }],
  ["@import url(nothing.css);",
   function(aApplied, aParent, aException) {
     // just test whether it threw
     return aParent instanceof CSSRule ? aException : !aException;
   }],
  ["@namespace test url(http://example.org);",
   function(aApplied, aParent, aException) {
     // just test whether it threw
     return aParent instanceof CSSRule ? aException : !aException;
   }],
  ["@counter-style unlikely-counter-style { system: extends cjk-decimal; }",
   function (aApplied, aParent, aException) {
     var width = testCounterStyle.getBoundingClientRect().width;
     if (aException) {
       return false;
     }
     return width == (aApplied ? cjkDecimalWidth : decimalWidth);
   }],
];

function runTest()
{
  // First, assert that our assumed available fonts are indeed available
  // and have expected metrics.
  ok(monospaceWidth > 0, "monospace text has width");
  ok(serifWidth > 0, "serif text has width");
  ok(Math.abs(monospaceWidth - serifWidth) > 1, "monospace and serif text have sufficiently different widths");

  // And that the #test-font element starts off using the "serif" font.
  var initialFontTestWidth = testFont.getBoundingClientRect().width;
  is(initialFontTestWidth, serifWidth);

  ok(decimalWidth > 0, "decimal counter has width");
  ok(cjkDecimalWidth > 0, "cjk-decimal counter has width");
  ok(decimalWidth != cjkDecimalWidth, "decimal and cjk-decimal counter have different width")

  var initialCounterStyleWidth = testCounterStyle.getBoundingClientRect().width;
  is(initialCounterStyleWidth, decimalWidth);

  // We construct a style sheet with zero, one or two levels of conditional
  // grouping rules (taken from outerRuleInfo), with one of the inner rules
  // at the deepest level.
  var style = document.getElementById("style");

  // For each of the outer rule types...
  for (var outerRule1 = 0; outerRule1 < outerRuleInfo.length; outerRule1++) {
    // For each of { 0 = don't create an outer rule,
    //               1 = create an outer rule with a passing condition,
    //               2 = create an outer rule with a failing condition }...
    for (var outerRuleCondition1 = 0; outerRuleCondition1 <= 2; outerRuleCondition1++) {

      // For each of the outer rule types again...
      for (var outerRule2 = 0; outerRule2 < outerRuleInfo.length; outerRule2++) {
        // For each of { 0 = don't create an outer rule,
        //               1 = create an outer rule with a passing condition,
        //               2 = create an outer rule with a failing condition } again...
        for (var outerRuleCondition2 = 0; outerRuleCondition2 <= 2; outerRuleCondition2++) {

          // For each of the inner rule types...
          for (var innerRule = 0; innerRule < innerRuleInfo.length; innerRule++) {

            // Clear rules
            var object = style.sheet;
            while (object.cssRules.length) {
              object.deleteRule(0);
            }

            // We'll record whether the inner rule should have been applied,
            // according to whether we put passing or failing conditional
            // grouping rules around it.
            var applied = true;

            if (outerRuleCondition1) {
              // Create an outer conditional rule.
              object.insertRule([outerRuleInfo[outerRule1][0],
                                 outerRuleInfo[outerRule1][outerRuleCondition1],
                                 "{}"].join(" "), 0);
              object = object.cssRules[0];

              if (outerRuleCondition1 == 2) {
                // If we used a failing condition, we don't expect the inner
                // rule to be applied.
                applied = false;
              }
            }

            if (outerRuleCondition2) {
              // Create another outer conditional rule as a child of the first
              // outer conditional rule (or the style sheet, if we didn't create
              // a first outer conditional rule).
              object.insertRule([outerRuleInfo[outerRule2][0],
                                 outerRuleInfo[outerRule2][outerRuleCondition2],
                                 "{}"].join(" "), 0);
              object = object.cssRules[0];

              if (outerRuleCondition2 == 2) {
                // If we used a failing condition, we don't expect the inner
                // rule to be applied.
                applied = false;
              }
            }

            var outer = object instanceof CSSRule ? object.cssText : "style sheet";
            var inner = innerRuleInfo[innerRule][0];

            // Insert the inner rule.
            var exception = null;
            try {
              object.insertRule(inner, 0);
            } catch (e) {
              exception = e;
            }

            ok(innerRuleInfo[innerRule][1](applied, object, exception),
               "<" + [outerRule1, outerRuleCondition1, outerRule2,
                      outerRuleCondition2, innerRule].join(",") + "> " +
               "inserting " + inner + " into " + outer.replace(/ *\n */g, ' '));
          }
        }
      }
    }
  }

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();
runTest();
</script>
</body>
</html>