Skip to content
Permalink
Browse files

Parse currentColor as Canvas2D color

  • Loading branch information
dzbarsky committed Nov 21, 2015
1 parent ea690a2 commit 8408891ec5d2c7d455e52bef4181adbb276413a5
@@ -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 {
@@ -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<RGBA,()> {
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(

This file was deleted.

This file was deleted.

This file was deleted.

@@ -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"
},
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<!-- DO NOT EDIT! This test has been generated by tools/gentest.py. -->
<title>Canvas test: 2d.fillStyle.parse.current.notrendered</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/canvas-tests.js"></script>
<link rel="stylesheet" href="/common/canvas-tests.css">
<body class="show_output">

<h1>2d.fillStyle.parse.current.basic</h1>
<p class="desc">currentColor is computed from the canvas element</p>


<p class="output">Actual output:</p>
<canvas id="c" class="output" width="100" height="50"><p class="fallback">FAIL (fallback content)</p></canvas>
<p class="output expectedtext">Expected output:<p><img src="/images/green-100x50.png" class="output expected" id="expected" alt="">
<ul id="d"></ul>
<script>
var t = async_test("currentColor is computed from the canvas element even when element is not rendered");
_addTest(function(canvas, ctx) {

canvas.setAttribute('style', 'color: #0f0;');
canvas.style.display = 'none';
canvas.offsetTop;
ctx.fillStyle = 'currentColor';
canvas.style.display = 'inline';
ctx.fillRect(0, 0, 100, 50);
_assertPixel(canvas, 50,25, 0,255,0,255, "50,25", "0,255,0,255");


});
</script>

0 comments on commit 8408891

Please sign in to comment.
You can’t perform that action at this time.