summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_style_cssText.html
blob: 869063020a1864ed1fe26670a9af015692e5f911 (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
<html>
<head>
<meta charset="UTF-8">
<title>Test for Bug 1391169</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style id="style"></style>
</head>
<body>
<pre id="log">
Log is:
</pre>
<script>
let styleElement = document.getElementById("style");
let logElement = document.getElementById("log");
console.log("logElement is " + logElement);

function log(text)
{
  logElement.innerHTML += text + "\n";
}

function textContentToCssText(text)
{
  // Pass input in via textContent.
  styleElement.textContent = text;

  // Read output from concatenated cssText of all rules.
  let s = "";
  let rules = document.styleSheets[1].cssRules;
  for (let i = 0; i < rules.length; ++i) {
    s += rules.item(i).cssText;
  }
  return s;
}

function noWhitespace(text)
{
  return text.replace(/\s/g, "");
}

function testData(input)
{
  let text;
  let pass1Goal;
  if (typeof(input) == "string") {
    // Only text data, assume characters should be the same.
    text = input;
    pass1Goal = input;
  } else {
    [text, pass1Goal] = input;
  }

  let pass1Text = textContentToCssText(text);
  is(noWhitespace(pass1Text), noWhitespace(pass1Goal), "textContent --> cssText correct characters emitted with input \"" + text + "\"");

  let pass2Text = textContentToCssText(pass1Text);
  is(pass2Text, pass1Text, "textContent --> cssText roundtrip with input \"" + text + "\"");

  log(text + " --> " + pass1Text + " --> " + pass2Text);
}

let data = [
  "*{}",
  "* *{}",
  "* > *{}",
  "*>*{}",
  "* * *{}",
  "* > * > *{}",
  "* + *{}",
  "* ~ *{}",
  ["*|*{}", "*{}"],
  ["*|* > *{}", "* > *{}"],
  "#tag{}",
  "tag{}",
  "@namespace tag url(\"fakeURL\"); tag|*{}",
  "@namespace tag url(\"fakeURL\"); tag|* + *{}",
];

for (let i = 0; i < data.length; i++) {
  testData(data[i]);
}
</script>
</body>
</html>