Skip to content

Commit

Permalink
CanvasGradient#addColorStop should throw for invalid colors and offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky committed Aug 9, 2015
1 parent d4d4d6d commit 33ccb64
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
20 changes: 12 additions & 8 deletions components/script/dom/canvasgradient.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
}

Expand Down
1 change: 1 addition & 0 deletions components/script/dom/webidls/CanvasGradient.webidl
Expand Up @@ -7,6 +7,7 @@
// [Exposed=(Window,Worker)]
interface CanvasGradient {
// opaque object
[Throws]
void addColorStop(double offset, DOMString color);
};

Expand Down
@@ -0,0 +1,5 @@
[2d.gradient.object.current.html]
type: testharness
[Canvas test: 2d.gradient.object.current]
expected: FAIL

This file was deleted.

Expand Up @@ -22,9 +22,9 @@ <h1>2d.gradient.object.invalidoffset</h1>
var g = ctx.createLinearGradient(0, 0, 100, 0);
assert_throws("INDEX_SIZE_ERR", function() { g.addColorStop(-1, '#000'); });
assert_throws("INDEX_SIZE_ERR", function() { g.addColorStop(2, '#000'); });
assert_throws("INDEX_SIZE_ERR", function() { g.addColorStop(Infinity, '#000'); });
assert_throws("INDEX_SIZE_ERR", function() { g.addColorStop(-Infinity, '#000'); });
assert_throws("INDEX_SIZE_ERR", function() { g.addColorStop(NaN, '#000'); });
assert_throws(new TypeError(), function() { g.addColorStop(Infinity, '#000'); });
assert_throws(new TypeError(), function() { g.addColorStop(-Infinity, '#000'); });
assert_throws(new TypeError(), function() { g.addColorStop(NaN, '#000'); });


});
Expand Down

0 comments on commit 33ccb64

Please sign in to comment.