From da7058fcd6d1af73024efd207c85bdacf691aadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Z=C3=BCnd?= Date: Fri, 28 Feb 2020 14:57:28 +0100 Subject: [PATCH] Move String#isWhitespace from the String prototype to a utiltiy function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit R=jacktfranklin@chromium.org Bug: chromium:1050549 Change-Id: Ief2e7c72d6faecd3849e66cfc7300891e22055b3 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2080372 Commit-Queue: Simon Zünd Reviewed-by: Jack Franklin --- front_end/elements/ClassesPaneWidget.js | 3 ++- front_end/elements/ElementsTreeElement.js | 3 ++- front_end/elements/StylePropertyTreeElement.js | 6 ++++-- front_end/formatter_worker/HTMLFormatter.js | 4 +++- front_end/platform/string-utilities.js | 8 ++++++++ front_end/platform/utilities.js | 7 ------- test/unittests/front_end/platform/string-utilities.ts | 11 +++++++++++ 7 files changed, 30 insertions(+), 12 deletions(-) diff --git a/front_end/elements/ClassesPaneWidget.js b/front_end/elements/ClassesPaneWidget.js index b08063cfc0..a99e3e3c73 100644 --- a/front_end/elements/ClassesPaneWidget.js +++ b/front_end/elements/ClassesPaneWidget.js @@ -3,6 +3,7 @@ // found in the LICENSE file. import * as Common from '../common/common.js'; +import * as Platform from '../platform/platform.js'; import * as SDK from '../sdk/sdk.js'; import * as UI from '../ui/ui.js'; @@ -69,7 +70,7 @@ export class ClassesPaneWidget extends UI.Widget.Widget { let text = event.target.textContent; if (isEscKey(event)) { - if (!text.isWhitespace()) { + if (!Platform.StringUtilities.isWhitespace(text)) { event.consume(true); } text = ''; diff --git a/front_end/elements/ElementsTreeElement.js b/front_end/elements/ElementsTreeElement.js index 6901871e92..5935ff4f4c 100644 --- a/front_end/elements/ElementsTreeElement.js +++ b/front_end/elements/ElementsTreeElement.js @@ -31,6 +31,7 @@ import * as Common from '../common/common.js'; import * as Components from '../components/components.js'; import * as Host from '../host/host.js'; +import * as Platform from '../platform/platform.js'; import * as ProtocolModule from '../protocol/protocol.js'; // eslint-disable-line no-unused-vars import * as SDK from '../sdk/sdk.js'; import * as TextUtils from '../text_utils/text_utils.js'; @@ -1023,7 +1024,7 @@ export class ElementsTreeElement extends UI.TreeOutline.TreeElement { } } } else if (moveDirection === 'forward') { - if (!newText.isWhitespace()) { + if (!Platform.StringUtilities.isWhitespace(newText)) { this._addNewAttribute(); } else { this._startEditingTagName(); diff --git a/front_end/elements/StylePropertyTreeElement.js b/front_end/elements/StylePropertyTreeElement.js index 3eb812e0be..4884b3f5e3 100644 --- a/front_end/elements/StylePropertyTreeElement.js +++ b/front_end/elements/StylePropertyTreeElement.js @@ -5,6 +5,7 @@ import * as ColorPicker from '../color_picker/color_picker.js'; import * as Common from '../common/common.js'; import * as InlineEditor from '../inline_editor/inline_editor.js'; +import * as Platform from '../platform/platform.js'; import * as SDK from '../sdk/sdk.js'; import * as TextUtils from '../text_utils/text_utils.js'; import * as UI from '../ui/ui.js'; @@ -973,7 +974,7 @@ export class StylePropertyTreeElement extends UI.TreeOutline.TreeElement { // Make the Changes and trigger the moveToNextCallback after updating. let moveToIndex = moveTo && this.treeOutline ? this.treeOutline.rootElement().indexOfChild(moveTo) : -1; - const blankInput = userInput.isWhitespace(); + const blankInput = Platform.StringUtilities.isWhitespace(userInput); const shouldCommitNewProperty = this._newProperty && (isPropertySplitPaste || moveToOther || (!moveDirection && !isEditingName) || (isEditingName && blankInput) || nameValueEntered); @@ -982,7 +983,8 @@ export class StylePropertyTreeElement extends UI.TreeOutline.TreeElement { let propertyText; if (nameValueEntered) { propertyText = this.nameElement.textContent; - } else if (blankInput || (this._newProperty && this.valueElement.textContent.isWhitespace())) { + } else if ( + blankInput || (this._newProperty && Platform.StringUtilities.isWhitespace(this.valueElement.textContent))) { propertyText = ''; } else { if (isEditingName) { diff --git a/front_end/formatter_worker/HTMLFormatter.js b/front_end/formatter_worker/HTMLFormatter.js index 2cbf04f87c..0486d999d6 100644 --- a/front_end/formatter_worker/HTMLFormatter.js +++ b/front_end/formatter_worker/HTMLFormatter.js @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import * as Platform from '../platform/platform.js'; + import {CSSFormatter} from './CSSFormatter.js'; import {FormattedContentBuilder} from './FormattedContentBuilder.js'; // eslint-disable-line no-unused-vars import {AbortTokenization, createTokenizer} from './FormatterWorker.js'; @@ -106,7 +108,7 @@ export class HTMLFormatter { * @param {!Token} token */ _formatToken(element, token) { - if (token.value.isWhitespace()) { + if (Platform.StringUtilities.isWhitespace(token.value)) { return; } if (token.type.has('comment') || token.type.has('meta')) { diff --git a/front_end/platform/string-utilities.js b/front_end/platform/string-utilities.js index a9140b3284..0a7f0a5a21 100644 --- a/front_end/platform/string-utilities.js +++ b/front_end/platform/string-utilities.js @@ -304,3 +304,11 @@ export const findLineEndingIndexes = inputString => { endings.push(inputString.length); return endings; }; + +/** + * @param {string} inputString + * @return {boolean} + */ +export const isWhitespace = inputString => { + return /^\s*$/.test(inputString); +}; diff --git a/front_end/platform/utilities.js b/front_end/platform/utilities.js index 0cc6a3769c..c085f8dcce 100644 --- a/front_end/platform/utilities.js +++ b/front_end/platform/utilities.js @@ -64,13 +64,6 @@ String.prototype.replaceControlCharacters = function() { return this.replace(/[\0-\x08\x0B\f\x0E-\x1F\x80-\x9F]/g, '\uFFFD'); }; -/** - * @return {boolean} - */ -String.prototype.isWhitespace = function() { - return /^\s*$/.test(this); -}; - /** * @param {string} chars * @return {string} diff --git a/test/unittests/front_end/platform/string-utilities.ts b/test/unittests/front_end/platform/string-utilities.ts index c3a911083e..7080b32ae7 100644 --- a/test/unittests/front_end/platform/string-utilities.ts +++ b/test/unittests/front_end/platform/string-utilities.ts @@ -60,4 +60,15 @@ describe('StringUtilities', () => { assert.deepEqual(indexes, [4, 7, 10, 12]); }); }); + + describe('isWhitespace', () => { + it('correctly recognizes different kinds of whitespace', () => { + assert.isTrue(StringUtilities.isWhitespace('')); + assert.isTrue(StringUtilities.isWhitespace(' ')); + assert.isTrue(StringUtilities.isWhitespace('\t')); + assert.isTrue(StringUtilities.isWhitespace('\n')); + + assert.isFalse(StringUtilities.isWhitespace(' foo ')); + }); + }); });