From aca1a43ec8cb1faae3c71e0603745cb0063b6054 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Mon, 10 Jun 2019 11:03:22 -0700 Subject: [PATCH] Fix regular expressions. 1) Simplifications. 2) Enable toolbox category colours to be specified using the full range of CSS formats (not just hue or #rrggbb). 3) Fix bug where `Blockly.utils.checkMessageReferences('%{BKY_today} %{BKY_xxx}')` returns true. --- core/field_colour.js | 5 ++--- core/field_date.js | 9 +++++---- core/procedures.js | 2 +- core/toolbox.js | 26 ++++++++++++++++---------- core/utils.js | 22 ++++++---------------- core/variables.js | 2 +- 6 files changed, 31 insertions(+), 35 deletions(-) diff --git a/core/field_colour.js b/core/field_colour.js index d7cd85a33..fb0641878 100644 --- a/core/field_colour.js +++ b/core/field_colour.js @@ -194,9 +194,8 @@ Blockly.FieldColour.prototype.doValueUpdate_ = function(newValue) { Blockly.FieldColour.prototype.getText = function() { var colour = this.value_; // Try to use #rgb format if possible, rather than #rrggbb. - var m = colour.match(/^#(.)\1(.)\2(.)\3$/); - if (m) { - colour = '#' + m[1] + m[2] + m[3]; + if (/^#(.)\1(.)\2(.)\3$/.test(colour)) { + colour = '#' + colour[1] + colour[3] + colour[5]; } return colour; }; diff --git a/core/field_date.js b/core/field_date.js index 5d80a665c..0878f78b1 100644 --- a/core/field_date.js +++ b/core/field_date.js @@ -29,6 +29,7 @@ goog.provide('Blockly.FieldDate'); goog.require('Blockly.Events'); goog.require('Blockly.Field'); goog.require('Blockly.utils.dom'); +goog.require('Blockly.utils.string'); goog.require('goog.date'); goog.require('goog.date.DateTime'); @@ -255,13 +256,13 @@ Blockly.FieldDate.widgetDispose_ = function() { * @private */ Blockly.FieldDate.loadLanguage_ = function() { - var reg = /^DateTimeSymbols_(.+)$/; for (var prop in goog.i18n) { - var m = prop.match(reg); - if (m) { - var lang = m[1].toLowerCase().replace('_', '.'); // E.g. 'pt.br' + if (Blockly.utils.string.startsWith(prop, 'DateTimeSymbols_')) { + var lang = prop.substr(16).toLowerCase().replace('_', '.'); + // E.g. 'DateTimeSymbols_pt_BR' -> 'pt.br' if (goog.getObjectByName(lang, Blockly.Msg)) { goog.i18n.DateTimeSymbols = goog.i18n[prop]; + break; } } } diff --git a/core/procedures.js b/core/procedures.js index 6923a7d3b..4b96c40f4 100644 --- a/core/procedures.js +++ b/core/procedures.js @@ -160,7 +160,7 @@ Blockly.Procedures.isNameUsed = function(name, workspace, opt_exclude) { */ Blockly.Procedures.rename = function(name) { // Strip leading and trailing whitespace. Beyond this, all names are legal. - name = name.replace(/^[\s\xa0]+|[\s\xa0]+$/g, ''); + name = name.trim(); // Ensure two identically-named procedures don't exist. var legalName = Blockly.Procedures.findLegalName(name, this.getSourceBlock()); diff --git a/core/toolbox.js b/core/toolbox.js index edb37d3bc..d0619276d 100644 --- a/core/toolbox.js +++ b/core/toolbox.js @@ -32,6 +32,7 @@ goog.require('Blockly.Flyout'); goog.require('Blockly.HorizontalFlyout'); goog.require('Blockly.Touch'); goog.require('Blockly.utils'); +goog.require('Blockly.utils.colour'); goog.require('Blockly.utils.dom'); goog.require('Blockly.utils.Rect'); goog.require('Blockly.VerticalFlyout'); @@ -396,17 +397,22 @@ Blockly.Toolbox.prototype.setColour_ = function(colourValue, childOut, if (colour === null || colour === '') { // No attribute. No colour. childOut.hexColour = ''; - } else if (/^#[0-9a-fA-F]{6}$/.test(colour)) { - childOut.hexColour = colour; - this.hasColours_ = true; - } else if (typeof colour === 'number' || - (typeof colour === 'string' && !isNaN(Number(colour)))) { - childOut.hexColour = Blockly.hueToHex(Number(colour)); - this.hasColours_ = true; } else { - childOut.hexColour = ''; - console.warn('Toolbox category "' + categoryName + - '" has unrecognized colour attribute: ' + colour); + var hue = Number(colour); + if (!isNaN(hue)) { + childOut.hexColour = Blockly.hueToHex(hue); + this.hasColours_ = true; + } else { + var hex = Blockly.utils.colour.parse(colour); + if (hex) { + childOut.hexColour = hex; + this.hasColours_ = true; + } else { + childOut.hexColour = ''; + console.warn('Toolbox category "' + categoryName + + '" has unrecognized colour attribute: ' + colour); + } + } } }; diff --git a/core/utils.js b/core/utils.js index 278121e20..160696d76 100644 --- a/core/utils.js +++ b/core/utils.js @@ -256,23 +256,13 @@ Blockly.utils.checkMessageReferences = function(message) { // TODO (#1169): Implement support for other string tables, // prefixes other than BKY_. - var regex = /%{(BKY_[A-Z][A-Z0-9_]*)}/gi; - var match = regex.exec(message); - while (match) { - var msgKey = match[1]; - msgKey = msgKey.toUpperCase(); - if (msgKey.substr(0, 4) != 'BKY_') { - console.log('WARNING: Unsupported message table prefix in %{' + - match[1] + '}.'); - validSoFar = false; // Continue to report other errors. - } else if (msgTable[msgKey.substr(4)] == undefined) { - console.log('WARNING: No message string for %{' + match[1] + '}.'); + var m = message.match(/%{BKY_[A-Z]\w*}/ig); + for (var i = 0; i < m.length; i++) { + var msgKey = m[i].toUpperCase(); + if (msgTable[msgKey.slice(6, -1)] == undefined) { + console.log('WARNING: No message string for ' + m[i] + ' in ' + message); validSoFar = false; // Continue to report other errors. } - - // Re-run on remainder of string. - message = message.substring(match.index + msgKey.length + 1); - match = regex.exec(message); } return validSoFar; @@ -350,7 +340,7 @@ Blockly.utils.tokenizeInterpolation_ = function(message, buffer.push(c); } else { var rawKey = buffer.join(''); - if (/[a-zA-Z][a-zA-Z0-9_]*/.test(rawKey)) { // Strict matching + if (/[A-Z]\w*/i.test(rawKey)) { // Strict matching // Found a valid string key. Attempt case insensitive match. var keyUpper = rawKey.toUpperCase(); diff --git a/core/variables.js b/core/variables.js index a37271702..f002e2ca7 100644 --- a/core/variables.js +++ b/core/variables.js @@ -399,7 +399,7 @@ Blockly.Variables.promptName = function(promptText, defaultText, callback) { // Merge runs of whitespace. Strip leading and trailing whitespace. // Beyond this, all names are legal. if (newVar) { - newVar = newVar.replace(/[\s\xa0]+/g, ' ').replace(/^ | $/g, ''); + newVar = newVar.replace(/[\s\xa0]+/g, ' ').trim(); if (newVar == Blockly.Msg['RENAME_VARIABLE'] || newVar == Blockly.Msg['NEW_VARIABLE']) { // Ok, not ALL names are legal...