summaryrefslogtreecommitdiffstats
path: root/layout/inspector/tests/test_is_valid_css_color.html
blob: d18a9ee384b512a9b05322861bea6f6297544988 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test InspectorUtils::isValidCSSColor</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">
  const InspectorUtils = SpecialPowers.InspectorUtils;

  // Color names
  // XXX getCSSValuesForProperty no longer returns the complete color
  //     keyword list, so skip it for now.
  if (false) {
    let colors = InspectorUtils.getCSSValuesForProperty("color");
    let notColor = ["hsl", "hsla", "inherit", "initial", "rgb", "rgba",
                    "unset", "transparent", "currentColor"];
    for (let color of colors) {
      if (notColor.includes(color)) {
        continue;
      }
      ok(InspectorUtils.isValidCSSColor(color), color + " is a valid color");
      ok(!InspectorUtils.isValidCSSColor("xxx" + color), "xxx" + color + " is not a valid color");
    }
  }

  // rgb(a)
  for (let i = 0; i <= 265; i++) {
    ok(InspectorUtils.isValidCSSColor("rgb(" + i + ",0,0)"), "rgb(" + i + ",0,0) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgb(0," + i + ",0)"), "rgb(0," + i + ",0) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgb(0,0," + i + ")"), "rgb(0,0," + i + ") is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(" + i + ",0,0,0.2)"), "rgba(" + i + ",0,0,0.2) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(0," + i + ",0,0.5)"), "rgba(0," + i + ",0,0.5) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(0,0," + i + ",0.7)"), "rgba(0,0," + i + ",0.7) is a valid color");

    ok(!InspectorUtils.isValidCSSColor("rgbxxx(" + i + ",0,0)"), "rgbxxx(" + i + ",0,0) is not a valid color");
    ok(!InspectorUtils.isValidCSSColor("rgbxxx(0," + i + ",0)"), "rgbxxx(0," + i + ",0) is not a valid color");
    ok(!InspectorUtils.isValidCSSColor("rgbxxx(0,0," + i + ")"), "rgbxxx(0,0," + i + ") is not a valid color");
  }

  // rgb(a) (%)
  for (let i = 0; i <= 110; i++) {
    ok(InspectorUtils.isValidCSSColor("rgb(" + i + "%,0%,0%)"), "rgb(" + i + "%,0%,0%) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgb(0%," + i + "%,0%)"), "rgb(0%," + i + "%,0%) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgb(0%,0%," + i + "%)"), "rgb(0%,0%," + i + "%) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(" + i + "%,0%,0%,0.2)"), "rgba(" + i + "%,0%,0%,0.2) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(0%," + i + "%,0%,0.5)"), "rgba(0%," + i + "%,0%,0.5) is a valid color");
    ok(InspectorUtils.isValidCSSColor("rgba(0%,0%," + i + "%,0.7)"), "rgba(0%,0%," + i + "%,0.7) is a valid color");

    ok(!InspectorUtils.isValidCSSColor("rgbaxxx(" + i + "%,0%,0%,0.2)"), "rgbaxxx(" + i + "%,0%,0%,0.2) is not a valid color");
    ok(!InspectorUtils.isValidCSSColor("rgbaxxx(0%," + i + "%,0%,0.5)"), "rgbaxxx(0%," + i + "%,0%,0.5) is not a valid color");
    ok(!InspectorUtils.isValidCSSColor("rgbaxxx(0%,0%," + i + "%,0.7)"), "rgbaxxx(0%,0%," + i + "%,0.7) is not a valid color");
  }

  // hsl(a)
  for (let i = 0; i <= 370; i++) {
    ok(InspectorUtils.isValidCSSColor("hsl(" + i + ",30%,10%)"), "rgb(" + i + ",30%,10%) is a valid color");
    ok(InspectorUtils.isValidCSSColor("hsla(" + i + ",60%,70%,0.2)"), "rgba(" + i + ",60%,70%,0.2) is a valid color");
  }
  for (let i = 0; i <= 110; i++) {
    ok(InspectorUtils.isValidCSSColor("hsl(100," + i + "%,20%)"), "hsl(100," + i + "%,20%) is a valid color");
    ok(InspectorUtils.isValidCSSColor("hsla(100,20%," + i + "%,0.6)"), "hsla(100,20%," + i + "%,0.6) is a valid color");
  }

  // hex
  for (let i = 0; i <= 255; i++) {
    let hex = (i).toString(16);
    if (hex.length === 1) {
      hex = 0 + hex;
    }
    ok(InspectorUtils.isValidCSSColor("#" + hex + "7777"), "#" + hex + "7777 is a valid color");
    ok(InspectorUtils.isValidCSSColor("#77" + hex + "77"), "#77" + hex + "77 is a valid color");
    ok(InspectorUtils.isValidCSSColor("#7777" + hex), "#7777" + hex + " is a valid color");
  }
  ok(!InspectorUtils.isValidCSSColor("#kkkkkk"), "#kkkkkk is not a valid color");

  // short hex
  for (let i = 0; i <= 16; i++) {
    let hex = (i).toString(16);
    ok(InspectorUtils.isValidCSSColor("#" + hex + hex + hex), "#" + hex + hex + hex + " is a valid color");
  }
  ok(!InspectorUtils.isValidCSSColor("#ggg"), "#ggg is not a valid color");

  // named
  ok(InspectorUtils.isValidCSSColor("red"), "red is a valid color");
  ok(InspectorUtils.isValidCSSColor("transparent"), "transparent is a valid color");
  ok(InspectorUtils.isValidCSSColor("currentColor"), "currentColor is a valid color");
  </script>
</head>
<body>
<h1>Test InspectorUtils::isValidCSSColor</h1>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>