Skip to content

Commit

Permalink
Change style parset to more futureproof quirk
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Pulges committed Nov 11, 2013
1 parent a3b8cb3 commit 98427ba
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 77 deletions.
6 changes: 3 additions & 3 deletions src/commands/bgColorStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/foreColorStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/lang/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
};
69 changes: 0 additions & 69 deletions src/quirks/parse_color_style.js

This file was deleted.

73 changes: 73 additions & 0 deletions src/quirks/style_parser.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/toolbar/dialog_bgColorStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<length; i++) {
field = fields[i];
Expand Down
2 changes: 1 addition & 1 deletion src/toolbar/dialog_foreColorStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "color") : null;
color = (colorStr) ? wysihtml5.quirks.styleParser.parseColor(colorStr, "color") : null;

for (; i<length; i++) {
field = fields[i];
Expand Down

0 comments on commit 98427ba

Please sign in to comment.