https://bugs.webkit.org/show_bug.cgi?id=104835
Source/WebCore:
Patch by John J. Barton <johnjbarton@chromium.org> on 2013-02-11
Reviewed by Vsevolod Vlasov.
WebInspector.Color.parse() returns a Color from a string, or null;
Ctor calls now call parse();
In the StylesSideBarPane, test null rather than catch(e).
Added case to inspector/styles/styles-invalid-color-values.html
* inspector/front-end/Color.js:
(WebInspector.Color):
(WebInspector.Color.parse):
(WebInspector.Color.fromRGBA):
(WebInspector.Color.fromRGB):
(WebInspector.Color.prototype.toString):
(WebInspector.Color.prototype._parse.this.alpha.set 0):
(WebInspector.Color.prototype._parse.this.nickname.set 2):
(WebInspector.Color.prototype._parse.this.hsla.set 1):
(WebInspector.Color.prototype._parse.this.rgba.set 0):
(WebInspector.Color.prototype._parse.set WebInspector):
(WebInspector.Color.prototype._parse):
* inspector/front-end/Spectrum.js:
(WebInspector.Spectrum.prototype.get color):
* inspector/front-end/StylesSidebarPane.js:
LayoutTests:
Patch by John J. Barton <johnjbarton@chromium.org> on 2013-02-11
Reviewed by Vsevolod Vlasov.
Added case to test parsing 'none' from border style
* inspector/styles/styles-invalid-color-values-expected.txt:
* inspector/styles/styles-invalid-color-values.html:
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@142440
268f45cc-cd09-0410-ab3c-
d52691b4dbfc
+2013-02-11 John J. Barton <johnjbarton@chromium.org>
+
+ Web Inspector: Don't throw exceptions in WebInspector.Color
+ https://bugs.webkit.org/show_bug.cgi?id=104835
+
+ Reviewed by Vsevolod Vlasov.
+
+ Added case to test parsing 'none' from border style
+
+ * inspector/styles/styles-invalid-color-values-expected.txt:
+ * inspector/styles/styles-invalid-color-values.html:
+
2013-02-11 Andrey Lushnikov <lushnikov@chromium.org>
Web Inspector: home button behaviour is wrong in DTE
hsl: hsl(0, 100%, 50%)
hsla: hsla(0, 100%, 50%, 1)
+Running: testInvalidColors
+
+SUCCESS: parsed invalid color none to null
+
// Each of these has their alpha clipped [0.0, 1.0].
'rgba(255, 0, 0, -5)', // clipped to rgba(255,0,0,0)
'rgba(255, 0, 0, 5)', // clipped to rgba(255,0,0,1)
+ ];
+
+ var invalidColors = [
+ // An invalid color, eg a value for a shorthand like 'border' which can have a color
+ 'none',
];
InspectorTest.runTestSuite([
dumpColorRepresentationsForColor(colors[i]);
next();
},
+ function testInvalidColors(next)
+ {
+ for (var i = 0; i < invalidColors.length; ++i)
+ dumpErrorsForInvalidColor(invalidColors[i]);
+ next();
+ },
]);
- function dumpColorRepresentationsForColor(colorString)
+ function dumpErrorsForInvalidColor(colorString)
{
- try {
- var color = new WebInspector.Color(colorString);
- } catch (e) {
- InspectorTest.addResult("FAIL: Error parsing color '" + colorString + "'.");
+ var color = WebInspector.Color.parse(colorString);
+ if (!color) {
+ InspectorTest.addResult("");
+ InspectorTest.addResult("SUCCESS: parsed invalid color " + colorString + " to null");
return;
+ } else {
+ InspectorTest.addResult("");
+ InspectorTest.addResult("FAIL: invalid color " + colorString + " did not parse to to null");
}
+ }
+
+ function dumpColorRepresentationsForColor(colorString)
+ {
+ var color = WebInspector.Color.parse(colorString);
+ if (!color)
+ return;
InspectorTest.addResult("");
InspectorTest.addResult("color: " + colorString);
+2013-02-11 John J. Barton <johnjbarton@chromium.org>
+
+ Web Inspector: Don't throw exceptions in WebInspector.Color
+ https://bugs.webkit.org/show_bug.cgi?id=104835
+
+ Reviewed by Vsevolod Vlasov.
+
+ WebInspector.Color.parse() returns a Color from a string, or null;
+ Ctor calls now call parse();
+ In the StylesSideBarPane, test null rather than catch(e).
+
+ Added case to inspector/styles/styles-invalid-color-values.html
+
+ * inspector/front-end/Color.js:
+ (WebInspector.Color):
+ (WebInspector.Color.parse):
+ (WebInspector.Color.fromRGBA):
+ (WebInspector.Color.fromRGB):
+ (WebInspector.Color.prototype.toString):
+ (WebInspector.Color.prototype._parse.this.alpha.set 0):
+ (WebInspector.Color.prototype._parse.this.nickname.set 2):
+ (WebInspector.Color.prototype._parse.this.hsla.set 1):
+ (WebInspector.Color.prototype._parse.this.rgba.set 0):
+ (WebInspector.Color.prototype._parse.set WebInspector):
+ (WebInspector.Color.prototype._parse):
+ * inspector/front-end/Spectrum.js:
+ (WebInspector.Spectrum.prototype.get color):
+ * inspector/front-end/StylesSidebarPane.js:
+
2013-02-11 Andrey Lushnikov <lushnikov@chromium.org>
Web Inspector: home button behaviour is wrong in DTE
*/
/**
- * @constructor
+ * Don't call directly, use WebInspector.Color.parse().
+ * @constructor
*/
WebInspector.Color = function(str)
{
this.value = str;
- this._parse();
}
+WebInspector.Color.parse = function(str)
+{
+ var maybeColor = new WebInspector.Color(str);
+ return maybeColor._parse() ? maybeColor : null;
+}
/**
* @param {number=} a
*/
WebInspector.Color.fromRGBA = function(r, g, b, a)
{
- return new WebInspector.Color("rgba(" + r + "," + g + "," + b + "," + (typeof a === "undefined" ? 1 : a) + ")");
+ return WebInspector.Color.parse("rgba(" + r + "," + g + "," + b + "," + (typeof a === "undefined" ? 1 : a) + ")");
}
WebInspector.Color.fromRGB = function(r, g, b)
{
- return new WebInspector.Color("rgb(" + r + "," + g + "," + b + ")");
+ return WebInspector.Color.parse("rgb(" + r + "," + g + "," + b + ")");
}
WebInspector.Color.prototype = {
return this.nickname;
}
- throw "invalid color format";
+ return "invalid color format: " + format;
},
/**
this.hsla = set[1];
this.nickname = set[2];
this.alpha = set[0][3];
- return;
+ return true;
}
// Simple - #hex, rgb(), nickname, hsl()
this.format = "nickname";
this.hex = WebInspector.Color.Nicknames[nickname];
} else // unknown name
- throw "unknown color name";
+ return false;
} else if (match[4]) { // hsl
this.format = "hsl";
var hsl = match[4].replace(/%/g, "").split(/\s*,\s*/);
this.nickname = set[2];
}
- return;
+ return true;
}
// Advanced - rgba(), hsla()
this.rgba = this._hslaToRGBA(this.hsla, this.alpha);
}
- return;
+ return true;
}
// Could not parse as a valid color
- throw "could not parse color";
+ return false;
}
}
var colorValue = color.toString(this.outputColorFormat);
if (!colorValue)
colorValue = color.toString(); // this.outputColorFormat can be invalid for current color (e.g. "nickname").
- return new WebInspector.Color(colorValue);
+ return WebInspector.Color.parse(colorValue);
},
get outputColorFormat()
function processColor(text)
{
- try {
- var color = new WebInspector.Color(text);
- } catch (e) {
- return document.createTextNode(text);
- }
+ var color = WebInspector.Color.parse(text);
+ // We can be called with valid non-color values of |text| (like 'none' from border style)
+ if (!color)
+ return document.createTextNode(text);
+
var format = getFormat();
var hasSpectrum = self._parentPane;
var spectrumHelper = hasSpectrum ? self._parentPane._spectrumHelper : null;