|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Lit from '../../third_party/lit/lit.js'; |
| 6 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 7 | + |
| 8 | +import cssValueTraceViewStyles from './cssValueTraceView.css.js'; |
| 9 | + |
| 10 | +const {html, render} = Lit; |
| 11 | + |
| 12 | +export interface ViewInput { |
| 13 | + substitutions: Node[][]; |
| 14 | + evaluations: Node[][]; |
| 15 | + finalResult: Node[]|undefined; |
| 16 | + onToggle: () => void; |
| 17 | +} |
| 18 | +export interface ViewOutput {} |
| 19 | + |
| 20 | +export type View = ( |
| 21 | + input: ViewInput, |
| 22 | + output: ViewOutput, |
| 23 | + target: HTMLElement, |
| 24 | + ) => void; |
| 25 | + |
| 26 | +export class CSSValueTraceView extends UI.Widget.VBox { |
| 27 | + #view: View; |
| 28 | + #finalResult: Node[]|undefined = undefined; |
| 29 | + #evaluations: Node[][] = []; |
| 30 | + #substitutions: Node[][] = []; |
| 31 | + |
| 32 | + constructor( |
| 33 | + view: View = (input, output, target): |
| 34 | + void => { |
| 35 | + const substitutionIcon = html`<span class=trace-line-icon aria-label="resolved to">\u21B3</span>`; |
| 36 | + const evalIcon = html`<span class=trace-line-icon aria-label="is equal to">\u003D</span>`; |
| 37 | + const [firstEvaluation, ...intermediateEvaluations] = input.evaluations; |
| 38 | + render( |
| 39 | + // clang-format off |
| 40 | + html` |
| 41 | +<div class="css-value-trace monospace"> |
| 42 | + ${input.substitutions.map(line => html`${substitutionIcon}<span class="trace-line">${line}</span>`)} |
| 43 | + ${ |
| 44 | + firstEvaluation && intermediateEvaluations.length === 0 |
| 45 | + ? html`${evalIcon}<span class="trace-line">${firstEvaluation}</span>` |
| 46 | + : html`<details |
| 47 | + @toggle=${input.onToggle} |
| 48 | + ?hidden=${!firstEvaluation || intermediateEvaluations.length === 0}> |
| 49 | + <summary> |
| 50 | + ${evalIcon}<devtools-icon class=marker></devtools-icon><span class="trace-line">${firstEvaluation}</span> |
| 51 | + </summary> |
| 52 | + <div> |
| 53 | + ${intermediateEvaluations.map(evaluation => html`${evalIcon}<span class="trace-line">${evaluation}</span>`)} |
| 54 | + </div> |
| 55 | + </details>` |
| 56 | + } |
| 57 | + ${!input.finalResult ? '' : html`${evalIcon}<span class="trace-line">${input.finalResult}</span>`} |
| 58 | +</div> |
| 59 | +`, |
| 60 | + // clang-format on |
| 61 | + target, |
| 62 | + ); |
| 63 | + }, |
| 64 | + ) { |
| 65 | + super(true); |
| 66 | + this.registerRequiredCSS(cssValueTraceViewStyles); |
| 67 | + this.#view = view; |
| 68 | + this.requestUpdate(); |
| 69 | + } |
| 70 | + |
| 71 | + showTrace( |
| 72 | + substitutions: Node[][], |
| 73 | + evaluations: Node[][], |
| 74 | + finalResult: Node[]|undefined, |
| 75 | + ): void { |
| 76 | + this.#substitutions = substitutions; |
| 77 | + this.#evaluations = evaluations; |
| 78 | + this.#finalResult = finalResult; |
| 79 | + this.requestUpdate(); |
| 80 | + } |
| 81 | + |
| 82 | + override performUpdate(): void { |
| 83 | + const viewInput: ViewInput = { |
| 84 | + substitutions: this.#substitutions, |
| 85 | + evaluations: this.#evaluations, |
| 86 | + finalResult: this.#finalResult, |
| 87 | + onToggle: () => this.onResize(), |
| 88 | + }; |
| 89 | + const viewOutput = {}; |
| 90 | + this.#view(viewInput, viewOutput, this.contentElement); |
| 91 | + } |
| 92 | +} |
0 commit comments