Auto merge of #7104 - dzbarsky:add_color_stop, r=Ms2ger

CanvasGradient#addColorStop should throw for invalid colors and offsets

The new test failure is because the color stop has a value of 'currentColor' which we don't support yet.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/7104)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-08-11 06:24:35 -06:00
commit d9925f5f92
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);
};