Re: convertToSpecifiedUnits and zero units

Am 27.08.2011 um 03:36 schrieb Dean Jackson:

> 
> On 24/08/2011, at 10:49 PM, Øyvind Stenhaug wrote:
> 
>> Units
>> 
>> This doesn't seem to consider what happens if the unitType argument is a zero length, e.g. SVG_LENGTHTYPE_EMS when 1em=0px.
>> 
>> <svg xmlns="http://www.w3.org/2000/svg" font-size="0">
>>  <rect width="100px"/>
>>  <script type="application/ecmascript">
>>     onload = function() {
>>        try {
>>           var width = document.querySelector('rect').width.baseVal;
>>           width.convertToSpecifiedUnits(width.SVG_LENGTHTYPE_EMS);
>>           alert(width.valueInSpecifiedUnits + ' ' + width.value);
>>        } catch(e) {
>>           alert(e);
>>        }
>>     }
>>  </script>
>> </svg>
>> 
>> Gecko and Presto give "Infinity NaN", failing to "[p]reserve the same underlying stored value".
>> 
>> WebKit throws NOT_SUPPORTED_ERR, although the spec only says it's "[r]aised if unitType is SVG_LENGTHTYPE_UNKNOWN or not a valid unit type constant (one of the other SVG_LENGTHTYPE_* constants defined on this interface)".
>> 
>> (When trying to convert a zero value, Presto gives "0 0", Gecko "NaN NaN" and WebKit still throws.)
> 
> The WebKit behaviour here is a bug which is easily fixed, but what values do you suggest we return?

I don't think it's a bug. This was my intention, if no font-size is available then throw here.
In general WebKit is much more strict about the SVG DOM primitives like SVGAngle, SVGLength, etc, when it comes to corner cases.
I think it's a bad idea to silently fail/ignore these things.

The scope of this problem is much larger. Think of handling percentage viewports on eg. <rect>'s rect.x.baseVal if the <rect> is not living in the document, only created dynamically.
I think failing silently, when someones tries to convert eg. 100px to percent, if the <rect> is not in the document, is a bad idea.

I wrote several tests for the SVG DOM primitives that nail down the WebKit specific throwing behaviour. (IIRC Gecko also throws in lots of cases).
http://trac.webkit.org/export/72123/trunk/LayoutTests/svg/dom/script-tests/SVGLength.js
http://trac.webkit.org/export/71014/trunk/LayoutTests/svg/dom/script-tests/SVGLength-px.js
http://trac.webkit.org/export/71014/trunk/LayoutTests/svg/dom/script-tests/SVGLength-px-with-context.js

Mostly interessting is eg this snippet form SVGLength-px.js: 
debug("Try converting from px to ems, should fail as the SVGLength is not associated with a SVGSVGElement, and thus no font-size information is available");
shouldThrow("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS)");

My opinion is that the SVG DOM primitives should be _much_ more strict and throwy. We've changed to this in WebKit trunk sth. like 10 months ago, and when I tested it back then, I saw FF also throw a lot more than speced, and it's a good thing IMHO.

Cheers,
Niko

Received on Saturday, 27 August 2011 10:00:57 UTC