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.
This commit is contained in:
Neil Fraser
2019-06-10 11:03:22 -07:00
parent 0213de11bc
commit aca1a43ec8
6 changed files with 31 additions and 35 deletions

View File

@@ -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;
};

View File

@@ -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;
}
}
}

View File

@@ -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());

View File

@@ -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);
}
}
}
};

View File

@@ -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();

View File

@@ -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...