Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Tim van der Lippe | 7696157 | 2021-04-06 10:48:07 | [diff] [blame] | 5 | import * as Common from '../../core/common/common.js'; |
Tim van der Lippe | bb352e6 | 2021-04-01 17:57:28 | [diff] [blame] | 6 | import * as i18n from '../../core/i18n/i18n.js'; |
Tim van der Lippe | aa1ed7a | 2021-03-31 14:38:27 | [diff] [blame] | 7 | import * as Platform from '../../core/platform/platform.js'; |
Tim van der Lippe | d8caac4 | 2021-03-31 14:40:44 | [diff] [blame] | 8 | import * as Root from '../../core/root/root.js'; |
Tim van der Lippe | e00b92f | 2021-03-31 16:52:17 | [diff] [blame] | 9 | import * as SDK from '../../core/sdk/sdk.js'; |
Tim van der Lippe | fca98ed | 2021-04-08 14:10:14 | [diff] [blame] | 10 | import * as TextUtils from '../../models/text_utils/text_utils.js'; |
Tim van der Lippe | 8499fe2 | 2021-04-12 16:42:47 | [diff] [blame] | 11 | import * as DataGrid from '../../ui/legacy/components/data_grid/data_grid.js'; |
Tim van der Lippe | 339ad26 | 2021-04-21 12:23:36 | [diff] [blame] | 12 | import * as Components from '../../ui/legacy/components/utils/utils.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 13 | import * as UI from '../../ui/legacy/legacy.js'; |
Tim van der Lippe | 229a54f | 2021-05-14 16:59:05 | [diff] [blame] | 14 | import type * as Protocol from '../../generated/protocol.js'; |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 15 | |
Tim van der Lippe | f8c26e2 | 2021-11-03 11:46:58 | [diff] [blame] | 16 | import cssOverviewCompletedViewStyles from './cssOverviewCompletedView.css.js'; |
Jack Franklin | e839c0c | 2022-05-03 08:47:44 | [diff] [blame^] | 17 | import type { |
| 18 | OverviewController, PopulateNodesEvent, PopulateNodesEventNodes, PopulateNodesEventNodeTypes} from |
| 19 | './CSSOverviewController.js'; |
Kateryna Prokopenko | 4a80253 | 2021-09-16 08:57:06 | [diff] [blame] | 20 | import {Events as CSSOverViewControllerEvents} from './CSSOverviewController.js'; |
Tim van der Lippe | 9d0cb5f | 2020-01-09 14:10:38 | [diff] [blame] | 21 | import {CSSOverviewSidebarPanel, SidebarEvents} from './CSSOverviewSidebarPanel.js'; |
Tim van der Lippe | bfbb58f | 2021-02-25 17:34:19 | [diff] [blame] | 22 | import type {UnusedDeclaration} from './CSSOverviewUnusedDeclarations.js'; |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 23 | |
Simon Zünd | fbfd107 | 2021-03-01 07:38:53 | [diff] [blame] | 24 | const UIStrings = { |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 25 | /** |
| 26 | *@description Label for the summary in the CSS Overview report |
| 27 | */ |
| 28 | overviewSummary: 'Overview summary', |
| 29 | /** |
| 30 | *@description Title of colors subsection in the CSS Overview Panel |
| 31 | */ |
| 32 | colors: 'Colors', |
| 33 | /** |
| 34 | *@description Title of font info subsection in the CSS Overview Panel |
| 35 | */ |
| 36 | fontInfo: 'Font info', |
| 37 | /** |
| 38 | *@description Label to denote unused declarations in the target page |
| 39 | */ |
| 40 | unusedDeclarations: 'Unused declarations', |
| 41 | /** |
| 42 | *@description Label for the number of media queries in the CSS Overview report |
| 43 | */ |
| 44 | mediaQueries: 'Media queries', |
| 45 | /** |
| 46 | *@description Title of the Elements Panel |
| 47 | */ |
| 48 | elements: 'Elements', |
| 49 | /** |
| 50 | *@description Label for the number of External stylesheets in the CSS Overview report |
| 51 | */ |
| 52 | externalStylesheets: 'External stylesheets', |
| 53 | /** |
| 54 | *@description Label for the number of inline style elements in the CSS Overview report |
| 55 | */ |
| 56 | inlineStyleElements: 'Inline style elements', |
| 57 | /** |
| 58 | *@description Label for the number of style rules in CSS Overview report |
| 59 | */ |
| 60 | styleRules: 'Style rules', |
| 61 | /** |
| 62 | *@description Label for the number of type selectors in the CSS Overview report |
| 63 | */ |
| 64 | typeSelectors: 'Type selectors', |
| 65 | /** |
| 66 | *@description Label for the number of ID selectors in the CSS Overview report |
| 67 | */ |
| 68 | idSelectors: 'ID selectors', |
| 69 | /** |
| 70 | *@description Label for the number of class selectors in the CSS Overview report |
| 71 | */ |
| 72 | classSelectors: 'Class selectors', |
| 73 | /** |
| 74 | *@description Label for the number of universal selectors in the CSS Overview report |
| 75 | */ |
| 76 | universalSelectors: 'Universal selectors', |
| 77 | /** |
| 78 | *@description Label for the number of Attribute selectors in the CSS Overview report |
| 79 | */ |
| 80 | attributeSelectors: 'Attribute selectors', |
| 81 | /** |
| 82 | *@description Label for the number of non-simple selectors in the CSS Overview report |
| 83 | */ |
| 84 | nonsimpleSelectors: 'Non-simple selectors', |
| 85 | /** |
| 86 | *@description Label for unique background colors in the CSS Overview Panel |
| 87 | *@example {32} PH1 |
| 88 | */ |
| 89 | backgroundColorsS: 'Background colors: {PH1}', |
| 90 | /** |
| 91 | *@description Label for unique text colors in the CSS Overview Panel |
| 92 | *@example {32} PH1 |
| 93 | */ |
| 94 | textColorsS: 'Text colors: {PH1}', |
| 95 | /** |
| 96 | *@description Label for unique fill colors in the CSS Overview Panel |
| 97 | *@example {32} PH1 |
| 98 | */ |
| 99 | fillColorsS: 'Fill colors: {PH1}', |
| 100 | /** |
| 101 | *@description Label for unique border colors in the CSS Overview Panel |
| 102 | *@example {32} PH1 |
| 103 | */ |
| 104 | borderColorsS: 'Border colors: {PH1}', |
| 105 | /** |
| 106 | *@description Label to indicate that there are no fonts in use |
| 107 | */ |
| 108 | thereAreNoFonts: 'There are no fonts.', |
| 109 | /** |
| 110 | *@description Message to show when no unused declarations in the target page |
| 111 | */ |
| 112 | thereAreNoUnusedDeclarations: 'There are no unused declarations.', |
| 113 | /** |
| 114 | *@description Message to show when no media queries are found in the target page |
| 115 | */ |
| 116 | thereAreNoMediaQueries: 'There are no media queries.', |
| 117 | /** |
| 118 | *@description Title of the Drawer for contrast issues in the CSS Overview Panel |
| 119 | */ |
| 120 | contrastIssues: 'Contrast issues', |
| 121 | /** |
Peter Marshall | 645f7bf | 2021-03-04 09:56:20 | [diff] [blame] | 122 | * @description Text to indicate how many times this CSS rule showed up. |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 123 | */ |
Peter Marshall | 5166fad | 2021-03-04 10:06:49 | [diff] [blame] | 124 | nOccurrences: '{n, plural, =1 {# occurrence} other {# occurrences}}', |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 125 | /** |
| 126 | *@description Section header for contrast issues in the CSS Overview Panel |
| 127 | *@example {1} PH1 |
| 128 | */ |
| 129 | contrastIssuesS: 'Contrast issues: {PH1}', |
| 130 | /** |
| 131 | *@description Title of the button for a contrast issue in the CSS Overview Panel |
| 132 | *@example {#333333} PH1 |
| 133 | *@example {#333333} PH2 |
| 134 | *@example {2} PH3 |
| 135 | */ |
| 136 | textColorSOverSBackgroundResults: 'Text color {PH1} over {PH2} background results in low contrast for {PH3} elements', |
| 137 | /** |
| 138 | *@description Label aa text content in Contrast Details of the Color Picker |
| 139 | */ |
| 140 | aa: 'AA', |
| 141 | /** |
| 142 | *@description Label aaa text content in Contrast Details of the Color Picker |
| 143 | */ |
| 144 | aaa: 'AAA', |
| 145 | /** |
| 146 | *@description Label for the APCA contrast in Color Picker |
| 147 | */ |
| 148 | apca: 'APCA', |
| 149 | /** |
| 150 | *@description Label for the column in the element list in the CSS Overview report |
| 151 | */ |
| 152 | element: 'Element', |
| 153 | /** |
| 154 | *@description Column header title denoting which declaration is unused |
| 155 | */ |
| 156 | declaration: 'Declaration', |
| 157 | /** |
| 158 | *@description Text for the source of something |
| 159 | */ |
| 160 | source: 'Source', |
| 161 | /** |
| 162 | *@description Text of a DOM element in Contrast Details of the Color Picker |
| 163 | */ |
| 164 | contrastRatio: 'Contrast ratio', |
| 165 | /** |
Peter Marshall | 3d96738 | 2021-02-25 06:56:18 | [diff] [blame] | 166 | *@description Accessible title of a table in the CSS Overview Elements. |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 167 | */ |
| 168 | cssOverviewElements: 'CSS Overview Elements', |
| 169 | /** |
| 170 | *@description Title of the button to show the element in the CSS Overview panel |
| 171 | */ |
| 172 | showElement: 'Show element', |
| 173 | }; |
Tim van der Lippe | 1d7474a | 2021-03-19 15:41:06 | [diff] [blame] | 174 | const str_ = i18n.i18n.registerUIStrings('panels/css_overview/CSSOverviewCompletedView.ts', UIStrings); |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 175 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 176 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 177 | export type NodeStyleStats = Map<string, Set<number>>; |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 178 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 179 | export interface ContrastIssue { |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 180 | nodeId: Protocol.DOM.BackendNodeId; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 181 | contrastRatio: number; |
| 182 | textColor: Common.Color.Color; |
| 183 | backgroundColor: Common.Color.Color; |
| 184 | thresholdsViolated: { |
| 185 | aa: boolean, |
| 186 | aaa: boolean, |
| 187 | apca: boolean, |
| 188 | }; |
| 189 | } |
| 190 | export interface OverviewData { |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 191 | backgroundColors: Map<string, Set<Protocol.DOM.BackendNodeId>>; |
| 192 | textColors: Map<string, Set<Protocol.DOM.BackendNodeId>>; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 193 | textColorContrastIssues: Map<string, ContrastIssue[]>; |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 194 | fillColors: Map<string, Set<Protocol.DOM.BackendNodeId>>; |
| 195 | borderColors: Map<string, Set<Protocol.DOM.BackendNodeId>>; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 196 | globalStyleStats: { |
| 197 | styleRules: number, |
| 198 | inlineStyles: number, |
| 199 | externalSheets: number, |
| 200 | stats: {type: number, class: number, id: number, universal: number, attribute: number, nonSimple: number}, |
| 201 | }; |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 202 | fontInfo: Map<string, Map<string, Map<string, Protocol.DOM.BackendNodeId[]>>>; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 203 | elementCount: number; |
| 204 | mediaQueries: Map<string, Protocol.CSS.CSSMedia[]>; |
| 205 | unusedDeclarations: Map<string, UnusedDeclaration[]>; |
| 206 | } |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 207 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 208 | export type FontInfo = Map<string, Map<string, Map<string, number[]>>>; |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 209 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 210 | function getBorderString(color: Common.Color.Color): string { |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 211 | let [h, s, l] = color.hsla(); |
| 212 | h = Math.round(h * 360); |
| 213 | s = Math.round(s * 100); |
| 214 | l = Math.round(l * 100); |
| 215 | |
| 216 | // Reduce the lightness of the border to make sure that there's always a visible outline. |
| 217 | l = Math.max(0, l - 15); |
| 218 | |
| 219 | return `1px solid hsl(${h}deg ${s}% ${l}%)`; |
| 220 | } |
| 221 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 222 | export class CSSOverviewCompletedView extends UI.Panel.PanelWithSidebar { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 223 | #controller: OverviewController; |
| 224 | #formatter: Intl.NumberFormat; |
| 225 | readonly #mainContainer: UI.SplitWidget.SplitWidget; |
| 226 | readonly #resultsContainer: UI.Widget.VBox; |
| 227 | readonly #elementContainer: DetailsView; |
| 228 | readonly #sideBar: CSSOverviewSidebarPanel; |
Tim van der Lippe | f8c26e2 | 2021-11-03 11:46:58 | [diff] [blame] | 229 | #cssModel?: SDK.CSSModel.CSSModel; |
| 230 | #domModel?: SDK.DOMModel.DOMModel; |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 231 | #linkifier: Components.Linkifier.Linkifier; |
| 232 | #viewMap: Map<string, ElementDetailsView>; |
| 233 | #data: OverviewData|null; |
| 234 | #fragment?: UI.Fragment.Fragment; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 235 | |
Tim van der Lippe | f8c26e2 | 2021-11-03 11:46:58 | [diff] [blame] | 236 | constructor(controller: OverviewController) { |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 237 | super('css_overview_completed_view'); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 238 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 239 | this.#controller = controller; |
| 240 | this.#formatter = new Intl.NumberFormat('en-US'); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 241 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 242 | this.#mainContainer = new UI.SplitWidget.SplitWidget(true, true); |
| 243 | this.#resultsContainer = new UI.Widget.VBox(); |
| 244 | this.#elementContainer = new DetailsView(); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 245 | |
| 246 | // If closing the last tab, collapse the sidebar. |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 247 | this.#elementContainer.addEventListener(Events.TabClosed, evt => { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 248 | if (evt.data === 0) { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 249 | this.#mainContainer.setSidebarMinimized(true); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 250 | } |
| 251 | }); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 252 | |
| 253 | // Dupe the styles into the main container because of the shadow root will prevent outer styles. |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 254 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 255 | this.#mainContainer.setMainWidget(this.#resultsContainer); |
| 256 | this.#mainContainer.setSidebarWidget(this.#elementContainer); |
| 257 | this.#mainContainer.setVertical(false); |
| 258 | this.#mainContainer.setSecondIsSidebar(true); |
| 259 | this.#mainContainer.setSidebarMinimized(true); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 260 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 261 | this.#sideBar = new CSSOverviewSidebarPanel(); |
Jacky Hu | 6e56463 | 2022-04-14 09:30:00 | [diff] [blame] | 262 | this.#sideBar.setMinimumSize(100, 25); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 263 | this.splitWidget().setSidebarWidget(this.#sideBar); |
| 264 | this.splitWidget().setMainWidget(this.#mainContainer); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 265 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 266 | this.#linkifier = new Components.Linkifier.Linkifier(/* maxLinkLength */ 20, /* useLinkDecorator */ true); |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 267 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 268 | this.#viewMap = new Map(); |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 269 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 270 | this.#sideBar.addItem(i18nString(UIStrings.overviewSummary), 'summary'); |
| 271 | this.#sideBar.addItem(i18nString(UIStrings.colors), 'colors'); |
| 272 | this.#sideBar.addItem(i18nString(UIStrings.fontInfo), 'font-info'); |
| 273 | this.#sideBar.addItem(i18nString(UIStrings.unusedDeclarations), 'unused-declarations'); |
| 274 | this.#sideBar.addItem(i18nString(UIStrings.mediaQueries), 'media-queries'); |
| 275 | this.#sideBar.select('summary'); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 276 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 277 | this.#sideBar.addEventListener(SidebarEvents.ItemSelected, this.#sideBarItemSelected, this); |
| 278 | this.#sideBar.addEventListener(SidebarEvents.Reset, this.#sideBarReset, this); |
| 279 | this.#controller.addEventListener(CSSOverViewControllerEvents.Reset, this.#reset, this); |
| 280 | this.#controller.addEventListener(CSSOverViewControllerEvents.PopulateNodes, this.#createElementsView, this); |
| 281 | this.#resultsContainer.element.addEventListener('click', this.#onClick.bind(this)); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 282 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 283 | this.#data = null; |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 284 | } |
| 285 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 286 | wasShown(): void { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 287 | super.wasShown(); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 288 | this.#mainContainer.registerCSSFiles([cssOverviewCompletedViewStyles]); |
Kriti Sapra | f09e2e6 | 2021-08-02 09:19:33 | [diff] [blame] | 289 | this.registerCSSFiles([cssOverviewCompletedViewStyles]); |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 290 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 291 | // TODO(paullewis): update the links in the panels in case source has been . |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 292 | } |
| 293 | |
Tim van der Lippe | f8c26e2 | 2021-11-03 11:46:58 | [diff] [blame] | 294 | initializeModels(target: SDK.Target.Target): void { |
| 295 | const cssModel = target.model(SDK.CSSModel.CSSModel); |
| 296 | const domModel = target.model(SDK.DOMModel.DOMModel); |
| 297 | if (!cssModel || !domModel) { |
| 298 | throw new Error('Target must provide CSS and DOM models'); |
| 299 | } |
| 300 | this.#cssModel = cssModel; |
| 301 | this.#domModel = domModel; |
| 302 | } |
| 303 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 304 | #sideBarItemSelected(event: Common.EventTarget.EventTargetEvent<string>): void { |
Kateryna Prokopenko | d79121e | 2021-09-15 11:08:38 | [diff] [blame] | 305 | const {data} = event; |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 306 | const section = (this.#fragment as UI.Fragment.Fragment).$(data); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 307 | if (!section) { |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 308 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 309 | } |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 310 | |
| 311 | section.scrollIntoView(); |
| 312 | } |
| 313 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 314 | #sideBarReset(): void { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 315 | this.#controller.dispatchEventToListeners(CSSOverViewControllerEvents.Reset); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 316 | } |
| 317 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 318 | #reset(): void { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 319 | this.#resultsContainer.element.removeChildren(); |
| 320 | this.#mainContainer.setSidebarMinimized(true); |
| 321 | this.#elementContainer.closeTabs(); |
| 322 | this.#viewMap = new Map(); |
Alex Rudenko | af4d990 | 2020-11-25 09:18:07 | [diff] [blame] | 323 | CSSOverviewCompletedView.pushedNodes.clear(); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 324 | this.#sideBar.select('summary'); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 325 | } |
| 326 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 327 | #onClick(evt: Event): void { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 328 | if (!evt.target) { |
| 329 | return; |
| 330 | } |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 331 | const target = (evt.target as HTMLElement); |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 332 | const dataset = target.dataset; |
| 333 | |
| 334 | const type = dataset.type; |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 335 | if (!type || !this.#data) { |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 336 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 337 | } |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 338 | |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 339 | let payload: PopulateNodesEvent; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 340 | switch (type) { |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 341 | case 'contrast': { |
| 342 | const section = dataset.section; |
| 343 | const key = dataset.key; |
| 344 | |
| 345 | if (!key) { |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // Remap the Set to an object that is the same shape as the unused declarations. |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 350 | const nodes = this.#data.textColorContrastIssues.get(key) || []; |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 351 | payload = {type, key, nodes, section}; |
| 352 | break; |
| 353 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 354 | case 'color': { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 355 | const color = dataset.color; |
| 356 | const section = dataset.section; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 357 | if (!color) { |
| 358 | return; |
| 359 | } |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 360 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 361 | let nodes; |
| 362 | switch (section) { |
| 363 | case 'text': |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 364 | nodes = this.#data.textColors.get(color); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 365 | break; |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 366 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 367 | case 'background': |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 368 | nodes = this.#data.backgroundColors.get(color); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 369 | break; |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 370 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 371 | case 'fill': |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 372 | nodes = this.#data.fillColors.get(color); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 373 | break; |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 374 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 375 | case 'border': |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 376 | nodes = this.#data.borderColors.get(color); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
| 380 | if (!nodes) { |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | // Remap the Set to an object that is the same shape as the unused declarations. |
| 385 | nodes = Array.from(nodes).map(nodeId => ({nodeId})); |
| 386 | payload = {type, color, nodes, section}; |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | case 'unused-declarations': { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 391 | const declaration = dataset.declaration; |
| 392 | if (!declaration) { |
| 393 | return; |
| 394 | } |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 395 | const nodes = this.#data.unusedDeclarations.get(declaration); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 396 | if (!nodes) { |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | payload = {type, declaration, nodes}; |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | case 'media-queries': { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 405 | const text = dataset.text; |
| 406 | if (!text) { |
| 407 | return; |
| 408 | } |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 409 | const nodes = this.#data.mediaQueries.get(text); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 410 | if (!nodes) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | payload = {type, text, nodes}; |
| 415 | break; |
| 416 | } |
| 417 | |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 418 | case 'font-info': { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 419 | const value = dataset.value; |
| 420 | if (!dataset.path) { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | const [fontFamily, fontMetric] = dataset.path.split('/'); |
| 425 | if (!value) { |
| 426 | return; |
| 427 | } |
| 428 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 429 | const fontFamilyInfo = this.#data.fontInfo.get(fontFamily); |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 430 | if (!fontFamilyInfo) { |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | const fontMetricInfo = fontFamilyInfo.get(fontMetric); |
| 435 | if (!fontMetricInfo) { |
| 436 | return; |
| 437 | } |
| 438 | |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 439 | const nodesIds = fontMetricInfo.get(value); |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 440 | if (!nodesIds) { |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | const nodes = nodesIds.map(nodeId => ({nodeId})); |
| 445 | const name = `${value} (${fontFamily}, ${fontMetric})`; |
| 446 | payload = {type, name, nodes}; |
| 447 | break; |
| 448 | } |
| 449 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 450 | default: |
| 451 | return; |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | evt.consume(); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 455 | this.#controller.dispatchEventToListeners(CSSOverViewControllerEvents.PopulateNodes, {payload}); |
| 456 | this.#mainContainer.setSidebarMinimized(false); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 457 | } |
| 458 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 459 | async #render(data: OverviewData): Promise<void> { |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 460 | if (!data || !('backgroundColors' in data) || !('textColors' in data)) { |
| 461 | return; |
| 462 | } |
| 463 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 464 | this.#data = data; |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 465 | const { |
| 466 | elementCount, |
| 467 | backgroundColors, |
| 468 | textColors, |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 469 | textColorContrastIssues, |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 470 | fillColors, |
| 471 | borderColors, |
| 472 | globalStyleStats, |
| 473 | mediaQueries, |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 474 | unusedDeclarations, |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 475 | fontInfo, |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 476 | } = this.#data; |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 477 | |
| 478 | // Convert rgb values from the computed styles to either undefined or HEX(A) strings. |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 479 | const sortedBackgroundColors = this.#sortColorsByLuminance(backgroundColors); |
| 480 | const sortedTextColors = this.#sortColorsByLuminance(textColors); |
| 481 | const sortedFillColors = this.#sortColorsByLuminance(fillColors); |
| 482 | const sortedBorderColors = this.#sortColorsByLuminance(borderColors); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 483 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 484 | this.#fragment = UI.Fragment.Fragment.build` |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 485 | <div class="vbox overview-completed-view"> |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 486 | <div $="summary" class="results-section horizontally-padded summary"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 487 | <h1>${i18nString(UIStrings.overviewSummary)}</h1> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 488 | |
| 489 | <ul> |
| 490 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 491 | <div class="label">${i18nString(UIStrings.elements)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 492 | <div class="value">${this.#formatter.format(elementCount)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 493 | </li> |
| 494 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 495 | <div class="label">${i18nString(UIStrings.externalStylesheets)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 496 | <div class="value">${this.#formatter.format(globalStyleStats.externalSheets)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 497 | </li> |
| 498 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 499 | <div class="label">${i18nString(UIStrings.inlineStyleElements)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 500 | <div class="value">${this.#formatter.format(globalStyleStats.inlineStyles)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 501 | </li> |
| 502 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 503 | <div class="label">${i18nString(UIStrings.styleRules)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 504 | <div class="value">${this.#formatter.format(globalStyleStats.styleRules)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 505 | </li> |
| 506 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 507 | <div class="label">${i18nString(UIStrings.mediaQueries)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 508 | <div class="value">${this.#formatter.format(mediaQueries.size)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 509 | </li> |
| 510 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 511 | <div class="label">${i18nString(UIStrings.typeSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 512 | <div class="value">${this.#formatter.format(globalStyleStats.stats.type)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 513 | </li> |
| 514 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 515 | <div class="label">${i18nString(UIStrings.idSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 516 | <div class="value">${this.#formatter.format(globalStyleStats.stats.id)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 517 | </li> |
| 518 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 519 | <div class="label">${i18nString(UIStrings.classSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 520 | <div class="value">${this.#formatter.format(globalStyleStats.stats.class)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 521 | </li> |
| 522 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 523 | <div class="label">${i18nString(UIStrings.universalSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 524 | <div class="value">${this.#formatter.format(globalStyleStats.stats.universal)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 525 | </li> |
| 526 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 527 | <div class="label">${i18nString(UIStrings.attributeSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 528 | <div class="value">${this.#formatter.format(globalStyleStats.stats.attribute)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 529 | </li> |
| 530 | <li> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 531 | <div class="label">${i18nString(UIStrings.nonsimpleSelectors)}</div> |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 532 | <div class="value">${this.#formatter.format(globalStyleStats.stats.nonSimple)}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 533 | </li> |
| 534 | </ul> |
| 535 | </div> |
| 536 | |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 537 | <div $="colors" class="results-section horizontally-padded colors"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 538 | <h1>${i18nString(UIStrings.colors)}</h1> |
| 539 | <h2>${i18nString(UIStrings.backgroundColorsS, { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 540 | PH1: sortedBackgroundColors.length, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 541 | })}</h2> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 542 | <ul> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 543 | ${sortedBackgroundColors.map(this.#colorsToFragment.bind(this, 'background'))} |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 544 | </ul> |
| 545 | |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 546 | <h2>${i18nString(UIStrings.textColorsS, { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 547 | PH1: sortedTextColors.length, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 548 | })}</h2> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 549 | <ul> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 550 | ${sortedTextColors.map(this.#colorsToFragment.bind(this, 'text'))} |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 551 | </ul> |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 552 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 553 | ${textColorContrastIssues.size > 0 ? this.#contrastIssuesToFragment(textColorContrastIssues) : ''} |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 554 | |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 555 | <h2>${i18nString(UIStrings.fillColorsS, { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 556 | PH1: sortedFillColors.length, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 557 | })}</h2> |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 558 | <ul> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 559 | ${sortedFillColors.map(this.#colorsToFragment.bind(this, 'fill'))} |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 560 | </ul> |
| 561 | |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 562 | <h2>${i18nString(UIStrings.borderColorsS, { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 563 | PH1: sortedBorderColors.length, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 564 | })}</h2> |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 565 | <ul> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 566 | ${sortedBorderColors.map(this.#colorsToFragment.bind(this, 'border'))} |
Paul Lewis | 12bc63e | 2019-10-23 10:53:29 | [diff] [blame] | 567 | </ul> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 568 | </div> |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 569 | |
Paul Lewis | 66a15fc | 2019-11-07 11:17:11 | [diff] [blame] | 570 | <div $="font-info" class="results-section font-info"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 571 | <h1>${i18nString(UIStrings.fontInfo)}</h1> |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 572 | ${ |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 573 | fontInfo.size > 0 ? this.#fontInfoToFragment(fontInfo) : |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 574 | UI.Fragment.Fragment.build`<div>${i18nString(UIStrings.thereAreNoFonts)}</div>`} |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 575 | </div> |
| 576 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 577 | <div $="unused-declarations" class="results-section unused-declarations"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 578 | <h1>${i18nString(UIStrings.unusedDeclarations)}</h1> |
Paul Lewis | a3dcbf3 | 2019-10-29 14:41:51 | [diff] [blame] | 579 | ${ |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 580 | unusedDeclarations.size > 0 ? this.#groupToFragment(unusedDeclarations, 'unused-declarations', 'declaration') : |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 581 | UI.Fragment.Fragment.build`<div class="horizontally-padded">${ |
| 582 | i18nString(UIStrings.thereAreNoUnusedDeclarations)}</div>`} |
Paul Lewis | 8aef901 | 2019-10-29 14:15:17 | [diff] [blame] | 583 | </div> |
| 584 | |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 585 | <div $="media-queries" class="results-section media-queries"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 586 | <h1>${i18nString(UIStrings.mediaQueries)}</h1> |
Paul Lewis | a3dcbf3 | 2019-10-29 14:41:51 | [diff] [blame] | 587 | ${ |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 588 | mediaQueries.size > 0 ? this.#groupToFragment(mediaQueries, 'media-queries', 'text') : |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 589 | UI.Fragment.Fragment.build`<div class="horizontally-padded">${ |
| 590 | i18nString(UIStrings.thereAreNoMediaQueries)}</div>`} |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 591 | </div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 592 | </div>`; |
| 593 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 594 | this.#resultsContainer.element.appendChild(this.#fragment.element()); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 595 | } |
| 596 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 597 | #createElementsView(evt: Common.EventTarget.EventTargetEvent<{payload: PopulateNodesEvent}>): void { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 598 | const {payload} = evt.data; |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 599 | |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 600 | let id = ''; |
| 601 | let tabTitle = ''; |
| 602 | |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 603 | switch (payload.type) { |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 604 | case 'contrast': { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 605 | const {section, key} = payload; |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 606 | id = `${section}-${key}`; |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 607 | tabTitle = i18nString(UIStrings.contrastIssues); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 611 | case 'color': { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 612 | const {section, color} = payload; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 613 | id = `${section}-${color}`; |
| 614 | tabTitle = `${color.toUpperCase()} (${section})`; |
| 615 | break; |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 616 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 617 | |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 618 | case 'unused-declarations': { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 619 | const {declaration} = payload; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 620 | id = `${declaration}`; |
| 621 | tabTitle = `${declaration}`; |
| 622 | break; |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 623 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 624 | |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 625 | case 'media-queries': { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 626 | const {text} = payload; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 627 | id = `${text}`; |
| 628 | tabTitle = `${text}`; |
| 629 | break; |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 630 | } |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 631 | |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 632 | case 'font-info': { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 633 | const {name} = payload; |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 634 | id = `${name}`; |
| 635 | tabTitle = `${name}`; |
| 636 | break; |
Mathias Bynens | 88e8f15 | 2020-03-25 14:33:12 | [diff] [blame] | 637 | } |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 638 | } |
| 639 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 640 | let view = this.#viewMap.get(id); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 641 | if (!view) { |
Tim van der Lippe | f8c26e2 | 2021-11-03 11:46:58 | [diff] [blame] | 642 | if (!this.#domModel || !this.#cssModel) { |
| 643 | throw new Error('Unable to initialize CSS Overview, missing models'); |
| 644 | } |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 645 | view = new ElementDetailsView(this.#controller, this.#domModel, this.#cssModel, this.#linkifier); |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 646 | void view.populateNodes(payload.nodes); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 647 | this.#viewMap.set(id, view); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 648 | } |
| 649 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 650 | this.#elementContainer.appendTab(id, tabTitle, view, true); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 651 | } |
| 652 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 653 | #fontInfoToFragment(fontInfo: Map<string, Map<string, Map<string, number[]>>>): UI.Fragment.Fragment { |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 654 | const fonts = Array.from(fontInfo.entries()); |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 655 | return UI.Fragment.Fragment.build` |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 656 | ${fonts.map(([font, fontMetrics]) => { |
| 657 | return UI.Fragment.Fragment.build`<section class="font-family"><h2>${font}</h2> ${ |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 658 | this.#fontMetricsToFragment(font, fontMetrics)}</section>`; |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 659 | })} |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 660 | `; |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 661 | } |
| 662 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 663 | #fontMetricsToFragment(font: string, fontMetrics: Map<string, Map<string, number[]>>): UI.Fragment.Fragment { |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 664 | const fontMetricInfo = Array.from(fontMetrics.entries()); |
| 665 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 666 | return UI.Fragment.Fragment.build` |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 667 | <div class="font-metric"> |
| 668 | ${fontMetricInfo.map(([label, values]) => { |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 669 | const sanitizedPath = `${font}/${label}`; |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 670 | return UI.Fragment.Fragment.build` |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 671 | <div> |
| 672 | <h3>${label}</h3> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 673 | ${this.#groupToFragment(values, 'font-info', 'value', sanitizedPath)} |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 674 | </div>`; |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 675 | })} |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 676 | </div>`; |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 677 | } |
| 678 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 679 | #groupToFragment( |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 680 | items: Map<string, (number | UnusedDeclaration | Protocol.CSS.CSSMedia)[]>, type: string, dataLabel: string, |
| 681 | path: string = ''): UI.Fragment.Fragment { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 682 | // Sort by number of items descending. |
| 683 | const values = Array.from(items.entries()).sort((d1, d2) => { |
| 684 | const v1Nodes = d1[1]; |
| 685 | const v2Nodes = d2[1]; |
| 686 | return v2Nodes.length - v1Nodes.length; |
| 687 | }); |
| 688 | |
| 689 | const total = values.reduce((prev, curr) => prev + curr[1].length, 0); |
| 690 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 691 | return UI.Fragment.Fragment.build`<ul> |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 692 | ${values.map(([title, nodes]) => { |
| 693 | const width = 100 * nodes.length / total; |
Peter Marshall | 645f7bf | 2021-03-04 09:56:20 | [diff] [blame] | 694 | const itemLabel = i18nString(UIStrings.nOccurrences, {n: nodes.length}); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 695 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 696 | return UI.Fragment.Fragment.build`<li> |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 697 | <div class="title">${title}</div> |
Paul Lewis | 7d10a73 | 2019-11-06 16:11:51 | [diff] [blame] | 698 | <button data-type="${type}" data-path="${path}" data-${dataLabel}="${title}"> |
Peter Marshall | 40dd7d9 | 2021-02-19 11:07:37 | [diff] [blame] | 699 | <div class="details">${itemLabel}</div> |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 700 | <div class="bar-container"> |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 701 | <div class="bar" style="width: ${width}%;"></div> |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 702 | </div> |
| 703 | </button> |
| 704 | </li>`; |
| 705 | })} |
| 706 | </ul>`; |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 707 | } |
| 708 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 709 | #contrastIssuesToFragment(issues: Map<string, ContrastIssue[]>): UI.Fragment.Fragment { |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 710 | return UI.Fragment.Fragment.build` |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 711 | <h2>${i18nString(UIStrings.contrastIssuesS, { |
| 712 | PH1: issues.size, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 713 | })}</h2> |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 714 | <ul> |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 715 | ${[...issues.entries()].map(([key, value]) => this.#contrastIssueToFragment(key, value))} |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 716 | </ul> |
| 717 | `; |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 718 | } |
| 719 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 720 | #contrastIssueToFragment(key: string, issues: ContrastIssue[]): UI.Fragment.Fragment { |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 721 | console.assert(issues.length > 0); |
| 722 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 723 | let minContrastIssue: ContrastIssue = issues[0]; |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 724 | for (const issue of issues) { |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 725 | // APCA contrast can be a negative value that is to be displayed. But the |
| 726 | // absolute value is used to compare against the threshold. Therefore, the min |
| 727 | // absolute value is the worst contrast. |
| 728 | if (Math.abs(issue.contrastRatio) < Math.abs(minContrastIssue.contrastRatio)) { |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 729 | minContrastIssue = issue; |
| 730 | } |
| 731 | } |
| 732 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 733 | const color = (minContrastIssue.textColor.asString(Common.Color.Format.HEXA) as string); |
| 734 | const backgroundColor = (minContrastIssue.backgroundColor.asString(Common.Color.Format.HEXA) as string); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 735 | |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 736 | const showAPCA = Root.Runtime.experiments.isEnabled('APCA'); |
| 737 | |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 738 | const blockFragment = UI.Fragment.Fragment.build`<li> |
| 739 | <button |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 740 | title="${i18nString(UIStrings.textColorSOverSBackgroundResults, { |
| 741 | PH1: color, |
| 742 | PH2: backgroundColor, |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 743 | PH3: issues.length, |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 744 | })}" |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 745 | data-type="contrast" data-key="${key}" data-section="contrast" class="block" $="color"> |
| 746 | Text |
| 747 | </button> |
| 748 | <div class="block-title"> |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 749 | <div class="contrast-warning hidden" $="aa"><span class="threshold-label">${ |
| 750 | i18nString(UIStrings.aa)}</span></div> |
| 751 | <div class="contrast-warning hidden" $="aaa"><span class="threshold-label">${ |
| 752 | i18nString(UIStrings.aaa)}</span></div> |
| 753 | <div class="contrast-warning hidden" $="apca"><span class="threshold-label">${ |
| 754 | i18nString(UIStrings.apca)}</span></div> |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 755 | </div> |
| 756 | </li>`; |
| 757 | |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 758 | if (showAPCA) { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 759 | const apca = (blockFragment.$('apca') as HTMLElement); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 760 | if (minContrastIssue.thresholdsViolated.apca) { |
| 761 | apca.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 762 | } else { |
| 763 | apca.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 764 | } |
| 765 | apca.classList.remove('hidden'); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 766 | } else { |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 767 | const aa = (blockFragment.$('aa') as HTMLElement); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 768 | if (minContrastIssue.thresholdsViolated.aa) { |
| 769 | aa.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 770 | } else { |
| 771 | aa.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 772 | } |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 773 | const aaa = (blockFragment.$('aaa') as HTMLElement); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 774 | if (minContrastIssue.thresholdsViolated.aaa) { |
| 775 | aaa.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 776 | } else { |
| 777 | aaa.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 778 | } |
| 779 | aa.classList.remove('hidden'); |
| 780 | aaa.classList.remove('hidden'); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 781 | } |
| 782 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 783 | const block = (blockFragment.$('color') as HTMLElement); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 784 | block.style.backgroundColor = backgroundColor; |
| 785 | block.style.color = color; |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 786 | block.style.border = getBorderString(minContrastIssue.backgroundColor); |
Alex Rudenko | d55e18c | 2020-09-23 11:37:06 | [diff] [blame] | 787 | |
| 788 | return blockFragment; |
| 789 | } |
| 790 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 791 | #colorsToFragment(section: string, color: string): UI.Fragment.Fragment|undefined { |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 792 | const blockFragment = UI.Fragment.Fragment.build`<li> |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 793 | <button data-type="color" data-color="${color}" data-section="${section}" class="block" $="color"></button> |
Jacky Hu | 22de401 | 2022-04-13 10:06:38 | [diff] [blame] | 794 | <div class="block-title color-text">${color}</div> |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 795 | </li>`; |
| 796 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 797 | const block = (blockFragment.$('color') as HTMLElement); |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 798 | block.style.backgroundColor = color; |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 799 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 800 | const borderColor = Common.Color.Color.parse(color); |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 801 | if (!borderColor) { |
| 802 | return; |
| 803 | } |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 804 | block.style.border = getBorderString(borderColor); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 805 | |
| 806 | return blockFragment; |
| 807 | } |
| 808 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 809 | #sortColorsByLuminance(srcColors: Map<string, Set<number>>): string[] { |
Paul Lewis | de0224c | 2019-10-22 16:23:15 | [diff] [blame] | 810 | return Array.from(srcColors.keys()).sort((colA, colB) => { |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 811 | const colorA = Common.Color.Color.parse(colA); |
| 812 | const colorB = Common.Color.Color.parse(colB); |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 813 | if (!colorA || !colorB) { |
| 814 | return 0; |
| 815 | } |
Alex Rudenko | 91403e7 | 2020-06-04 07:10:49 | [diff] [blame] | 816 | return Common.ColorUtils.luminance(colorB.rgba()) - Common.ColorUtils.luminance(colorA.rgba()); |
Paul Lewis | 8db3fa8 | 2019-10-10 11:55:53 | [diff] [blame] | 817 | }); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 818 | } |
| 819 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 820 | setOverviewData(data: OverviewData): void { |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 821 | void this.#render(data); |
Paul Lewis | 8cddf993 | 2019-09-27 16:40:07 | [diff] [blame] | 822 | } |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 823 | |
| 824 | // eslint-disable-next-line @typescript-eslint/naming-convention |
Sigurd Schneider | e18ce8e | 2021-07-06 08:57:07 | [diff] [blame] | 825 | static readonly pushedNodes = new Set<Protocol.DOM.BackendNodeId>(); |
Paul Lewis | 4da3b30 | 2019-11-21 14:23:47 | [diff] [blame] | 826 | } |
Kateryna Prokopenko | 4a80253 | 2021-09-16 08:57:06 | [diff] [blame] | 827 | export class DetailsView extends Common.ObjectWrapper.eventMixin<EventTypes, typeof UI.Widget.VBox>(UI.Widget.VBox) { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 828 | #tabbedPane: UI.TabbedPane.TabbedPane; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 829 | constructor() { |
| 830 | super(); |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 831 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 832 | this.#tabbedPane = new UI.TabbedPane.TabbedPane(); |
| 833 | this.#tabbedPane.show(this.element); |
| 834 | this.#tabbedPane.addEventListener(UI.TabbedPane.Events.TabClosed, () => { |
| 835 | this.dispatchEventToListeners(Events.TabClosed, this.#tabbedPane.tabIds().length); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 836 | }); |
| 837 | } |
| 838 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 839 | appendTab(id: string, tabTitle: string, view: UI.Widget.Widget, isCloseable?: boolean): void { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 840 | if (!this.#tabbedPane.hasTab(id)) { |
| 841 | this.#tabbedPane.appendTab(id, tabTitle, view, undefined, undefined, isCloseable); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 842 | } |
| 843 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 844 | this.#tabbedPane.selectTab(id); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 845 | } |
| 846 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 847 | closeTabs(): void { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 848 | this.#tabbedPane.closeTabs(this.#tabbedPane.tabIds()); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 849 | } |
Paul Lewis | 4da3b30 | 2019-11-21 14:23:47 | [diff] [blame] | 850 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 851 | |
Kateryna Prokopenko | 4a80253 | 2021-09-16 08:57:06 | [diff] [blame] | 852 | export const enum Events { |
| 853 | TabClosed = 'TabClosed', |
| 854 | } |
| 855 | |
| 856 | export type EventTypes = { |
| 857 | [Events.TabClosed]: number, |
| 858 | }; |
| 859 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 860 | export class ElementDetailsView extends UI.Widget.Widget { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 861 | readonly #controller: OverviewController; |
| 862 | #domModel: SDK.DOMModel.DOMModel; |
| 863 | readonly #cssModel: SDK.CSSModel.CSSModel; |
| 864 | readonly #linkifier: Components.Linkifier.Linkifier; |
| 865 | readonly #elementGridColumns: DataGrid.DataGrid.ColumnDescriptor[]; |
| 866 | #elementGrid: DataGrid.SortableDataGrid.SortableDataGrid<unknown>; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 867 | |
| 868 | constructor( |
| 869 | controller: OverviewController, domModel: SDK.DOMModel.DOMModel, cssModel: SDK.CSSModel.CSSModel, |
| 870 | linkifier: Components.Linkifier.Linkifier) { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 871 | super(); |
| 872 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 873 | this.#controller = controller; |
| 874 | this.#domModel = domModel; |
| 875 | this.#cssModel = cssModel; |
| 876 | this.#linkifier = linkifier; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 877 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 878 | this.#elementGridColumns = [ |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 879 | { |
| 880 | id: 'nodeId', |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 881 | title: i18nString(UIStrings.element), |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 882 | sortable: true, |
| 883 | weight: 50, |
| 884 | titleDOMFragment: undefined, |
| 885 | sort: undefined, |
| 886 | align: undefined, |
Sigurd Schneider | 2533de1 | 2020-09-24 10:28:44 | [diff] [blame] | 887 | width: undefined, |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 888 | fixedWidth: undefined, |
| 889 | editable: undefined, |
| 890 | nonSelectable: undefined, |
| 891 | longText: undefined, |
| 892 | disclosure: undefined, |
| 893 | allowInSortByEvenWhenHidden: undefined, |
| 894 | dataType: undefined, |
| 895 | defaultWeight: undefined, |
| 896 | }, |
| 897 | { |
| 898 | id: 'declaration', |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 899 | title: i18nString(UIStrings.declaration), |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 900 | sortable: true, |
| 901 | weight: 50, |
| 902 | titleDOMFragment: undefined, |
| 903 | sort: undefined, |
| 904 | align: undefined, |
Sigurd Schneider | 2533de1 | 2020-09-24 10:28:44 | [diff] [blame] | 905 | width: undefined, |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 906 | fixedWidth: undefined, |
| 907 | editable: undefined, |
| 908 | nonSelectable: undefined, |
| 909 | longText: undefined, |
| 910 | disclosure: undefined, |
| 911 | allowInSortByEvenWhenHidden: undefined, |
| 912 | dataType: undefined, |
| 913 | defaultWeight: undefined, |
| 914 | }, |
| 915 | { |
| 916 | id: 'sourceURL', |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 917 | title: i18nString(UIStrings.source), |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 918 | sortable: false, |
| 919 | weight: 100, |
| 920 | titleDOMFragment: undefined, |
| 921 | sort: undefined, |
| 922 | align: undefined, |
Sigurd Schneider | 2533de1 | 2020-09-24 10:28:44 | [diff] [blame] | 923 | width: undefined, |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 924 | fixedWidth: undefined, |
| 925 | editable: undefined, |
| 926 | nonSelectable: undefined, |
| 927 | longText: undefined, |
| 928 | disclosure: undefined, |
| 929 | allowInSortByEvenWhenHidden: undefined, |
| 930 | dataType: undefined, |
| 931 | defaultWeight: undefined, |
| 932 | }, |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 933 | { |
| 934 | id: 'contrastRatio', |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 935 | title: i18nString(UIStrings.contrastRatio), |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 936 | sortable: true, |
| 937 | weight: 25, |
| 938 | titleDOMFragment: undefined, |
| 939 | sort: undefined, |
| 940 | align: undefined, |
Alex Rudenko | af4d990 | 2020-11-25 09:18:07 | [diff] [blame] | 941 | width: '150px', |
| 942 | fixedWidth: true, |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 943 | editable: undefined, |
| 944 | nonSelectable: undefined, |
| 945 | longText: undefined, |
| 946 | disclosure: undefined, |
| 947 | allowInSortByEvenWhenHidden: undefined, |
| 948 | dataType: undefined, |
| 949 | defaultWeight: undefined, |
| 950 | }, |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 951 | ]; |
| 952 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 953 | this.#elementGrid = new DataGrid.SortableDataGrid.SortableDataGrid({ |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 954 | displayName: i18nString(UIStrings.cssOverviewElements), |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 955 | columns: this.#elementGridColumns, |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 956 | editCallback: undefined, |
| 957 | deleteCallback: undefined, |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 958 | refreshCallback: undefined, |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 959 | }); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 960 | this.#elementGrid.element.classList.add('element-grid'); |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 961 | this.#elementGrid.element.addEventListener('mouseover', this.#onMouseOver.bind(this)); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 962 | this.#elementGrid.setStriped(true); |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 963 | this.#elementGrid.addEventListener( |
| 964 | DataGrid.DataGrid.Events.SortingChanged, this.#sortMediaQueryDataGrid.bind(this)); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 965 | |
Tim van der Lippe | 65a4037e | 2021-11-03 11:59:57 | [diff] [blame] | 966 | this.#elementGrid.asWidget().show(this.element); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 967 | } |
| 968 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 969 | #sortMediaQueryDataGrid(): void { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 970 | const sortColumnId = this.#elementGrid.sortColumnId(); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 971 | if (!sortColumnId) { |
| 972 | return; |
| 973 | } |
| 974 | |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 975 | const comparator = DataGrid.SortableDataGrid.SortableDataGrid.StringComparator.bind(null, sortColumnId); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 976 | this.#elementGrid.sortNodes(comparator, !this.#elementGrid.isSortOrderAscending()); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 977 | } |
| 978 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 979 | #onMouseOver(evt: Event): void { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 980 | // Traverse the event path on the grid to find the nearest element with a backend node ID attached. Use |
| 981 | // that for the highlighting. |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 982 | const node = (evt.composedPath() as HTMLElement[]).find(el => el.dataset && el.dataset.backendNodeId); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 983 | if (!node) { |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | const backendNodeId = Number(node.dataset.backendNodeId); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 988 | this.#controller.dispatchEventToListeners(CSSOverViewControllerEvents.RequestNodeHighlight, backendNodeId); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 989 | } |
| 990 | |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 991 | async populateNodes(data: PopulateNodesEventNodes): Promise<void> { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 992 | this.#elementGrid.rootNode().removeChildren(); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 993 | |
| 994 | if (!data.length) { |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | const [firstItem] = data; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 999 | const visibility = new Set<string>(); |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1000 | 'nodeId' in firstItem && firstItem.nodeId && visibility.add('nodeId'); |
| 1001 | 'declaration' in firstItem && firstItem.declaration && visibility.add('declaration'); |
| 1002 | 'sourceURL' in firstItem && firstItem.sourceURL && visibility.add('sourceURL'); |
| 1003 | 'contrastRatio' in firstItem && firstItem.contrastRatio && visibility.add('contrastRatio'); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1004 | |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1005 | let relatedNodesMap: Map<Protocol.DOM.BackendNodeId, SDK.DOMModel.DOMNode|null>|null|undefined; |
| 1006 | if ('nodeId' in firstItem && visibility.has('nodeId')) { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1007 | // Grab the nodes from the frontend, but only those that have not been |
| 1008 | // retrieved already. |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 1009 | const nodeIds = (data as {nodeId: Protocol.DOM.BackendNodeId}[]).reduce((prev, curr) => { |
| 1010 | const nodeId = curr.nodeId; |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1011 | if (CSSOverviewCompletedView.pushedNodes.has(nodeId)) { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1012 | return prev; |
| 1013 | } |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1014 | CSSOverviewCompletedView.pushedNodes.add(nodeId); |
| 1015 | return prev.add(nodeId); |
| 1016 | }, new Set<Protocol.DOM.BackendNodeId>()); |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1017 | relatedNodesMap = await this.#domModel.pushNodesByBackendIdsToFrontend(nodeIds); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | for (const item of data) { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1021 | let frontendNode; |
| 1022 | if ('nodeId' in item && visibility.has('nodeId')) { |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 1023 | if (!relatedNodesMap) { |
| 1024 | continue; |
| 1025 | } |
Philip Pfaffe | 8071e53 | 2021-09-20 08:27:15 | [diff] [blame] | 1026 | frontendNode = relatedNodesMap.get(item.nodeId); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1027 | if (!frontendNode) { |
| 1028 | continue; |
| 1029 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1030 | } |
| 1031 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1032 | const node = new ElementNode(item, frontendNode, this.#linkifier, this.#cssModel); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1033 | node.selectable = false; |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1034 | this.#elementGrid.insertChild(node); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1035 | } |
| 1036 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1037 | this.#elementGrid.setColumnsVisiblity(visibility); |
| 1038 | this.#elementGrid.renderInline(); |
| 1039 | this.#elementGrid.wasShown(); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1040 | } |
Paul Lewis | 4da3b30 | 2019-11-21 14:23:47 | [diff] [blame] | 1041 | } |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1042 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1043 | export class ElementNode extends DataGrid.SortableDataGrid.SortableDataGridNode<ElementNode> { |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1044 | readonly #linkifier: Components.Linkifier.Linkifier; |
| 1045 | readonly #cssModel: SDK.CSSModel.CSSModel; |
| 1046 | readonly #frontendNode: SDK.DOMModel.DOMNode|null|undefined; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1047 | |
| 1048 | constructor( |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1049 | data: PopulateNodesEventNodeTypes, frontendNode: SDK.DOMModel.DOMNode|null|undefined, |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1050 | linkifier: Components.Linkifier.Linkifier, cssModel: SDK.CSSModel.CSSModel) { |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1051 | super(data); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1052 | |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1053 | this.#frontendNode = frontendNode; |
| 1054 | this.#linkifier = linkifier; |
| 1055 | this.#cssModel = cssModel; |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1056 | } |
| 1057 | |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1058 | createCell(columnId: string): HTMLElement { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1059 | // Nodes. |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1060 | const frontendNode = this.#frontendNode; |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1061 | if (columnId === 'nodeId') { |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1062 | const cell = this.createTD(columnId); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1063 | cell.textContent = '...'; |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1064 | |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1065 | if (!frontendNode) { |
| 1066 | throw new Error('Node entry is missing a related frontend node.'); |
| 1067 | } |
| 1068 | |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 1069 | void Common.Linkifier.Linkifier.linkify(frontendNode).then(link => { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1070 | cell.textContent = ''; |
Philip Pfaffe | cc7d938 | 2021-09-17 11:55:10 | [diff] [blame] | 1071 | (link as HTMLElement).dataset.backendNodeId = frontendNode.backendNodeId().toString(); |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1072 | cell.appendChild(link); |
Alex Rudenko | b7419b7 | 2020-09-29 11:10:08 | [diff] [blame] | 1073 | const button = document.createElement('button'); |
| 1074 | button.classList.add('show-element'); |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 1075 | UI.Tooltip.Tooltip.install(button, i18nString(UIStrings.showElement)); |
Alex Rudenko | b7419b7 | 2020-09-29 11:10:08 | [diff] [blame] | 1076 | button.tabIndex = 0; |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1077 | button.onclick = (): void => this.data.node.scrollIntoView(); |
Alex Rudenko | b7419b7 | 2020-09-29 11:10:08 | [diff] [blame] | 1078 | cell.appendChild(button); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1079 | }); |
| 1080 | return cell; |
| 1081 | } |
| 1082 | |
| 1083 | // Links to CSS. |
| 1084 | if (columnId === 'sourceURL') { |
| 1085 | const cell = this.createTD(columnId); |
| 1086 | |
| 1087 | if (this.data.range) { |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 1088 | const link = this.#linkifyRuleLocation( |
Tim van der Lippe | 1e4e83c | 2021-10-12 16:16:58 | [diff] [blame] | 1089 | this.#cssModel, this.#linkifier, this.data.styleSheetId, |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 1090 | TextUtils.TextRange.TextRange.fromObject(this.data.range)); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1091 | |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 1092 | if (!link || link.textContent === '') { |
Mathias Bynens | 23ee1aa | 2020-03-02 12:06:38 | [diff] [blame] | 1093 | cell.textContent = '(unable to link)'; |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 1094 | } else { |
| 1095 | cell.appendChild(link); |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1096 | } |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1097 | } else { |
Paul Lewis | ed808ce | 2019-11-06 14:21:54 | [diff] [blame] | 1098 | cell.textContent = '(unable to link to inlined styles)'; |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1099 | } |
| 1100 | return cell; |
| 1101 | } |
| 1102 | |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 1103 | if (columnId === 'contrastRatio') { |
| 1104 | const cell = this.createTD(columnId); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1105 | const showAPCA = Root.Runtime.experiments.isEnabled('APCA'); |
Alex Rudenko | a9f40de | 2021-01-15 13:28:37 | [diff] [blame] | 1106 | const contrastRatio = Platform.NumberUtilities.floor(this.data.contrastRatio, 2); |
| 1107 | const contrastRatioString = showAPCA ? contrastRatio + '%' : contrastRatio; |
Jan Scheffler | 83e4e26 | 2021-03-17 14:56:51 | [diff] [blame] | 1108 | const border = getBorderString(this.data.backgroundColor); |
| 1109 | const color = this.data.textColor.asString(); |
| 1110 | const backgroundColor = this.data.backgroundColor.asString(); |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 1111 | const contrastFragment = UI.Fragment.Fragment.build` |
| 1112 | <div class="contrast-container-in-grid" $="container"> |
Jan Scheffler | 830467c | 2021-02-25 13:44:47 | [diff] [blame] | 1113 | <span class="contrast-preview" style="border: ${border}; |
Jan Scheffler | 83e4e26 | 2021-03-17 14:56:51 | [diff] [blame] | 1114 | color: ${color}; |
| 1115 | background-color: ${backgroundColor};">Aa</span> |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1116 | <span>${contrastRatioString}</span> |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 1117 | </div> |
| 1118 | `; |
| 1119 | const container = contrastFragment.$('container'); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1120 | if (showAPCA) { |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 1121 | container.append(UI.Fragment.Fragment.build`<span>${i18nString(UIStrings.apca)}</span>`.element()); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1122 | if (this.data.thresholdsViolated.apca) { |
| 1123 | container.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 1124 | } else { |
| 1125 | container.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 1126 | } |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 1127 | } else { |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 1128 | container.append(UI.Fragment.Fragment.build`<span>${i18nString(UIStrings.aa)}</span>`.element()); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1129 | if (this.data.thresholdsViolated.aa) { |
| 1130 | container.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 1131 | } else { |
| 1132 | container.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 1133 | } |
Christy Chen | b6e28b6 | 2021-01-22 08:18:00 | [diff] [blame] | 1134 | container.append(UI.Fragment.Fragment.build`<span>${i18nString(UIStrings.aaa)}</span>`.element()); |
Alex Rudenko | 69c377e | 2020-11-27 07:40:35 | [diff] [blame] | 1135 | if (this.data.thresholdsViolated.aaa) { |
| 1136 | container.appendChild(UI.Icon.Icon.create('smallicon-no')); |
| 1137 | } else { |
| 1138 | container.appendChild(UI.Icon.Icon.create('smallicon-checkmark-square')); |
| 1139 | } |
Alex Rudenko | d3ba0ee | 2020-09-25 14:01:21 | [diff] [blame] | 1140 | } |
| 1141 | cell.appendChild(contrastFragment.element()); |
| 1142 | return cell; |
| 1143 | } |
| 1144 | |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1145 | return super.createCell(columnId); |
| 1146 | } |
| 1147 | |
Tim van der Lippe | 0160157 | 2021-12-15 15:36:29 | [diff] [blame] | 1148 | #linkifyRuleLocation( |
Sigurd Schneider | b5657ef | 2021-08-09 13:06:33 | [diff] [blame] | 1149 | cssModel: SDK.CSSModel.CSSModel, linkifier: Components.Linkifier.Linkifier, |
| 1150 | styleSheetId: Protocol.CSS.StyleSheetId, ruleLocation: TextUtils.TextRange.TextRange): Element|undefined { |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1151 | const styleSheetHeader = cssModel.styleSheetHeaderForId(styleSheetId); |
Andres Olivares | 3dcefe4 | 2020-09-14 16:15:42 | [diff] [blame] | 1152 | if (!styleSheetHeader) { |
| 1153 | return; |
| 1154 | } |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1155 | const lineNumber = styleSheetHeader.lineNumberInSource(ruleLocation.startLine); |
| 1156 | const columnNumber = styleSheetHeader.columnNumberInSource(ruleLocation.startLine, ruleLocation.startColumn); |
Tim van der Lippe | 7946bbc | 2020-02-13 13:58:42 | [diff] [blame] | 1157 | const matchingSelectorLocation = new SDK.CSSModel.CSSLocation(styleSheetHeader, lineNumber, columnNumber); |
Paul Lewis | ebc4719 | 2019-10-09 15:06:41 | [diff] [blame] | 1158 | return linkifier.linkifyCSSLocation(matchingSelectorLocation); |
| 1159 | } |
Paul Lewis | 4da3b30 | 2019-11-21 14:23:47 | [diff] [blame] | 1160 | } |