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)
Core
Internationalization
Tracking
()
NEW
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.
| Reporter | ||
Updated•8 years ago
|
Component: Localization → Internationalization
Comment 1•8 years ago
|
||
This looks hacky as hell. Is there really no other way to recognize an object of type date across compartments?
Comment 2•8 years ago
|
||
I see what you did there: https://searchfox.org/mozilla-central/source/devtools/shared/gcli/source/lib/gcli/types/date.js#48 ... :)
there's also that - https://searchfox.org/mozilla-central/source/js/src/jsdate.cpp#1508 ?
| Reporter | ||
Comment 3•8 years ago
|
||
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.
Comment 4•8 years ago
|
||
On #jsapi Waldo suggested:
```
function isDate(value) {
try {
Reflect.apply(Date.prototype.valueOf, value, []);
return true;
} catch () {
return false;
}
}
```
Updated•8 years ago
|
Flags: needinfo?(gandalf)
Updated•3 years ago
|
Severity: normal → S3
You need to log in
before you can comment on or make changes to this bug.
Description
•