[whatwg] Microdata - Handling the case where a string is upgraded to an object
Philip Jägenstedt
philipj at opera.com
Mon Jul 18 04:20:11 PDT 2011
On Thu, 14 Jul 2011 20:49:44 +0200, Tab Atkins Jr. <jackalmage at gmail.com>
wrote:
> Some IRC discussion this morning concerned the scenario where an API
> starts by exposing a property as a string, but later wants to change
> it to be a complex object.
>
> This appears to be a reasonably common scenario. For example, a
> vocabulary with a "name" property may start with it being a string,
> and then later change to an object exposing "firstname"/"lastname"/etc
> properties. A vocabulary for a music library may start by having
> "track" as a string, then later expanding it to expose the track
> title, the individual artist, the running time, etc.
>
> In a very similar vein, the CSSOM is currently defined to always
> return property values as strings. We want to instead return complex
> objects that expose useful information and interfaces specialized on
> the value's type, however. For compat reasons, we have to use an
> entirely different accessor in order to expose this type of thing.
>
> It seems that this may be a useful problem to solve in Microdata. We
> can expose either an attribute or a privileged property name for the
> object's "name"/"title"/"string representation". Then, when using the
> .items accessor, objects can be returned with a custom .toString that
> returns that value, so they can be used as strings in legacy code.
>
> Thoughts?
There is no items IDL attribute, do you mean getItems() or .itemValue
perhaps?
I take it the problem is with code like this:
<div itemscope itemtype="person"><span itemprop="name">Foo
Barsson</span></div>
<script>
var p = document.getItems("person")[0];
alert(p.properties.namedItem("name")[0].itemValue);
</script>
If the HTML changes to
<div itemscope itemtype="person"><span itemprop="name" itemscope><span
itemprop="givenName">Foo</span> <span
itemprop="familyName">Barsson</span></span></div>
then the script would be alerting "[object HTMLElement]" instead of "Foo
Barsson".
I'm not sure why this would be a problem. If someone changes the page,
then can't they adjust the script to match? Is it extensions and libraries
that you're worried about?
As for the solution, are you suggesting that .itemValue return a special
object which is like HTMLElement in all regards except for how it
toString()s?
--
Philip Jägenstedt
Core Developer
Opera Software
More information about the whatwg
mailing list