forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1347798 - Create a small per-document bindings for DOMLocalizatio…
…n. r=mossop MozReview-Commit-ID: DfxIYVxyt9C
- Loading branch information
Zibi Braniecki
committed
Sep 10, 2017
1 parent
cbc0a71
commit 7e897ed
Showing
6 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
toolkit.jar: | ||
content/global/l10n.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
const { DOMLocalization } = | ||
Components.utils.import("resource://gre/modules/DOMLocalization.jsm"); | ||
|
||
/** | ||
* Polyfill for document.ready polyfill. | ||
* See: https://github.com/whatwg/html/issues/127 for details. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function documentReady() { | ||
const rs = document.readyState; | ||
if (rs === 'interactive' || rs === 'completed') { | ||
return Promise.resolve(); | ||
} | ||
|
||
return new Promise( | ||
resolve => document.addEventListener( | ||
'readystatechange', resolve, { once: true } | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Scans the `elem` for links with localization resources. | ||
* | ||
* @param {Element} elem | ||
* @returns {Array<string>} | ||
*/ | ||
function getResourceLinks(elem) { | ||
return Array.from(elem.querySelectorAll('link[rel="localization"]')).map( | ||
el => el.getAttribute('href') | ||
); | ||
} | ||
|
||
const resourceIds = getResourceLinks(document.head || document); | ||
|
||
document.l10n = new DOMLocalization(window, resourceIds); | ||
|
||
// trigger first context to be fetched eagerly | ||
document.l10n.ctxs.touchNext(); | ||
|
||
document.l10n.ready = documentReady().then(() => { | ||
document.l10n.registerObservers(); | ||
window.addEventListener('unload', () => { | ||
document.l10n.unregisterObservers(); | ||
}); | ||
document.l10n.connectRoot(document.documentElement); | ||
return document.l10n.translateRoots(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters