blob: f64c3818f33cc5e884644ed7af52f6aad414cb27 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
Tim van der Lippe97611c92020-02-12 16:56:5828
29import * as Common from '../common/common.js';
Tim van der Lippe97611c92020-02-12 16:56:5830import * as SDK from '../sdk/sdk.js';
31import * as UI from '../ui/ui.js';
32
Tim van der Lippeaabc8302019-12-10 15:34:4533import {ElementsSidebarPane} from './ElementsSidebarPane.js';
Blink Reformat4c46d092018-04-07 15:32:3734
35/**
36 * @unrestricted
37 */
Tim van der Lippeaabc8302019-12-10 15:34:4538export class MetricsSidebarPane extends ElementsSidebarPane {
Blink Reformat4c46d092018-04-07 15:32:3739 constructor() {
40 super();
Jack Franklin71519f82020-11-03 12:08:5941 this.registerRequiredCSS('elements/metricsSidebarPane.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:3742
Changhao Hanb87e7b72020-09-15 20:48:2043 /** @type {?SDK.CSSProperty.CSSProperty} */
44 this.originalPropertyData = null;
45 /** @type {?SDK.CSSProperty.CSSProperty} */
46 this.previousPropertyDataCandidate = null;
Tim van der Lippe97611c92020-02-12 16:56:5847 /** @type {?SDK.CSSStyleDeclaration.CSSStyleDeclaration} */
Blink Reformat4c46d092018-04-07 15:32:3748 this._inlineStyle = null;
Changhao Hanb87e7b72020-09-15 20:48:2049 /** @type {string} */
50 this._highlightMode = '';
51 /** @type {!Array<!{element: !HTMLElement, name: string, backgroundColor: string}>} */
52 this._boxElements = [];
Blink Reformat4c46d092018-04-07 15:32:3753 }
54
55 /**
56 * @override
57 * @protected
58 * @return {!Promise.<?>}
59 */
60 doUpdate() {
61 // "style" attribute might have changed. Update metrics unless they are being edited
62 // (if a CSS property is added, a StyleSheetChanged event is dispatched).
Tim van der Lippe1d6e57a2019-09-30 11:55:3463 if (this._isEditingMetrics) {
Blink Reformat4c46d092018-04-07 15:32:3764 return Promise.resolve();
Tim van der Lippe1d6e57a2019-09-30 11:55:3465 }
Blink Reformat4c46d092018-04-07 15:32:3766
67 // FIXME: avoid updates of a collapsed pane.
68 const node = this.node();
69 const cssModel = this.cssModel();
70 if (!node || node.nodeType() !== Node.ELEMENT_NODE || !cssModel) {
71 this.contentElement.removeChildren();
72 return Promise.resolve();
73 }
74
75 /**
76 * @param {?Map.<string, string>} style
Tim van der Lippe13f71fb2019-11-29 11:17:3977 * @this {MetricsSidebarPane}
Blink Reformat4c46d092018-04-07 15:32:3778 */
79 function callback(style) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3480 if (!style || this.node() !== node) {
Blink Reformat4c46d092018-04-07 15:32:3781 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3482 }
Blink Reformat4c46d092018-04-07 15:32:3783 this._updateMetrics(style);
84 }
Changhao Hanb87e7b72020-09-15 20:48:2085
86 if (!node.id) {
87 return Promise.resolve();
Blink Reformat4c46d092018-04-07 15:32:3788 }
89
90 const promises = [
91 cssModel.computedStylePromise(node.id).then(callback.bind(this)),
Changhao Hanb87e7b72020-09-15 20:48:2092 cssModel.inlineStylesPromise(node.id).then(inlineStyleResult => {
93 if (inlineStyleResult && this.node() === node) {
94 this._inlineStyle = inlineStyleResult.inlineStyle;
95 }
96 }),
Blink Reformat4c46d092018-04-07 15:32:3797 ];
98 return Promise.all(promises);
99 }
100
101 /**
102 * @override
103 */
104 onCSSModelChanged() {
105 this.update();
106 }
107
108 /**
109 * @param {!Map.<string, string>} style
110 * @param {string} propertyName
111 * @return {number}
112 */
113 _getPropertyValueAsPx(style, propertyName) {
Changhao Hanb87e7b72020-09-15 20:48:20114 const propertyValue = style.get(propertyName);
115 if (!propertyValue) {
116 return 0;
117 }
118 return Number(propertyValue.replace(/px$/, '') || 0);
Blink Reformat4c46d092018-04-07 15:32:37119 }
120
121 /**
122 * @param {!Map.<string, string>} computedStyle
123 * @param {string} componentName
124 */
125 _getBox(computedStyle, componentName) {
126 const suffix = componentName === 'border' ? '-width' : '';
127 const left = this._getPropertyValueAsPx(computedStyle, componentName + '-left' + suffix);
128 const top = this._getPropertyValueAsPx(computedStyle, componentName + '-top' + suffix);
129 const right = this._getPropertyValueAsPx(computedStyle, componentName + '-right' + suffix);
130 const bottom = this._getPropertyValueAsPx(computedStyle, componentName + '-bottom' + suffix);
Changhao Hanb87e7b72020-09-15 20:48:20131 return {left, top, right, bottom};
Blink Reformat4c46d092018-04-07 15:32:37132 }
133
134 /**
135 * @param {boolean} showHighlight
136 * @param {string} mode
137 * @param {!Event} event
138 */
139 _highlightDOMNode(showHighlight, mode, event) {
140 event.consume();
Changhao Hanb87e7b72020-09-15 20:48:20141 const node = this.node();
142 if (showHighlight && node) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34143 if (this._highlightMode === mode) {
Blink Reformat4c46d092018-04-07 15:32:37144 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34145 }
Blink Reformat4c46d092018-04-07 15:32:37146 this._highlightMode = mode;
Changhao Hanb87e7b72020-09-15 20:48:20147 node.highlight(mode);
Blink Reformat4c46d092018-04-07 15:32:37148 } else {
Changhao Hanb87e7b72020-09-15 20:48:20149 this._highlightMode = '';
Tim van der Lippe97611c92020-02-12 16:56:58150 SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight();
Blink Reformat4c46d092018-04-07 15:32:37151 }
152
Changhao Hanb87e7b72020-09-15 20:48:20153 for (const {element, name, backgroundColor} of this._boxElements) {
154 if (!node || mode === 'all' || name === mode) {
155 element.style.backgroundColor = backgroundColor;
Tim van der Lippe1d6e57a2019-09-30 11:55:34156 } else {
Blink Reformat4c46d092018-04-07 15:32:37157 element.style.backgroundColor = '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34158 }
Blink Reformat4c46d092018-04-07 15:32:37159 }
160 }
161
162 /**
163 * @param {!Map.<string, string>} style
164 */
165 _updateMetrics(style) {
166 // Updating with computed style.
Changhao Hanb87e7b72020-09-15 20:48:20167 const metricsElement = document.createElement('div');
Blink Reformat4c46d092018-04-07 15:32:37168 metricsElement.className = 'metrics';
169 const self = this;
170
171 /**
172 * @param {!Map.<string, string>} style
173 * @param {string} name
174 * @param {string} side
175 * @param {string} suffix
Tim van der Lippe13f71fb2019-11-29 11:17:39176 * @this {MetricsSidebarPane}
Blink Reformat4c46d092018-04-07 15:32:37177 */
178 function createBoxPartElement(style, name, side, suffix) {
Changhao Hanb87e7b72020-09-15 20:48:20179 const element = document.createElement('div');
180 element.className = side;
181
Blink Reformat4c46d092018-04-07 15:32:37182 const propertyName = (name !== 'position' ? name + '-' : '') + side + suffix;
183 let value = style.get(propertyName);
Changhao Hanb87e7b72020-09-15 20:48:20184 if (value === undefined) {
185 return element;
186 }
187
Tim van der Lippe1d6e57a2019-09-30 11:55:34188 if (value === '' || (name !== 'position' && value === '0px')) {
Blink Reformat4c46d092018-04-07 15:32:37189 value = '\u2012';
Tim van der Lippe1d6e57a2019-09-30 11:55:34190 } else if (name === 'position' && value === 'auto') {
Blink Reformat4c46d092018-04-07 15:32:37191 value = '\u2012';
Tim van der Lippe1d6e57a2019-09-30 11:55:34192 }
Blink Reformat4c46d092018-04-07 15:32:37193 value = value.replace(/px$/, '');
194 value = Number.toFixedIfFloating(value);
195
Blink Reformat4c46d092018-04-07 15:32:37196 element.textContent = value;
197 element.addEventListener('dblclick', this.startEditing.bind(this, element, name, propertyName, style), false);
198 return element;
199 }
200
201 /**
202 * @param {!Map.<string, string>} style
203 * @return {string}
204 */
205 function getContentAreaWidthPx(style) {
Changhao Hanb87e7b72020-09-15 20:48:20206 let width = style.get('width');
207 if (!width) {
208 return '';
209 }
210 width = width.replace(/px$/, '');
211 const widthValue = Number(width);
212 if (!isNaN(widthValue) && style.get('box-sizing') === 'border-box') {
Blink Reformat4c46d092018-04-07 15:32:37213 const borderBox = self._getBox(style, 'border');
214 const paddingBox = self._getBox(style, 'padding');
215
Changhao Hanb87e7b72020-09-15 20:48:20216 width = (widthValue - borderBox.left - borderBox.right - paddingBox.left - paddingBox.right).toString();
Blink Reformat4c46d092018-04-07 15:32:37217 }
218
Changhao Hanb87e7b72020-09-15 20:48:20219 return Number.toFixedIfFloating(width);
Blink Reformat4c46d092018-04-07 15:32:37220 }
221
222 /**
223 * @param {!Map.<string, string>} style
224 * @return {string}
225 */
226 function getContentAreaHeightPx(style) {
Changhao Hanb87e7b72020-09-15 20:48:20227 let height = style.get('height');
228 if (!height) {
229 return '';
230 }
231 height = height.replace(/px$/, '');
232 const heightValue = Number(height);
233 if (!isNaN(heightValue) && style.get('box-sizing') === 'border-box') {
Blink Reformat4c46d092018-04-07 15:32:37234 const borderBox = self._getBox(style, 'border');
235 const paddingBox = self._getBox(style, 'padding');
236
Changhao Hanb87e7b72020-09-15 20:48:20237 height = (heightValue - borderBox.top - borderBox.bottom - paddingBox.top - paddingBox.bottom).toString();
Blink Reformat4c46d092018-04-07 15:32:37238 }
239
Changhao Hanb87e7b72020-09-15 20:48:20240 return Number.toFixedIfFloating(height);
Blink Reformat4c46d092018-04-07 15:32:37241 }
242
243 // Display types for which margin is ignored.
Changhao Hanb87e7b72020-09-15 20:48:20244 const noMarginDisplayType = new Set([
245 'table-cell',
246 'table-column',
247 'table-column-group',
248 'table-footer-group',
249 'table-header-group',
250 'table-row',
251 'table-row-group',
252 ]);
Blink Reformat4c46d092018-04-07 15:32:37253
254 // Display types for which padding is ignored.
Changhao Hanb87e7b72020-09-15 20:48:20255 const noPaddingDisplayType = new Set([
256 'table-column',
257 'table-column-group',
258 'table-footer-group',
259 'table-header-group',
260 'table-row',
261 'table-row-group',
262 ]);
Blink Reformat4c46d092018-04-07 15:32:37263
264 // Position types for which top, left, bottom and right are ignored.
Changhao Hanb87e7b72020-09-15 20:48:20265 const noPositionType = new Set(['static']);
Blink Reformat4c46d092018-04-07 15:32:37266
267 const boxes = ['content', 'padding', 'border', 'margin', 'position'];
268 const boxColors = [
269 Common.Color.PageHighlight.Content, Common.Color.PageHighlight.Padding, Common.Color.PageHighlight.Border,
Tim van der Lippe97611c92020-02-12 16:56:58270 Common.Color.PageHighlight.Margin, Common.Color.Color.fromRGBA([0, 0, 0, 0])
Blink Reformat4c46d092018-04-07 15:32:37271 ];
272 const boxLabels = [
Tim van der Lippe97611c92020-02-12 16:56:58273 Common.UIString.UIString('content'), Common.UIString.UIString('padding'), Common.UIString.UIString('border'),
274 Common.UIString.UIString('margin'), Common.UIString.UIString('position')
Blink Reformat4c46d092018-04-07 15:32:37275 ];
276 let previousBox = null;
277 this._boxElements = [];
278 for (let i = 0; i < boxes.length; ++i) {
279 const name = boxes[i];
Changhao Hanb87e7b72020-09-15 20:48:20280 const display = style.get('display');
281 const position = style.get('position');
282 if (!display || !position) {
Blink Reformat4c46d092018-04-07 15:32:37283 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34284 }
Changhao Hanb87e7b72020-09-15 20:48:20285 if (name === 'margin' && noMarginDisplayType.has(display)) {
Blink Reformat4c46d092018-04-07 15:32:37286 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34287 }
Changhao Hanb87e7b72020-09-15 20:48:20288 if (name === 'padding' && noPaddingDisplayType.has(display)) {
289 continue;
290 }
291 if (name === 'position' && noPositionType.has(position)) {
Blink Reformat4c46d092018-04-07 15:32:37292 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34293 }
Blink Reformat4c46d092018-04-07 15:32:37294
Changhao Hanb87e7b72020-09-15 20:48:20295 const boxElement = /** @type {!HTMLDivElement} */ (document.createElement('div'));
Blink Reformat4c46d092018-04-07 15:32:37296 boxElement.className = name;
Changhao Hanb87e7b72020-09-15 20:48:20297 const backgroundColor = boxColors[i].asString(Common.Color.Format.RGBA) || '';
298 boxElement.style.backgroundColor = backgroundColor;
Blink Reformat4c46d092018-04-07 15:32:37299 boxElement.addEventListener(
300 'mouseover', this._highlightDOMNode.bind(this, true, name === 'position' ? 'all' : name), false);
Changhao Hanb87e7b72020-09-15 20:48:20301 this._boxElements.push({element: boxElement, name, backgroundColor});
Blink Reformat4c46d092018-04-07 15:32:37302
303 if (name === 'content') {
Changhao Hanb87e7b72020-09-15 20:48:20304 const widthElement = document.createElement('span');
Blink Reformat4c46d092018-04-07 15:32:37305 widthElement.textContent = getContentAreaWidthPx(style);
306 widthElement.addEventListener(
307 'dblclick', this.startEditing.bind(this, widthElement, 'width', 'width', style), false);
308
Changhao Hanb87e7b72020-09-15 20:48:20309 const heightElement = document.createElement('span');
Blink Reformat4c46d092018-04-07 15:32:37310 heightElement.textContent = getContentAreaHeightPx(style);
311 heightElement.addEventListener(
312 'dblclick', this.startEditing.bind(this, heightElement, 'height', 'height', style), false);
313
314 boxElement.appendChild(widthElement);
Sigurd Schneider23c52972020-10-13 09:31:14315 UI.UIUtils.createTextChild(boxElement, ' × ');
Blink Reformat4c46d092018-04-07 15:32:37316 boxElement.appendChild(heightElement);
317 } else {
318 const suffix = (name === 'border' ? '-width' : '');
319
Changhao Hanb87e7b72020-09-15 20:48:20320 const labelElement = document.createElement('div');
Blink Reformat4c46d092018-04-07 15:32:37321 labelElement.className = 'label';
322 labelElement.textContent = boxLabels[i];
323 boxElement.appendChild(labelElement);
324
325 boxElement.appendChild(createBoxPartElement.call(this, style, name, 'top', suffix));
Changhao Hanb87e7b72020-09-15 20:48:20326 boxElement.appendChild(document.createElement('br'));
Blink Reformat4c46d092018-04-07 15:32:37327 boxElement.appendChild(createBoxPartElement.call(this, style, name, 'left', suffix));
328
Tim van der Lippe1d6e57a2019-09-30 11:55:34329 if (previousBox) {
Blink Reformat4c46d092018-04-07 15:32:37330 boxElement.appendChild(previousBox);
Tim van der Lippe1d6e57a2019-09-30 11:55:34331 }
Blink Reformat4c46d092018-04-07 15:32:37332
333 boxElement.appendChild(createBoxPartElement.call(this, style, name, 'right', suffix));
Changhao Hanb87e7b72020-09-15 20:48:20334 boxElement.appendChild(document.createElement('br'));
Blink Reformat4c46d092018-04-07 15:32:37335 boxElement.appendChild(createBoxPartElement.call(this, style, name, 'bottom', suffix));
336 }
337
338 previousBox = boxElement;
339 }
340
Changhao Hanb87e7b72020-09-15 20:48:20341 metricsElement.appendChild(/** @type {!HTMLDivElement} */ (previousBox));
Blink Reformat4c46d092018-04-07 15:32:37342 metricsElement.addEventListener('mouseover', this._highlightDOMNode.bind(this, false, 'all'), false);
Changhao Han1d41edd2020-09-14 03:02:22343 metricsElement.addEventListener('mouseleave', this._highlightDOMNode.bind(this, false, 'all'), false);
Blink Reformat4c46d092018-04-07 15:32:37344 this.contentElement.removeChildren();
345 this.contentElement.appendChild(metricsElement);
Blink Reformat4c46d092018-04-07 15:32:37346 }
347
348 /**
349 * @param {!Element} targetElement
350 * @param {string} box
351 * @param {string} styleProperty
352 * @param {!Map.<string, string>} computedStyle
353 */
354 startEditing(targetElement, box, styleProperty, computedStyle) {
Tim van der Lippe97611c92020-02-12 16:56:58355 if (UI.UIUtils.isBeingEdited(targetElement)) {
Blink Reformat4c46d092018-04-07 15:32:37356 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 }
Blink Reformat4c46d092018-04-07 15:32:37358
Changhao Hanb87e7b72020-09-15 20:48:20359 /** @type {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} */
360 const context = {box, styleProperty, computedStyle, keyDownHandler: () => {}};
361 const boundKeyDown = this._handleKeyDown.bind(this, context);
Blink Reformat4c46d092018-04-07 15:32:37362 context.keyDownHandler = boundKeyDown;
363 targetElement.addEventListener('keydown', boundKeyDown, false);
364
365 this._isEditingMetrics = true;
366
367 const config =
368 new UI.InplaceEditor.Config(this._editingCommitted.bind(this), this.editingCancelled.bind(this), context);
Changhao Hanb87e7b72020-09-15 20:48:20369 UI.InplaceEditor.InplaceEditor.startEditing(targetElement, /** @type {!UI.InplaceEditor.Config<?>} */ (config));
Blink Reformat4c46d092018-04-07 15:32:37370
Changhao Hanb87e7b72020-09-15 20:48:20371 const selection = targetElement.getComponentSelection();
372 selection && selection.selectAllChildren(targetElement);
Blink Reformat4c46d092018-04-07 15:32:37373 }
374
Changhao Hanb87e7b72020-09-15 20:48:20375 /**
376 * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context
377 * @param {!Event} event
378 */
379 _handleKeyDown(context, event) {
380 const element = /** @type {!Element} */ (event.currentTarget);
Blink Reformat4c46d092018-04-07 15:32:37381
382 /**
383 * @param {string} originalValue
384 * @param {string} replacementString
Tim van der Lippe13f71fb2019-11-29 11:17:39385 * @this {MetricsSidebarPane}
Blink Reformat4c46d092018-04-07 15:32:37386 */
387 function finishHandler(originalValue, replacementString) {
388 this._applyUserInput(element, replacementString, originalValue, context, false);
389 }
390
391 /**
392 * @param {string} prefix
393 * @param {number} number
394 * @param {string} suffix
395 * @return {string}
396 */
397 function customNumberHandler(prefix, number, suffix) {
Changhao Hanb87e7b72020-09-15 20:48:20398 if (context.styleProperty !== 'margin' && number < 0) {
Blink Reformat4c46d092018-04-07 15:32:37399 number = 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:34400 }
Blink Reformat4c46d092018-04-07 15:32:37401 return prefix + number + suffix;
402 }
403
Tim van der Lippe97611c92020-02-12 16:56:58404 UI.UIUtils.handleElementValueModifications(
405 event, element, finishHandler.bind(this), undefined, customNumberHandler);
Blink Reformat4c46d092018-04-07 15:32:37406 }
407
Changhao Hanb87e7b72020-09-15 20:48:20408 /**
409 * @param {!Element} element
410 * @param {!{keyDownHandler: function(!Event):void}} context
411 */
Blink Reformat4c46d092018-04-07 15:32:37412 editingEnded(element, context) {
Changhao Hanb87e7b72020-09-15 20:48:20413 this.originalPropertyData = null;
414 this.previousPropertyDataCandidate = null;
Blink Reformat4c46d092018-04-07 15:32:37415 element.removeEventListener('keydown', context.keyDownHandler, false);
416 delete this._isEditingMetrics;
417 }
418
Changhao Hanb87e7b72020-09-15 20:48:20419 /**
420 * @param {!Element} element
421 * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context
422 */
Blink Reformat4c46d092018-04-07 15:32:37423 editingCancelled(element, context) {
Changhao Hanb87e7b72020-09-15 20:48:20424 if (this._inlineStyle) {
Blink Reformat4c46d092018-04-07 15:32:37425 if (!this.originalPropertyData) {
426 // An added property, remove the last property in the style.
427 const pastLastSourcePropertyIndex = this._inlineStyle.pastLastSourcePropertyIndex();
Tim van der Lippe1d6e57a2019-09-30 11:55:34428 if (pastLastSourcePropertyIndex) {
Blink Reformat4c46d092018-04-07 15:32:37429 this._inlineStyle.allProperties()[pastLastSourcePropertyIndex - 1].setText('', false);
Tim van der Lippe1d6e57a2019-09-30 11:55:34430 }
Blink Reformat4c46d092018-04-07 15:32:37431 } else {
432 this._inlineStyle.allProperties()[this.originalPropertyData.index].setText(
Changhao Hanb87e7b72020-09-15 20:48:20433 this.originalPropertyData.propertyText || '', false);
Blink Reformat4c46d092018-04-07 15:32:37434 }
435 }
436 this.editingEnded(element, context);
437 this.update();
438 }
439
Changhao Hanb87e7b72020-09-15 20:48:20440 /**
441 * @param {!Element} element
442 * @param {string} userInput
443 * @param {string} previousContent
444 * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context
445 * @param {boolean} commitEditor
446 */
Blink Reformat4c46d092018-04-07 15:32:37447 _applyUserInput(element, userInput, previousContent, context, commitEditor) {
448 if (!this._inlineStyle) {
449 // Element has no renderer.
450 return this.editingCancelled(element, context); // nothing changed, so cancel
451 }
452
Tim van der Lippe1d6e57a2019-09-30 11:55:34453 if (commitEditor && userInput === previousContent) {
454 return this.editingCancelled(element, context);
455 } // nothing changed, so cancel
Blink Reformat4c46d092018-04-07 15:32:37456
Tim van der Lippe1d6e57a2019-09-30 11:55:34457 if (context.box !== 'position' && (!userInput || userInput === '\u2012')) {
Blink Reformat4c46d092018-04-07 15:32:37458 userInput = '0px';
Tim van der Lippe1d6e57a2019-09-30 11:55:34459 } else if (context.box === 'position' && (!userInput || userInput === '\u2012')) {
Blink Reformat4c46d092018-04-07 15:32:37460 userInput = 'auto';
Tim van der Lippe1d6e57a2019-09-30 11:55:34461 }
Blink Reformat4c46d092018-04-07 15:32:37462
463 userInput = userInput.toLowerCase();
464 // Append a "px" unit if the user input was just a number.
Tim van der Lippe1d6e57a2019-09-30 11:55:34465 if (/^\d+$/.test(userInput)) {
Blink Reformat4c46d092018-04-07 15:32:37466 userInput += 'px';
Tim van der Lippe1d6e57a2019-09-30 11:55:34467 }
Blink Reformat4c46d092018-04-07 15:32:37468
469 const styleProperty = context.styleProperty;
470 const computedStyle = context.computedStyle;
471
472 if (computedStyle.get('box-sizing') === 'border-box' && (styleProperty === 'width' || styleProperty === 'height')) {
473 if (!userInput.match(/px$/)) {
Paul Lewisa83ea612020-03-04 13:01:36474 Common.Console.Console.instance().error(
Blink Reformat4c46d092018-04-07 15:32:37475 'For elements with box-sizing: border-box, only absolute content area dimensions can be applied');
476 return;
477 }
478
479 const borderBox = this._getBox(computedStyle, 'border');
480 const paddingBox = this._getBox(computedStyle, 'padding');
481 let userValuePx = Number(userInput.replace(/px$/, ''));
Tim van der Lippe1d6e57a2019-09-30 11:55:34482 if (isNaN(userValuePx)) {
Blink Reformat4c46d092018-04-07 15:32:37483 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34484 }
485 if (styleProperty === 'width') {
Blink Reformat4c46d092018-04-07 15:32:37486 userValuePx += borderBox.left + borderBox.right + paddingBox.left + paddingBox.right;
Tim van der Lippe1d6e57a2019-09-30 11:55:34487 } else {
Blink Reformat4c46d092018-04-07 15:32:37488 userValuePx += borderBox.top + borderBox.bottom + paddingBox.top + paddingBox.bottom;
Tim van der Lippe1d6e57a2019-09-30 11:55:34489 }
Blink Reformat4c46d092018-04-07 15:32:37490
491 userInput = userValuePx + 'px';
492 }
493
494 this.previousPropertyDataCandidate = null;
495
496 const allProperties = this._inlineStyle.allProperties();
497 for (let i = 0; i < allProperties.length; ++i) {
498 const property = allProperties[i];
Tim van der Lippe1d6e57a2019-09-30 11:55:34499 if (property.name !== context.styleProperty || !property.activeInStyle()) {
Blink Reformat4c46d092018-04-07 15:32:37500 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34501 }
Blink Reformat4c46d092018-04-07 15:32:37502
503 this.previousPropertyDataCandidate = property;
504 property.setValue(userInput, commitEditor, true, callback.bind(this));
505 return;
506 }
507
508 this._inlineStyle.appendProperty(context.styleProperty, userInput, callback.bind(this));
509
510 /**
511 * @param {boolean} success
Tim van der Lippe13f71fb2019-11-29 11:17:39512 * @this {MetricsSidebarPane}
Blink Reformat4c46d092018-04-07 15:32:37513 */
514 function callback(success) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34515 if (!success) {
Blink Reformat4c46d092018-04-07 15:32:37516 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34517 }
Changhao Hanb87e7b72020-09-15 20:48:20518 if (!this.originalPropertyData) {
Blink Reformat4c46d092018-04-07 15:32:37519 this.originalPropertyData = this.previousPropertyDataCandidate;
Tim van der Lippe1d6e57a2019-09-30 11:55:34520 }
Blink Reformat4c46d092018-04-07 15:32:37521
Changhao Hanb87e7b72020-09-15 20:48:20522 if (this._highlightMode) {
523 const node = this.node();
524 if (!node) {
525 return;
526 }
527 node.highlight(this._highlightMode);
Tim van der Lippe1d6e57a2019-09-30 11:55:34528 }
Blink Reformat4c46d092018-04-07 15:32:37529
Tim van der Lippe1d6e57a2019-09-30 11:55:34530 if (commitEditor) {
Blink Reformat4c46d092018-04-07 15:32:37531 this.update();
Tim van der Lippe1d6e57a2019-09-30 11:55:34532 }
Blink Reformat4c46d092018-04-07 15:32:37533 }
534 }
535
Changhao Hanb87e7b72020-09-15 20:48:20536 /**
537 * @param {!Element} element
538 * @param {string} userInput
539 * @param {string} previousContent
540 * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context
541 */
Blink Reformat4c46d092018-04-07 15:32:37542 _editingCommitted(element, userInput, previousContent, context) {
543 this.editingEnded(element, context);
544 this._applyUserInput(element, userInput, previousContent, context, true);
545 }
Tim van der Lippe13f71fb2019-11-29 11:17:39546}