summaryrefslogtreecommitdiffstats
path: root/devtools/shared/tests/xpcshell/test_prettifyCSS.js
blob: 3739c074629a2f7216add126b85e1f253b1d27de (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test prettifyCSS.

"use strict";

const {
  prettifyCSS,
} = require("resource://devtools/shared/inspector/css-logic.js");

const EXPAND_TAB = "devtools.editor.expandtab";

const TESTS_TAB_INDENT = [
  {
    name: "simple test. indent using tabs",
    input: "div { font-family:'Arial Black', Arial, sans-serif; }",
    expected: ["div {", "\tfont-family:'Arial Black', Arial, sans-serif;", "}"],
  },

  {
    name: "whitespace before open brace. indent using tabs",
    input: "div{}",
    expected: ["div {", "}"],
  },

  {
    name: "minified with trailing newline. indent using tabs",
    input:
      "\nbody{background:white;}div{font-size:4em;color:red}span{color:green;}\n",
    expected: [
      "body {",
      "\tbackground:white;",
      "}",
      "div {",
      "\tfont-size:4em;",
      "\tcolor:red",
      "}",
      "span {",
      "\tcolor:green;",
      "}",
    ],
  },

  {
    name: "leading whitespace. indent using tabs",
    input: "\n    div{color: red;}",
    expected: ["div {", "\tcolor: red;", "}"],
  },

  {
    name: "CSS with extra closing brace. indent using tabs",
    input: "body{margin:0}} div{color:red}",
    expected: ["body {", "\tmargin:0", "}", "}", "div {", "\tcolor:red", "}"],
  },
];

const TESTS_SPACE_INDENT = [
  {
    name: "simple test. indent using spaces",
    input: "div { font-family:'Arial Black', Arial, sans-serif; }",
    expected: ["div {", " font-family:'Arial Black', Arial, sans-serif;", "}"],
  },

  {
    name: "whitespace before open brace. indent using spaces",
    input: "div{}",
    expected: ["div {", "}"],
  },

  {
    name: "minified with trailing newline. indent using spaces",
    input:
      "\nbody{background:white;}div{font-size:4em;color:red}span{color:green;}\n",
    expected: [
      "body {",
      " background:white;",
      "}",
      "div {",
      " font-size:4em;",
      " color:red",
      "}",
      "span {",
      " color:green;",
      "}",
    ],
  },

  {
    name: "leading whitespace. indent using spaces",
    input: "\n    div{color: red;}",
    expected: ["div {", " color: red;", "}"],
  },

  {
    name: "CSS with extra closing brace. indent using spaces",
    input: "body{margin:0}} div{color:red}",
    expected: ["body {", " margin:0", "}", "}", "div {", " color:red", "}"],
  },

  {
    name: "HTML comments with some whitespace padding",
    input: "  \n\n\t  <!--\n\n\t body {color:red}  \n\n-->   \t\n",
    expected: ["body {", " color:red", "}"],
  },

  {
    name: "HTML comments without whitespace padding",
    input: "<!--body {color:red}-->",
    expected: ["body {", " color:red", "}"],
  },

  {
    name: "Breaking after commas in selectors",
    input:
      "@media screen, print {div, span, input {color: red;}}" +
      "div, div, input, pre, table {color: blue;}",
    expected: [
      "@media screen, print {",
      " div,",
      " span,",
      " input {",
      "  color: red;",
      " }",
      "}",
      "div,",
      "div,",
      "input,",
      "pre,",
      "table {",
      " color: blue;",
      "}",
    ],
  },

  {
    name: "Multiline comment in CSS",
    input: "/*\n * comment\n */\n#example{display:grid;}",
    expected: ["/*", " * comment", " */", "#example {", " display:grid;", "}"],
  },
];

function run_test() {
  // Note that prettifyCSS.LINE_SEPARATOR is computed lazily, so we
  // ensure it is set.
  prettifyCSS("");

  Services.prefs.setBoolPref(EXPAND_TAB, true);
  for (const test of TESTS_SPACE_INDENT) {
    info(test.name);

    const input = test.input.split("\n").join(prettifyCSS.LINE_SEPARATOR);
    const { result: output } = prettifyCSS(input);
    const expected =
      test.expected.join(prettifyCSS.LINE_SEPARATOR) +
      prettifyCSS.LINE_SEPARATOR;
    equal(output, expected, test.name);
  }

  Services.prefs.setBoolPref(EXPAND_TAB, false);
  for (const test of TESTS_TAB_INDENT) {
    info(test.name);

    const input = test.input.split("\n").join(prettifyCSS.LINE_SEPARATOR);
    const { result: output } = prettifyCSS(input);
    const expected =
      test.expected.join(prettifyCSS.LINE_SEPARATOR) +
      prettifyCSS.LINE_SEPARATOR;
    equal(output, expected, test.name);
  }
  Services.prefs.clearUserPref(EXPAND_TAB);
}