Closed Bug 814234 Opened 12 years ago Closed 11 years ago

Element.toString cannot be overridden

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: mark.yen, Unassigned)

References

(Depends on 1 open bug)

Details

STR:
1. Open scratchpad, or other JS environment.
2. Enter:
  var e = document.createElement("p"); e.toString = function()"1"; alert(e);

Expected results:
Alert box with contents "1"

Actual results:
Alert box with contents "[object HTMLParagraphElement]"

As far as I can tell (but I have not looked very hard), HTML5 / other WHATWG specs don't specify anything about Element.toString.  It's only in EMCA262 Ed5.1 as "Object.prototype.toString()", which means objects should be able to override.

Tested on Gecko 17; kbrosnan tested it for me on trunk (presumably near 2012-11-21).
toString handling with host objects is defined somewhere in
http://dev.w3.org/2006/webapi/WebIDL/
Hmm.

http://www.w3.org/TR/WebIDL/#es-environment specifies Object.prototype.toString; however, since manually invoking e.toString() works, that shouldn't matter.

The other possibility is something that's a stringifier, as specified in section 3.2.4... but that seems to be limited to DOMTokenList and Range, neither of which has much to do with Element.
None of that is relevant to this case, because <p> elements are not using WebIDL bindings yet.

What they're using is an XPConnect binding, and those land in XPC_WN_Shared_Convert, mostly.  Which does special strings for ToString conversions.

Note that this is not the same as stringifiers.  This is more like having a special [[ToPrimitive]].

I wouldn't worry about this, frankly; we plan to move this stuff to WebIDL anyway.
Depends on: ParisBindings
This is fixed by moving <p> to WebIDL bindings.
Status: UNCONFIRMED → RESOLVED
Closed: 11 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.