Closed Bug 345635 Opened 18 years ago Closed 5 years ago

SVGTextElement.getComputedTextLength() returns 0 if element not rendered

Categories

(Core :: SVG, defect)

x86
Windows 2000
defect
Not set
minor

Tracking

()

RESOLVED DUPLICATE of bug 376027

People

(Reporter: jetifi, Unassigned)

Details

(Keywords: testcase)

Attachments

(1 file, 1 obsolete file)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4

The getComputedTextLength() method of SVGTextElement returns 0 when the text element is either not rendered due to "display:none" or not a part of the active DOM. The spec[0] says "For non-rendering environments, the user agent shall make reasonable assumptions about glyph metrics."

[0] http://www.w3.org/TR/SVG/text.html#InterfaceSVGTextContentElement

Reproducible: Always

Steps to Reproduce:
The attached test case illustrates the problem. The format is slightly clunky but I hope the meaning is clear.
Actual Results:  
Both alerts 2. and 3. show a 0 in place of 49

Expected Results:  
Both alerts 2. and 3. should show 49

The workaround is to append each text element to a "visibility:hidden" SVGSVGElement in the DOM and use that to obtain the text length.

The use case is that when constructing SVG in ECMAScript, knowing the length of a text is important for correctly positioning the elements.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Keywords: testcase
If you are arguing that you need to know the length of the text to correctly position elements then consider the following fragment...

<svg>
    <text id="text1">hello</text>
    <text id="text2">he<tspan style="display:none">dddd</tspan>llo</text>
</svg>

Would you say that getComputedTextLength on text1 should give the same answer as getComputedTextLength on text2?

For what its worth, with ASV3 you get an exception from calling getComputedTextLength with display:none text.

Is there a definition in the spec of what a non-rendering environment actually is?

The current situation in mozilla is that if an element is display:none then it is basically ignored and most SVG DOM functions will either give an exception or return some null or 0 value.


Robert, as per your comment in #2 you're probably right; "display:none" does alter the rendering so probably 0 is the right answer in this case.

This only leaves point 3 in the attached example. This one is more directly applicable in the case of someone who needs to know the text length to correctly position elements; however this element isn't rendered either since it's not yet part of the document.

I'm not sure if this is what's meant by "non-rendering environment" or if this term is meant to refer to (e.g.) a headless server simply crunching DOMs without rendering them to a canvas.
(In reply to comment #3)

> This only leaves point 3 in the attached example. This one is more directly
> applicable in the case of someone who needs to know the text length to
> correctly position elements; however this element isn't rendered either since
> it's not yet part of the document.

A workaround would be to put the text it in the document at some location where it is not visible to the user or temporarily make it visibility:hidden, get the length and then position it correctly.

(In reply to comment #4)

Agreed, that's what I'm doing now actually, with the added step of converting from an E4X node first.

 - I
Assignee: general → nobody
QA Contact: ian → general
Attached file reporters testcase fixed to be valid (deleted) —
Attachment #230344 - Attachment is obsolete: true
To add a different use case, SVGTextElement.getComputedTextLength() returns 0 whenever the SVGTextElement is inside a symbol definition.

See reproduction here: https://jsfiddle.net/mike_marcacci/fcdrka8u/4/

On a separate note (I realize this is a different symptom, but I suspect the same issue) calling SVGTextElement.getBBox() under the same conditions throws an error.
Status: NEW → RESOLVED
Closed: 5 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: