summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_page_parser.html
blob: 8c94be0bf1785406e0c8f154e815cd665f140f0d (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
<!DOCTYPE HTML>
<html>
<!-- https://bugzilla.mozilla.org/show_bug.cgi?id=115199 -->
<head>
  <meta charset="UTF-8">
  <title>Test of @page parser</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<p>@page parsing (<a
  target="_blank"
  href="https://bugzilla.mozilla.org/show_bug.cgi?id=115199"
>bug 115199</a>)</p>
<pre id="display"></pre>
<style type="text/css" id="testbox"></style>
<script class="testbody" type="text/javascript">
  function _(b) { return "@page { " + b + " }"; };

  var testset = [
    // CSS 2.1 only allows margin properties in the page rule.

    // Check a bad property.
    { rule: "position: absolute;" },

    // Check good properties with invalid units.
    { rule: _("margin: 2in; margin: 2vw;"), expected: {
      "margin-top": "2in",
      "margin-right": "2in",
      "margin-bottom": "2in",
      "margin-left": "2in"
    }},
    { rule: _("margin-top: 2in; margin-top: 2vw;"), expected: {"margin-top": "2in"}},
    { rule: _("margin-top: 2in; margin-top: 2vh;"), expected: {"margin-top": "2in"}},
    { rule: _("margin-top: 2in; margin-top: 2vmax;"), expected: {"margin-top": "2in"}},
    { rule: _("margin-top: 2in; margin-top: 2vmin;"), expected: {"margin-top": "2in"}},

    // Check good properties.
    { rule: _("margin: 2in;"), expected: {
      "margin-top": "2in",
      "margin-right": "2in",
      "margin-bottom": "2in",
      "margin-left": "2in"
    }},
    { rule: _("margin-top: 2in;"), expected: {"margin-top": "2in"}},
    { rule: _("margin-left: 2in;"), expected: {"margin-left": "2in"}},
    { rule: _("margin-bottom: 2in;"), expected: {"margin-bottom": "2in"}},
    { rule: _("margin-right: 2in;"), expected: {"margin-right": "2in"}}
  ];

  var display = document.getElementById("display");
  var sheet = document.styleSheets[1];

  for (var curTest = 0; curTest < testset.length; curTest++) {
    try {
      while(sheet.cssRules.length > 0)
        sheet.deleteRule(0);
      sheet.insertRule(testset[curTest].rule, 0);
    } catch (e) {
      ok(e.name == "SyntaxError"
         && e instanceof DOMException
         && e.code == DOMException.SYNTAX_ERR
         && !('expected' in testset[curTest]),
         testset[curTest].rule + " syntax error thrown", e);
    }

    try {
      if (testset[curTest].expected) {
        is(sheet.cssRules.length, 1,
           testset[curTest].rule + " rule count");
           is(sheet.cssRules[0].type, CSSRule.PAGE_RULE,
           testset[curTest].rule + " rule type");

        var expected = testset[curTest].expected;
        var s = sheet.cssRules[0].style;
        var n = 0;

        // everything is set that should be
        for (var name in expected) {
          is(s.getPropertyValue(name), expected[name],
             testset[curTest].rule + " (prop " + name + ")");
          n++;
        }
        // nothing else is set
        is(s.length, n, testset[curTest].rule + "prop count");
        for (var i = 0; i < s.length; i++) {
          ok(s[i] in expected, testset[curTest].rule,
             "Unexpected item #" + i + ": " + s[i]);
        }
      } else {
        if (sheet.cssRules.length == 0) {
          is(sheet.cssRules.length, 0,
             testset[curTest].rule + " rule count (0)");
        } else {
          is(sheet.cssRules.length, 1,
             testset[curTest].rule + " rule count (1 non-page)");
          isnot(sheet.cssRules[0].type, CSSRule.PAGE_RULE,
                testset[curTest].rule + " rule type (1 non-page)");
        }
      }
    } catch (e) {
      ok(false, testset[curTest].rule, "During test: " + e);
    }
  }
</script>
</body>
</html>