Open Bug 1465718 Opened 8 years ago Updated 3 years ago

Cannot pass Date object from other documents to fluent function DATETIME

Categories

(Core :: Internationalization, enhancement)

enhancement

Tracking

()

People

(Reporter: jdescottes, Unassigned)

References

(Depends on 1 open bug)

Details

The fluent function DATETIME normally accepts Date objects as parameters. However if the Date object was created in another frame, the following check will fail: case "object": if (arg instanceof Date) { return new FluentDateTime(arg); } https://searchfox.org/mozilla-central/rev/83a923ef7a3b95a516f240a6810c20664b1e0ac9/intl/l10n/MessageContext.jsm#1507-1509 This is the typical use case where instanceof fails when comparing objects from a different context. As an alternative we could check Object.prototype.toString.call(arg) === '[object Date]' which will work across contexts. This issue occurred when using fluent-react in devtools, which loads each devtools panel in its own frame. It could also happen in regular websites if fluent is loaded in another frame than the one where the Date object comes from.
Component: Localization → Internationalization
This looks hacky as hell. Is there really no other way to recognize an object of type date across compartments?
That's a common pattern to detect objects across frames. That was widely used for detecting arrays before we had Array.isArray (which works across frames). I don't think we have such a thing for Date though. Alternatively you can also do duck typing, but since we want to know if the object is a Date and not only if it implements a specific method, I usually prefer to avoid duck typing in such cases. We could also check if new Date(arg) is not an invalid date, since we already check that it is an object.
On #jsapi Waldo suggested: ``` function isDate(value) { try { Reflect.apply(Date.prototype.valueOf, value, []); return true; } catch () { return false; } } ```
Flags: needinfo?(gandalf)
Depends on: 1611752
Flags: needinfo?(gandalf)
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.