From 8408891ec5d2c7d455e52bef4181adbb276413a5 Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Sun, 9 Aug 2015 19:43:09 -0400 Subject: [PATCH] Parse currentColor as Canvas2D color --- components/script/dom/canvasgradient.rs | 16 ++++++--- .../script/dom/canvasrenderingcontext2d.rs | 26 +++++++++++++-- .../2d.fillStyle.parse.current.basic.html.ini | 5 --- ...d.fillStyle.parse.current.changed.html.ini | 5 --- .../2d.gradient.object.current.html.ini | 5 --- tests/wpt/metadata/MANIFEST.json | 4 +++ ...d.fillStyle.parse.current.notrendered.html | 33 +++++++++++++++++++ 7 files changed, 73 insertions(+), 21 deletions(-) delete mode 100644 tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.basic.html.ini delete mode 100644 tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.changed.html.ini delete mode 100644 tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.gradient.object.current.html.ini create mode 100644 tests/wpt/web-platform-tests/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html diff --git a/components/script/dom/canvasgradient.rs b/components/script/dom/canvasgradient.rs index 1721e5e976a1..b2d3f846c892 100644 --- a/components/script/dom/canvasgradient.rs +++ b/components/script/dom/canvasgradient.rs @@ -3,6 +3,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use canvas_traits::{CanvasGradientStop, FillOrStrokeStyle, LinearGradientStyle, RadialGradientStyle}; +use cssparser::Color as CSSColor; +use cssparser::{Parser, RGBA}; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::CanvasGradientBinding; use dom::bindings::codegen::Bindings::CanvasGradientBinding::CanvasGradientMethods; @@ -11,7 +13,6 @@ use dom::bindings::global::GlobalRef; use dom::bindings::js::Root; use dom::bindings::num::Finite; use dom::bindings::reflector::{Reflector, reflect_dom_object}; -use dom::canvasrenderingcontext2d::parse_color; use util::str::DOMString; // https://html.spec.whatwg.org/multipage/#canvasgradient @@ -51,9 +52,16 @@ impl CanvasGradientMethods for CanvasGradient { return Err(Error::IndexSize); } - let color = match parse_color(&color) { - Ok(color) => color, - _ => return Err(Error::Syntax), + let mut parser = Parser::new(&color); + let color = CSSColor::parse(&mut parser); + let color = if parser.is_exhausted() { + match color { + Ok(CSSColor::RGBA(rgba)) => rgba, + Ok(CSSColor::CurrentColor) => RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0 }, + _ => return Err(Error::Syntax) + } + } else { + return Err(Error::Syntax) }; self.stops.borrow_mut().push(CanvasGradientStop { diff --git a/components/script/dom/canvasrenderingcontext2d.rs b/components/script/dom/canvasrenderingcontext2d.rs index 4f307a5c1277..2954fbb332ec 100644 --- a/components/script/dom/canvasrenderingcontext2d.rs +++ b/components/script/dom/canvasrenderingcontext2d.rs @@ -9,10 +9,12 @@ use canvas_traits::{FillOrStrokeStyle, LinearGradientStyle, RadialGradientStyle, use cssparser::Color as CSSColor; use cssparser::{Parser, RGBA}; use dom::bindings::cell::DOMRefCell; +use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods; use dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding; use dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::CanvasRenderingContext2DMethods; use dom::bindings::codegen::Bindings::CanvasRenderingContext2DBinding::CanvasWindingRule; use dom::bindings::codegen::Bindings::ImageDataBinding::ImageDataMethods; +use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::codegen::UnionTypes::HTMLImageElementOrHTMLCanvasElementOrCanvasRenderingContext2D; use dom::bindings::codegen::UnionTypes::StringOrCanvasGradientOrCanvasPattern; use dom::bindings::error::{Error, Fallible}; @@ -439,6 +441,26 @@ impl CanvasRenderingContext2D { Size2D::new(w as f32, h as f32))) } + fn parse_color(&self, string: &str) -> Result { + let mut parser = Parser::new(&string); + let color = CSSColor::parse(&mut parser); + if parser.is_exhausted() { + match color { + Ok(CSSColor::RGBA(rgba)) => Ok(rgba), + Ok(CSSColor::CurrentColor) => { + // TODO: will need to check that the context bitmap mode is fixed + // once we implement CanvasProxy + let window = window_from_node(&*self.canvas); + let style = window.GetComputedStyle(&*self.canvas.upcast(), None); + self.parse_color(&style.GetPropertyValue(DOMString::from("color"))) + }, + _ => Err(()) + } + } else { + Err(()) + } + } + pub fn get_renderer_id(&self) -> usize { self.renderer_id } @@ -844,7 +866,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D { fn SetStrokeStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) { match value { StringOrCanvasGradientOrCanvasPattern::eString(string) => { - match parse_color(&string) { + match self.parse_color(&string) { Ok(rgba) => { self.state.borrow_mut().stroke_style = CanvasFillOrStrokeStyle::Color(rgba); self.ipc_renderer @@ -884,7 +906,7 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D { fn SetFillStyle(&self, value: StringOrCanvasGradientOrCanvasPattern) { match value { StringOrCanvasGradientOrCanvasPattern::eString(string) => { - if let Ok(rgba) = parse_color(&string) { + if let Ok(rgba) = self.parse_color(&string) { self.state.borrow_mut().fill_style = CanvasFillOrStrokeStyle::Color(rgba); self.ipc_renderer .send(CanvasMsg::Canvas2d(Canvas2dMsg::SetFillStyle( diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.basic.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.basic.html.ini deleted file mode 100644 index de8c2aff18e0..000000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.basic.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.fillStyle.parse.current.basic.html] - type: testharness - [currentColor is computed from the canvas element] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.changed.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.changed.html.ini deleted file mode 100644 index f91d8688b8ab..000000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.changed.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.fillStyle.parse.current.changed.html] - type: testharness - [currentColor is computed when the attribute is set, not when it is painted] - expected: FAIL - diff --git a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.gradient.object.current.html.ini b/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.gradient.object.current.html.ini deleted file mode 100644 index c849f911b7fc..000000000000 --- a/tests/wpt/metadata/2dcontext/fill-and-stroke-styles/2d.gradient.object.current.html.ini +++ /dev/null @@ -1,5 +0,0 @@ -[2d.gradient.object.current.html] - type: testharness - [Canvas test: 2d.gradient.object.current] - expected: FAIL - diff --git a/tests/wpt/metadata/MANIFEST.json b/tests/wpt/metadata/MANIFEST.json index 32f8cbe9300b..53d6e38fd136 100644 --- a/tests/wpt/metadata/MANIFEST.json +++ b/tests/wpt/metadata/MANIFEST.json @@ -8366,6 +8366,10 @@ "url": "/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.changed.html" }, { + "path": "2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html", + "url": "/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html" + }, + { "path": "2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.removed.html", "url": "/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.removed.html" }, diff --git a/tests/wpt/web-platform-tests/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html b/tests/wpt/web-platform-tests/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html new file mode 100644 index 000000000000..3e8f007f9bf0 --- /dev/null +++ b/tests/wpt/web-platform-tests/2dcontext/fill-and-stroke-styles/2d.fillStyle.parse.current.notrendered.html @@ -0,0 +1,33 @@ + + +Canvas test: 2d.fillStyle.parse.current.notrendered + + + + + + +

2d.fillStyle.parse.current.basic

+

currentColor is computed from the canvas element

+ + +

Actual output:

+

FAIL (fallback content)

+

Expected output:

+

+ +