Skip to content

Commit dfd9ccf

Browse files
bomsyagoloman
authored andcommitted
Bug 2041780 - [devtools] Pretty printing stylesheets in the debugger r=devtools-reviewers,ochameau
Differential Revision: https://phabricator.services.mozilla.com/D305022
1 parent 59ba940 commit dfd9ccf

6 files changed

Lines changed: 151 additions & 14 deletions

File tree

devtools/client/debugger/src/actions/sources/prettyPrint.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import {
1010
updateBreakpointPositionsForNewPrettyPrintedSource,
1111
updateBreakpointsForNewPrettyPrintedSource,
1212
} from "../breakpoints/index";
13-
13+
const {
14+
prettifyCSS,
15+
} = require("resource://devtools/shared/inspector/css-logic.js");
1416
import {
1517
getPrettySourceURL,
16-
isJavaScript,
18+
isNotPrettyPrintable,
1719
isMinified,
1820
} from "../../utils/source";
1921
import { isFulfilled, fulfilled } from "../../utils/async-value";
@@ -79,11 +81,11 @@ export async function prettyPrintSourceTextContent(
7981

8082
const contentValue = content.value;
8183
if (
82-
(!isJavaScript(generatedSource, contentValue) && !generatedSource.isHTML) ||
84+
isNotPrettyPrintable(generatedSource, contentValue) ||
8385
contentValue.type !== "text"
8486
) {
8587
throw new Error(
86-
`Can't prettify ${contentValue.contentType} files, only HTML and Javascript.`
88+
`Can't prettify ${contentValue.contentType} files, only HTML, Javascript and CSS.`
8789
);
8890
}
8991

@@ -97,6 +99,12 @@ export async function prettyPrintSourceTextContent(
9799
content,
98100
actors,
99101
});
102+
} else if (generatedSource.isStyleSheet) {
103+
const { result } = prettifyCSS(contentValue.value, null);
104+
return {
105+
text: result,
106+
contentType: contentValue.contentType,
107+
};
100108
} else {
101109
prettyPrintWorkerResult = await prettyPrintWorker.prettyPrint({
102110
sourceText: contentValue.value,

devtools/client/debugger/src/selectors/sources.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { createSelector } from "devtools/client/shared/vendor/reselect";
66

7-
import { getPrettySourceURL, isJavaScript } from "../utils/source";
7+
import { getPrettySourceURL, isNotPrettyPrintable } from "../utils/source";
88

99
import { findPosition } from "../utils/breakpoint/breakpointPositions";
1010
import { isFulfilled } from "../utils/async-value";
@@ -255,10 +255,7 @@ export function canPrettyPrintSource(state, source, sourceActor) {
255255
const content = getSourceTextContentForSource(state, source, sourceActor);
256256
const sourceContent = content && isFulfilled(content) ? content.value : null;
257257

258-
if (
259-
!sourceContent ||
260-
(!isJavaScript(source, sourceContent) && !source.isHTML)
261-
) {
258+
if (!sourceContent || isNotPrettyPrintable(source, sourceContent)) {
262259
return false;
263260
}
264261

@@ -290,8 +287,10 @@ export function getPrettyPrintMessage(state, location) {
290287
return L10N.getStr("sourceFooter.prettyPrint.noContentMessage");
291288
}
292289

293-
if (!isJavaScript(source, sourceContent) && !source.isHTML) {
294-
return L10N.getStr("sourceFooter.prettyPrint.isNotJavascriptMessage");
290+
if (isNotPrettyPrintable(source, sourceContent)) {
291+
return L10N.getStr(
292+
"sourceFooter.prettyPrint.isNotPrettyPrintableSourceMessage"
293+
);
295294
}
296295

297296
return L10N.getStr("sourceTabs.prettyPrint");

devtools/client/debugger/src/utils/source.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ export function isJavaScript(source, content) {
163163
);
164164
}
165165

166+
export function isNotPrettyPrintable(source, sourceContent) {
167+
return (
168+
!isJavaScript(source, sourceContent) &&
169+
!source.isHTML &&
170+
!source.isStyleSheet
171+
);
172+
}
173+
166174
export function isPrettyURL(url) {
167175
return url ? url.endsWith(":formatted") : false;
168176
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
4+
5+
// Tests basic pretty-printing stylesheets.
6+
7+
"use strict";
8+
9+
const httpServer = createTestHTTPServer();
10+
const BASE_URL = `http://localhost:${httpServer.identity.primaryPort}/`;
11+
12+
httpServer.registerContentType("html", "text/html");
13+
14+
httpServer.registerPathHandler("/index.html", (request, response) => {
15+
response.setStatusLine(request.httpVersion, 200, "OK");
16+
response.write(`<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<link rel="stylesheet" href="/style.css">
20+
</head>
21+
<body>
22+
</body>
23+
</html>
24+
`);
25+
});
26+
27+
httpServer.registerPathHandler("/style.css", (request, response) => {
28+
response.setHeader("Content-Type", "text/css");
29+
response.write(
30+
`body{background:white;}div{font-size:4em;color:red}span{color:green;@media screen { background: blue; &>.myClass {padding: 1em} }}`
31+
);
32+
});
33+
34+
("use strict");
35+
36+
add_task(async function () {
37+
await pushPref("devtools.debugger.features.stylesheets-in-debugger", true);
38+
const dbg = await initDebuggerWithAbsoluteURL(
39+
BASE_URL + "index.html",
40+
"style.css"
41+
);
42+
43+
const MINIFIED_CSS_TEXT =
44+
"body{background:white;}div{font-size:4em;color:red}span{color:green;@media screen { background: blue; &>.myClass {padding: 1em} }}";
45+
const PRETTIFIED_CSS_TEXT = `
46+
body {
47+
background:white;
48+
}
49+
div {
50+
font-size:4em;
51+
color:red
52+
}
53+
span {
54+
color:green;
55+
@media screen {
56+
background: blue;
57+
&>.myClass {
58+
padding: 1em
59+
}
60+
}
61+
}
62+
`.trimStart();
63+
64+
await selectSource(dbg, "style.css", 2);
65+
66+
is(
67+
getLineCount(dbg),
68+
1,
69+
`The minified style sheet should have a single line`
70+
);
71+
72+
is(getEditorContent(dbg), MINIFIED_CSS_TEXT, "minified source is correct");
73+
74+
let prettyPrintButton = findElement(dbg, "prettyPrintButton");
75+
ok(!prettyPrintButton.disabled, "The pretty print button should be enabled");
76+
77+
ok(
78+
!prettyPrintButton.classList.contains("pretty"),
79+
"The pretty print button should not be enabled"
80+
);
81+
82+
await togglePrettyPrint(dbg);
83+
84+
is(
85+
getLineCount(dbg),
86+
17,
87+
`The pretty printed style sheet should the expected nunber of lines`
88+
);
89+
90+
is(
91+
getEditorContent(dbg),
92+
PRETTIFIED_CSS_TEXT,
93+
"minified source has been prettified automatically"
94+
);
95+
96+
ok(
97+
prettyPrintButton.classList.contains("pretty"),
98+
"The pretty print button should be enabled"
99+
);
100+
101+
await togglePrettyPrint(dbg);
102+
103+
prettyPrintButton = findElement(dbg, "prettyPrintButton");
104+
is(
105+
getLineCount(dbg),
106+
1,
107+
"The minified style sheet should have a single line"
108+
);
109+
110+
is(
111+
getEditorContent(dbg),
112+
MINIFIED_CSS_TEXT,
113+
"minified source is still correct"
114+
);
115+
116+
ok(
117+
!prettyPrintButton.classList.contains("pretty"),
118+
"The pretty print button should not be enabled"
119+
);
120+
});

devtools/client/debugger/test/mochitest/browser_gp.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ skip-if = [
158158
"os == 'win' && os_version == '11.26200' && arch == 'x86_64' && ccov", # Bug 1817966
159159
]
160160

161+
["browser_dbg-pretty-print-stylesheet.js"]
162+
161163
["browser_dbg-pretty-print.js"]
162164

163165
["browser_dbg-preview-bucketed-array.js"]

devtools/client/locales/en-US/debugger.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,9 @@ sourceFooter.prettyPrint.hasSourceMapMessage=Can’t pretty print generated sour
720720
# pretty print button in editor footer. This displays when the file has no content.
721721
sourceFooter.prettyPrint.noContentMessage=Can’t pretty print, file has no content
722722

723-
# LOCALIZATION NOTE (sourceFooter.prettyPrint.isNotJavascriptMessage): Tooltip text for the disabled
724-
# pretty print button in editor footer. This displays when the file is not JavaScript code.
725-
sourceFooter.prettyPrint.isNotJavascriptMessage=Can’t pretty print, file is not JavaScript
723+
# LOCALIZATION NOTE (sourceFooter.prettyPrint.isNotPrettyPrintableSourceMessage): Tooltip text for the disabled
724+
# pretty print button in editor footer. This displays when the file is not JavaScript or CSS code.
725+
sourceFooter.prettyPrint.isNotPrettyPrintableSourceMessage=Can’t pretty print, file is not JavaScript or CSS
726726

727727
# LOCALIZATION NOTE (sourceFooter.ignores): Tooltip text associated
728728
# with the ignores button

0 commit comments

Comments
 (0)