From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- devtools/shared/tests/xpcshell/test_prettifyCSS.js | 172 +++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 devtools/shared/tests/xpcshell/test_prettifyCSS.js (limited to 'devtools/shared/tests/xpcshell/test_prettifyCSS.js') diff --git a/devtools/shared/tests/xpcshell/test_prettifyCSS.js b/devtools/shared/tests/xpcshell/test_prettifyCSS.js new file mode 100644 index 0000000000..3739c07462 --- /dev/null +++ b/devtools/shared/tests/xpcshell/test_prettifyCSS.js @@ -0,0 +1,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 \t\n", + expected: ["body {", " color:red", "}"], + }, + + { + name: "HTML comments without whitespace padding", + input: "", + 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); +} -- cgit v1.2.3