CanvasGradient#addColorStop should throw for invalid colors and offsets

This commit is contained in:
David Zbarsky 2015-08-08 22:05:49 -04:00
parent 44d93bc37a
commit 7315b50973
7 changed files with 24 additions and 24 deletions

View file

@ -2,11 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::RGBA;
use canvas_traits::{CanvasGradientStop, FillOrStrokeStyle, LinearGradientStyle, RadialGradientStyle};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::CanvasGradientBinding;
use dom::bindings::codegen::Bindings::CanvasGradientBinding::CanvasGradientMethods;
use dom::bindings::error::Error::{IndexSize, Syntax};
use dom::bindings::error::ErrorResult;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::num::Finite;
@ -44,18 +45,21 @@ impl CanvasGradient {
impl<'a> CanvasGradientMethods for &'a CanvasGradient {
// https://html.spec.whatwg.org/multipage/#dom-canvasgradient-addcolorstop
fn AddColorStop(self, offset: Finite<f64>, color: String) {
let default_black = RGBA {
red: 0.0,
green: 0.0,
blue: 0.0,
alpha: 1.0,
fn AddColorStop(self, offset: Finite<f64>, color: String) -> ErrorResult {
if *offset < 0f64 || *offset > 1f64 {
return Err(IndexSize);
}
let color = match parse_color(&color) {
Ok(color) => color,
_ => return Err(Syntax)
};
self.stops.borrow_mut().push(CanvasGradientStop {
offset: (*offset) as f64,
color: parse_color(&color).unwrap_or(default_black),
color: color,
});
Ok(())
}
}

View file

@ -7,6 +7,7 @@
// [Exposed=(Window,Worker)]
interface CanvasGradient {
// opaque object
[Throws]
void addColorStop(double offset, DOMString color);
};