Bug 39149 - Canvas element cannot have negative width or height
Summary: Canvas element cannot have negative width or height
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-14 22:53 PDT by Andreas Kling
Modified: 2010-07-01 14:44 PDT (History)
6 users (show)

See Also:


Attachments
Proposed patch (8.21 KB, patch)
2010-05-14 22:55 PDT, Andreas Kling
no flags Details | Formatted Diff | Diff
Proposed patch v2 (8.46 KB, patch)
2010-05-15 22:47 PDT, Andreas Kling
no flags Details | Formatted Diff | Diff
Proposed patch v3 (8.38 KB, patch)
2010-05-16 15:34 PDT, Andreas Kling
kenneth: review-
kling: commit-queue-
Details | Formatted Diff | Diff
Proposed patch v4 (11.47 KB, patch)
2010-06-04 06:08 PDT, Andreas Kling
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Kling 2010-05-14 22:53:13 PDT
In those cases, it should default to 300 for width and 150 for height. (HTML5 spec 4.8.11)
Comment 1 Andreas Kling 2010-05-14 22:55:32 PDT
Created attachment 56140 [details]
Proposed patch
Comment 2 Kenneth Rohde Christiansen 2010-05-15 12:11:52 PDT
(In reply to comment #0)
> In those cases, it should default to 300 for width and 150 for height. (HTML5 spec 4.8.11)

link? It is useful with a link as the spec is work in progress and there are thus multiple versions (Editor Draft, etc)
Comment 3 Andreas Kling 2010-05-15 22:47:11 PDT
Created attachment 56176 [details]
Proposed patch v2

Updated patch with spec link in ChangeLogs:
http://www.whatwg.org/specs/web-apps/current-work/#attr-canvas-width
Comment 4 Eric Seidel (no email) 2010-05-16 00:39:34 PDT
Comment on attachment 56176 [details]
Proposed patch v2

WebCore/html/HTMLCanvasElement.cpp:116
 +          setAttribute(heightAttr, String::number(DefaultHeight));
I would have probably used a ternary here.  Something like:
int height = (value >= 0) ? value : DefaultHeight);

Otherwise the code and test look great.
Comment 5 Andreas Kling 2010-05-16 15:34:29 PDT
Created attachment 56199 [details]
Proposed patch v3

Updated patch, thanks Eric.
Comment 6 Darin Adler 2010-05-16 19:52:57 PDT
Comment on attachment 56199 [details]
Proposed patch v3

I think this patch is not entirely correct or at least not complete.

The code in HTMLCanvasElement::reset seems fine, but the changes to HTMLCanvasSetElement::setHeight and setWidth are either wrong or incomplete.

>  void HTMLCanvasElement::setHeight(int value)
>  {
> -    setAttribute(heightAttr, String::number(value));
> +    int height = (value >= 0) ? value : DefaultHeight;
> +    setAttribute(heightAttr, String::number(height));
>  }

In other similar call sites, I've seen behavior where attempting to set to an illegal value leaves the attribute alone rather than setting it to a default value.

To test this we'd want to set the height attribute of a canvas to something other than the default and see what an attempt to change it to a negative number does in that case.

It also seems strange and probably wrong that this would add a height attribute of "150" to a canvas element that previously had no height attribute of all.

I'd also like to know what happens if we attempt to set the height attribute to a non-numeric string. Both by assigning that value to height and using the setAttribute function. Also, what happens if we set a canvas's height attribute to a negative number using the setAttribute function.

I'd also like to see test cases where we set height to NaN and infinity to see how those cases should be handled. And hear what canvas implementations in other browser engines do in all those cases.
Comment 7 Andreas Kling 2010-05-16 20:19:07 PDT
Comment on attachment 56199 [details]
Proposed patch v3

Removing from commit-queue due to Darin's comments.
Comment 8 Kenneth Rohde Christiansen 2010-05-17 05:45:46 PDT
Comment on attachment 56199 [details]
Proposed patch v3

Good comments, setting r- to removed from pending patches.
Comment 9 Eric Seidel (no email) 2010-06-02 02:26:21 PDT
Comment on attachment 56176 [details]
Proposed patch v2

Cleared Eric Seidel's review+ from obsolete attachment 56176 [details] so that this bug does not appear in http://webkit.org/pending-commit.
Comment 10 Andreas Kling 2010-06-04 06:08:32 PDT
Created attachment 57867 [details]
Proposed patch v4

Updated patch with additional test coverage.
The spec isn't too explicit on how NaN/etc should be handled.
NaN/Infinity/null becomes 0 on Firefox and 300/150 (default value) on Opera.
Opera OTOH doesn't allow 0 width/height.
Personally, while I think mapping NaN/Infinity/null to the default values makes some sense,
it doesn't justify adding custom JS bindings to deal with these cases.
Comment 11 WebKit Commit Bot 2010-07-01 14:44:43 PDT
Comment on attachment 57867 [details]
Proposed patch v4

Clearing flags on attachment: 57867

Committed r62299: <http://trac.webkit.org/changeset/62299>
Comment 12 WebKit Commit Bot 2010-07-01 14:44:49 PDT
All reviewed patches have been landed.  Closing bug.