Move String#isWhitespace from the String prototype to a utiltiy function

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 <szuend@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
This commit is contained in:
Simon Zünd
2020-03-02 09:30:13 +00:00
committed by Commit Bot
parent b867043d77
commit da7058fcd6
7 changed files with 30 additions and 12 deletions
+2 -1
View File
@@ -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 = '';
+2 -1
View File
@@ -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();
@@ -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) {
+3 -1
View File
@@ -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')) {
+8
View File
@@ -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);
};
-7
View File
@@ -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}
@@ -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 '));
});
});
});