Skip to content

Commit 1c38075

Browse files
pfaffeDevtools-frontend LUCI CQ
authored andcommitted
[styles] pretty-print binary expressions
Fixed: 399818714 Change-Id: Ifdc0d2707c5fb259ff526bbcb0f88ccda1605e6b Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6311458 Auto-Submit: Philip Pfaffe <[email protected]> Commit-Queue: Philip Pfaffe <[email protected]> Reviewed-by: Eric Leese <[email protected]>
1 parent 3738fe0 commit 1c38075

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

front_end/core/sdk/CSSProperty.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
AngleMatcher,
1919
AutoBaseMatcher,
2020
BezierMatcher,
21+
BinOpMatcher,
2122
ColorMatcher,
2223
ColorMixMatcher,
2324
CSSWideKeywordMatcher,
@@ -138,6 +139,7 @@ export class CSSProperty extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
138139
new LengthMatcher(),
139140
new MathFunctionMatcher(),
140141
new AutoBaseMatcher(),
142+
new BinOpMatcher(),
141143
];
142144

143145
if (Root.Runtime.experiments.isEnabled('font-editor')) {

front_end/core/sdk/CSSPropertyParserMatchers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ export class VariableMatcher extends matcherBase(VariableMatch) {
141141
}
142142
}
143143

144+
export class BinOpMatch implements Match {
145+
constructor(readonly text: string, readonly node: CodeMirror.SyntaxNode) {
146+
}
147+
}
148+
149+
// clang-format off
150+
export class BinOpMatcher extends matcherBase(BinOpMatch) {
151+
// clang-format on
152+
override accepts(): boolean {
153+
return true;
154+
}
155+
override matches(node: CodeMirror.SyntaxNode, matching: BottomUpTreeMatching): BinOpMatch|null {
156+
return node.name === 'BinaryExpression' ? new BinOpMatch(matching.ast.text(node), node) : null;
157+
}
158+
}
159+
144160
export class TextMatch implements Match {
145161
computedText?: () => string;
146162
constructor(readonly text: string, readonly node: CodeMirror.SyntaxNode) {

front_end/panels/elements/PropertyRenderer.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ describeWithEnvironment('PropertyRenderer', () => {
5454
Printer.walk(ast).get());
5555
});
5656

57+
it('nicely formats binary expressions', () => {
58+
const property = 'calc((50 -(0* 4))* 1vmin)';
59+
const rule = `*{--property: ${property};}`;
60+
const tree = cssParser.parse(rule).topNode;
61+
const ast = new SDK.CSSPropertyParser.SyntaxTree(property, rule, tree);
62+
const matchedResult =
63+
SDK.CSSPropertyParser.BottomUpTreeMatching.walk(ast, [new SDK.CSSPropertyParserMatchers.BinOpMatcher()]);
64+
const renderer = new Elements.PropertyRenderer.BinOpRenderer();
65+
const context =
66+
new Elements.PropertyRenderer.RenderingContext(ast, new Map([[renderer.matchType, renderer]]), matchedResult);
67+
assert.deepEqual(
68+
textFragments(Elements.PropertyRenderer.Renderer.render(tree, context).nodes).join(''),
69+
'*{--property: calc((50 - (0 * 4)) * 1vmin);}', Printer.walk(ast).get());
70+
});
71+
5772
it('correctly renders subtrees', () => {
5873
const property = '2px var(--double, var(--fallback, black)) #32a1ce rgb(124 125 21 0)';
5974
const rule = `*{--property: ${property};}`;

front_end/panels/elements/PropertyRenderer.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,6 @@ export class URLRenderer extends rendererBase(SDK.CSSPropertyParserMatchers.URLM
355355
UI.UIUtils.createTextChild(container, ')');
356356
return [container];
357357
}
358-
359-
matcher(): SDK.CSSPropertyParserMatchers.URLMatcher {
360-
return new SDK.CSSPropertyParserMatchers.URLMatcher();
361-
}
362358
}
363359

364360
// clang-format off
@@ -370,8 +366,15 @@ export class StringRenderer extends rendererBase(SDK.CSSPropertyParserMatchers.S
370366
UI.Tooltip.Tooltip.install(element, unescapeCssString(match.text));
371367
return [element];
372368
}
369+
}
370+
371+
// clang-format off
372+
export class BinOpRenderer extends rendererBase(SDK.CSSPropertyParserMatchers.BinOpMatch) {
373+
// clang-format on
374+
override render(match: SDK.CSSPropertyParserMatchers.BinOpMatch, context: RenderingContext): Node[] {
375+
const [lhs, binop, rhs] =
376+
SDK.CSSPropertyParser.ASTUtils.children(match.node).map(child => Renderer.render(child, context).nodes);
373377

374-
matcher(): SDK.CSSPropertyParserMatchers.StringMatcher {
375-
return new SDK.CSSPropertyParserMatchers.StringMatcher();
378+
return [lhs, document.createTextNode(' '), binop, document.createTextNode(' '), rhs].flat();
376379
}
377380
}

front_end/panels/elements/StylePropertyTreeElement.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {cssRuleValidatorsMap, type Hint} from './CSSRuleValidator.js';
2929
import {CSSValueTraceView} from './CSSValueTraceView.js';
3030
import {ElementsPanel} from './ElementsPanel.js';
3131
import {
32+
BinOpRenderer,
3233
type MatchRenderer,
3334
Renderer,
3435
rendererBase,
@@ -1485,6 +1486,7 @@ export function getPropertyRenderers(
14851486
new LengthRenderer(stylesPane),
14861487
new MathFunctionRenderer(stylesPane),
14871488
new AutoBaseRenderer(computedStyles),
1489+
new BinOpRenderer(),
14881490
];
14891491
}
14901492

0 commit comments

Comments
 (0)