From 98427baaebe7caab4fca977ec0a3270da3a6d2bd Mon Sep 17 00:00:00 2001 From: Oliver Pulges Date: Mon, 11 Nov 2013 11:48:00 +0200 Subject: [PATCH] Change style parset to more futureproof quirk --- src/commands/bgColorStyle.js | 6 +-- src/commands/foreColorStyle.js | 6 +-- src/lang/array.js | 24 +++++++++ src/quirks/parse_color_style.js | 69 -------------------------- src/quirks/style_parser.js | 73 ++++++++++++++++++++++++++++ src/toolbar/dialog_bgColorStyle.js | 2 +- src/toolbar/dialog_foreColorStyle.js | 2 +- 7 files changed, 105 insertions(+), 77 deletions(-) delete mode 100644 src/quirks/parse_color_style.js create mode 100644 src/quirks/style_parser.js diff --git a/src/commands/bgColorStyle.js b/src/commands/bgColorStyle.js index ca42e8b..ab3fe78 100644 --- a/src/commands/bgColorStyle.js +++ b/src/commands/bgColorStyle.js @@ -8,7 +8,7 @@ wysihtml5.commands.bgColorStyle = { exec: function(composer, command, color) { - var colorVals = wysihtml5.quirks.parseColorStyleStr((typeof(color) == "object") ? "background-color:" + color.color : "background-color:" + color, "background-color"), + var colorVals = wysihtml5.quirks.styleParser.parseColor((typeof(color) == "object") ? "background-color:" + color.color : "background-color:" + color, "background-color"), colString; if (colorVals) { @@ -37,8 +37,8 @@ colorStr = st.getAttribute('style'); if (colorStr) { if (colorStr) { - val = wysihtml5.quirks.parseColorStyleStr(colorStr, "background-color"); - return wysihtml5.quirks.unParseColorStyleStr(val, props); + val = wysihtml5.quirks.styleParser.parseColor(colorStr, "background-color"); + return wysihtml5.quirks.styleParser.unparseColor(val, props); } } } diff --git a/src/commands/foreColorStyle.js b/src/commands/foreColorStyle.js index 9efcba7..80292ca 100644 --- a/src/commands/foreColorStyle.js +++ b/src/commands/foreColorStyle.js @@ -8,7 +8,7 @@ wysihtml5.commands.foreColorStyle = { exec: function(composer, command, color) { - var colorVals = wysihtml5.quirks.parseColorStyleStr((typeof(color) == "object") ? "color:" + color.color : "color:" + color, "color"), + var colorVals = wysihtml5.quirks.styleParser.parseColor((typeof(color) == "object") ? "color:" + color.color : "color:" + color, "color"), colString; if (colorVals) { @@ -36,8 +36,8 @@ colorStr = st.getAttribute('style'); if (colorStr) { if (colorStr) { - val = wysihtml5.quirks.parseColorStyleStr(colorStr, "color"); - return wysihtml5.quirks.unParseColorStyleStr(val, props); + val = wysihtml5.quirks.styleParser.parseColor(colorStr, "color"); + return wysihtml5.quirks.styleParser.unparseColor(val, props); } } } diff --git a/src/lang/array.js b/src/lang/array.js index b0c03e7..ff49e17 100644 --- a/src/lang/array.js +++ b/src/lang/array.js @@ -65,6 +65,30 @@ wysihtml5.lang.array = function(arr) { newArray.push(arr[i]); } return newArray; + }, + + /** + * Creates a new array with the results of calling a provided function on every element in this array. + * optionally this can be provided as second argument + * + * @example + * var childNodes = wysihtml5.lang.array([1,2,3,4]).map(function (value, index, array) { + return value * 2; + * }); + * // => [2,4,6,8] + */ + map: function(callback, thisArg) { + if (Array.prototype.map) { + return arr.map(callback, thisArg); + } else { + var len = arr.length >>> 0, + A = new Array(len), + i = 0; + for (; i < len; i++) { + A[i] = callback.call(thisArg, arr[i], i, arr); + } + return A; + } } }; }; \ No newline at end of file diff --git a/src/quirks/parse_color_style.js b/src/quirks/parse_color_style.js deleted file mode 100644 index e8cf46b..0000000 --- a/src/quirks/parse_color_style.js +++ /dev/null @@ -1,69 +0,0 @@ -(function(wysihtml5) { - var RGBA_REGEX = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([\d\.]+)\s*\)/i, - RGB_REGEX = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/i, - HEX6_REGEX = /^#([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])/i, - HEX3_REGEX = /^#([0-9a-f])([0-9a-f])([0-9a-f])/i; - - wysihtml5.quirks.parseColorStyleStr = function(stylesStr, paramName) { - var str; - var paramRegex = new RegExp("(^|\\s|;)" + paramName + "\\s*:\\s*[^;$]+" , "gi"); - params = stylesStr.match(paramRegex); - if (params) { - for (var i = params.length; i--;) { - params[i] = wysihtml5.lang.string(params[i].split(':')[1]).trim(); - } - str = params[params.length-1]; - - if (RGBA_REGEX.test(str)) { - colorMatch = str.match(RGBA_REGEX); - return [parseInt(colorMatch[1], 10), - parseInt(colorMatch[2], 10), - parseInt(colorMatch[3], 10), - parseInt(colorMatch[4], 10)]; - } else if (RGB_REGEX.test(str)) { - colorMatch = str.match(RGB_REGEX); - return [parseInt(colorMatch[1], 10), - parseInt(colorMatch[2], 10), - parseInt(colorMatch[3], 10), - 1]; - } else if (HEX6_REGEX.test(str)) { - colorMatch = str.match(HEX6_REGEX); - return [parseInt(colorMatch[1], 16), - parseInt(colorMatch[2], 16), - parseInt(colorMatch[3], 16), - 1]; - - } else if (HEX3_REGEX.test(str)) { - colorMatch = str.match(HEX3_REGEX); - return [(parseInt(colorMatch[1], 16) * 16) + parseInt(colorMatch[1], 16), - (parseInt(colorMatch[2], 16) * 16) + parseInt(colorMatch[2], 16), - (parseInt(colorMatch[3], 16) * 16) + parseInt(colorMatch[3], 16), - 1]; - } - } - return false; - }; - - wysihtml5.quirks.unParseColorStyleStr = function(val, props) { - if (props) { - if (props == "hex") { - return (val[0].toString(16).toUpperCase()) + (val[1].toString(16).toUpperCase()) + (val[2].toString(16).toUpperCase()); - } else if (props == "hash") { - return "#" + (val[0].toString(16).toUpperCase()) + (val[1].toString(16).toUpperCase()) + (val[2].toString(16).toUpperCase()); - } else if (props == "rgb") { - return "rgb(" + val[0] + "," + val[1] + "," + val[2] + ")"; - } else if (props == "rgba") { - return "rgba(" + val[0] + "," + val[1] + "," + val[2] + "," + val[3] + ")"; - } else if (props == "csv") { - return val[0] + "," + val[1] + "," + val[2] + "," + val[3]; - } - } - - if (val[3] && val[3] !== 1) { - return "rgba(" + val[0] + "," + val[1] + "," + val[2] + "," + val[3] + ")"; - } else { - return "rgb(" + val[0] + "," + val[1] + "," + val[2] + ")"; - } - }; - -})(wysihtml5); \ No newline at end of file diff --git a/src/quirks/style_parser.js b/src/quirks/style_parser.js new file mode 100644 index 0000000..f97255d --- /dev/null +++ b/src/quirks/style_parser.js @@ -0,0 +1,73 @@ +(function(wysihtml5) { + var RGBA_REGEX = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([\d\.]+)\s*\)/i, + RGB_REGEX = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/i, + HEX6_REGEX = /^#([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])/i, + HEX3_REGEX = /^#([0-9a-f])([0-9a-f])([0-9a-f])/i; + + wysihtml5.quirks.styleParser = { + + parseColor: function(stylesStr, paramName) { + var paramRegex = new RegExp("(^|\\s|;)" + paramName + "\\s*:\\s*[^;$]+" , "gi"), + params = stylesStr.match(paramRegex), + radix = 10, + str, colorMatch; + + if (params) { + for (var i = params.length; i--;) { + params[i] = wysihtml5.lang.string(params[i].split(':')[1]).trim(); + } + str = params[params.length-1]; + + if (RGBA_REGEX.test(str)) { + colorMatch = str.match(RGBA_REGEX); + } else if (RGB_REGEX.test(str)) { + colorMatch = str.match(RGB_REGEX); + } else if (HEX6_REGEX.test(str)) { + colorMatch = str.match(HEX6_REGEX); + radix = 16; + } else if (HEX3_REGEX.test(str)) { + colorMatch = str.match(HEX3_REGEX); + colorMatch.shift(); + colorMatch.push(1); + return wysihtml5.lang.array(colorMatch).map(function(d, idx) { + return (idx < 3) ? (parseInt(d, 16) * 16) + parseInt(d, 16): parseFloat(d); + }); + } + + if (colorMatch) { + colorMatch.shift(); + if (!colorMatch[3]) { + colorMatch.push(1); + } + return wysihtml5.lang.array(colorMatch).map(function(d, idx) { + return (idx < 3) ? parseInt(d, radix): parseFloat(d); + }); + } + } + return false; + }, + + unparseColor: function(val, props) { + if (props) { + if (props == "hex") { + return (val[0].toString(16).toUpperCase()) + (val[1].toString(16).toUpperCase()) + (val[2].toString(16).toUpperCase()); + } else if (props == "hash") { + return "#" + (val[0].toString(16).toUpperCase()) + (val[1].toString(16).toUpperCase()) + (val[2].toString(16).toUpperCase()); + } else if (props == "rgb") { + return "rgb(" + val[0] + "," + val[1] + "," + val[2] + ")"; + } else if (props == "rgba") { + return "rgba(" + val[0] + "," + val[1] + "," + val[2] + "," + val[3] + ")"; + } else if (props == "csv") { + return val[0] + "," + val[1] + "," + val[2] + "," + val[3]; + } + } + + if (val[3] && val[3] !== 1) { + return "rgba(" + val[0] + "," + val[1] + "," + val[2] + "," + val[3] + ")"; + } else { + return "rgb(" + val[0] + "," + val[1] + "," + val[2] + ")"; + } + } + }; + +})(wysihtml5); \ No newline at end of file diff --git a/src/toolbar/dialog_bgColorStyle.js b/src/toolbar/dialog_bgColorStyle.js index 5a7906b..7f65381 100644 --- a/src/toolbar/dialog_bgColorStyle.js +++ b/src/toolbar/dialog_bgColorStyle.js @@ -28,7 +28,7 @@ i = 0, firstElement = (this.elementToChange) ? ((wysihtml5.lang.object(this.elementToChange).isArray()) ? this.elementToChange[0] : this.elementToChange) : null, colorStr = (firstElement) ? firstElement.getAttribute('style') : null, - color = (colorStr) ? wysihtml5.quirks.parseColorStyleStr(colorStr, "background-color") : null; + color = (colorStr) ? wysihtml5.quirks.styleParser.parseColor(colorStr, "background-color") : null; for (; i