diff --git a/Hyphenopoly.js b/Hyphenopoly.js index 7f1d36f3..d15f28a6 100644 --- a/Hyphenopoly.js +++ b/Hyphenopoly.js @@ -228,8 +228,11 @@ H.events.delete(name); H.events.set(name, H.defProm()); H.events.get(name).then((v) => { - // eslint-disable-next-line security/detect-object-injection - H.handleEvent[name](v); + /* eslint-disable security/detect-object-injection */ + if (H.handleEvent[name]) { + H.handleEvent[name](v); + } + /* eslint-enable security/detect-object-injection */ }); } @@ -650,20 +653,35 @@ return r; } - H.createHyphenator = ((lang) => { + /** + * Creates a language-specific string hyphenator + * @param {String} lang - The language this hyphenator hyphenates + */ + function createStringHyphenator(lang) { return ((entity, sel = ".hyphenate") => { - if (entity instanceof HTMLElement) { - const elements = collectElements(entity, sel); - elements.each((l, els) => { - els.forEach((elo) => { - hyphenate(l, elo.selector, elo.element); - }); + if (typeof entity !== "string") { + H.events.get("error").resolve({ + "msg": "This use of hyphenators is deprecated. See https://mnater.github.io/Hyphenopoly/Hyphenators.html" }); - return null; } return hyphenate(lang, sel, entity); }); - }); + } + + /** + * Creates a polyglot HTML hyphenator + */ + function createDOMHyphenator() { + return ((entity, sel = ".hyphenate") => { + const elements = collectElements(entity, sel); + elements.each((l, els) => { + els.forEach((elo) => { + hyphenate(l, elo.selector, elo.element); + }); + }); + return null; + }); + } H.unhyphenate = () => { return H.res.get("els").then((elements) => { @@ -798,7 +816,7 @@ }); lo.ready = true; // eslint-disable-next-line security/detect-object-injection - H.hyphenators[lang].resolve(H.createHyphenator(lang)); + H.hyphenators[lang].resolve(createStringHyphenator(lang)); } if (H.events.has("engineReady")) { H.events.get("engineReady").resolve(lang); @@ -958,5 +976,18 @@ H.res.get("he").forEach((heProm, lang) => { instantiateWasmEngine(heProm, lang); }); + + Promise.all( + // Check all entries except "HTML" + Object.entries(H.hyphenators). + reduce((accumulator, value) => { + if (value[0] !== "HTML") { + return accumulator.concat(value[1]); + } + return accumulator; + }, []) + ).then(() => { + H.hyphenators.HTML.resolve(createDOMHyphenator()); + }); })(Hyphenopoly); })(window); diff --git a/Hyphenopoly_Loader.js b/Hyphenopoly_Loader.js index 86fb320c..5377421f 100644 --- a/Hyphenopoly_Loader.js +++ b/Hyphenopoly_Loader.js @@ -335,6 +335,7 @@ } /* eslint-enable security/detect-object-injection */ }); + H.hyphenators.HTML = H.defProm(); (() => { if (he && he.polyfill) { he.polyfill(); diff --git a/docs/Hyphenators.md b/docs/Hyphenators.md index e9b673c9..fb2d6f8f 100644 --- a/docs/Hyphenators.md +++ b/docs/Hyphenators.md @@ -6,12 +6,16 @@ Possible use cases are: * hyphenating text provided by the user (e.g. in a preview window of a blogging software) * … -For this use cases Hyphenopoly.js exposes `hyphenators` – language specific functions that hyphenates a string or a DOM-Object. +For this use cases Hyphenopoly.js exposes `hyphenators` – functions that hyphenate strings or DOM-Objects. + +There are two types of `hyphenators`: +* language specific `hyphenators` that can only hyphenate `strings` +* a polyglot `HTML`-hyphenator that can hyphenate DOM-objects of type `HTMLElement` ## Create and access `Hyphenopoly.hyphenators` -`hyphenators` are language specific functions that hyphenate their input. Hyphenopoly_Loader.js creates a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises) for a `hyphenator` for each language it loads (i.e. languages the UA doesn't support or languages you "FORCEHYPHENOPOLY"-ied). +Additionally it creates a Promise for a `HTML`-hyphenator, that is able to hyphenate HTMLEntities of all loaded languages. ````html ```` -In the example above we enforced Hyphenopoly_Loader.js to use Hyphenopoly.js for `en-us`. Since the UA seems to support CSS3-hyphens for German, `Hyphenopoly.hyphenators` only contain a Promise for a `en-us`-hyphenator. +In the example above we enforced Hyphenopoly_Loader.js to use Hyphenopoly.js for `en-us`. Since the UA seems to support CSS3-hyphens for German, `Hyphenopoly.hyphenators` only contain a Promise for a `en-us`-hyphenator and a Promise for the `HTML`-hyphenator. -## Use `Hyphenopoly.hyphenators` for Strings +## Use `Hyphenopoly.hyphenators[]` for Strings `hyphenators` are Promises. They are resolved as soon as everything necessary is loaded and ready (or rejected when something goes wrong). `hyphenators` resolve to a language specific function (a `hyphenator`) that hyphenates its input according to the settings for selectors (default: `.hyphenate`): -`function hyphenator({string|DOM-Element}, [Optional: selector=".hyphenate"]) => {string|undefined}` +`function hyphenator({string}, [Optional: selector=".hyphenate"]) => {string|undefined}` ````html -
Supercalifragilisticexpialidocious
+
+ hyphenation + Silbentrennung +
- + ```` +In the example above we assume that the browser supports hyphention for German. So the `HTML`-hyphenator only hyphenates Englisch elements. + ## Further notes and compatibility Instead of using `.then` on the Promises we could also use `async/await`: ````javascript async function runHyphenator(id) { - (await Hyphenopoly.hyphenators["en-us"])(document.getElementById(id)); + (await Hyphenopoly.hyphenators["HTML"])(document.getElementById(id)); } runHyphenator("hyphenateme"); ```` @@ -194,7 +208,7 @@ class Toggle extends React.Component { const el = this.el; //if hyphenation is handled by CSS, Hyphenopoly is undefined if (window.Hyphenopoly) { - window.Hyphenopoly.hyphenators["en-us"].then( + window.Hyphenopoly.hyphenators["HTML"].then( function (enHyphenator) { enHyphenator(el); } diff --git a/testsuite/test42.html b/testsuite/test42.html index 7f6ddbcb..342498fe 100644 --- a/testsuite/test42.html +++ b/testsuite/test42.html @@ -25,17 +25,17 @@ document.getElementById("test2").innerText = (await Hyphenopoly.hyphenators.de)(text); assert(); }*/ - function hyphenate_de(text) { - Hyphenopoly.hyphenators.de.then(function (dehyph) { - dehyph(document.getElementById("test2"), ".usePipe"); - dehyph(document.getElementById("test3"), ".usePipe"); - dehyph(document.getElementById("test4"), ".usePipe"); - dehyph(document.getElementById("test5"), ".usePipe"); - dehyph(document.getElementById("test6"), ".usePipe"); - assertAll(); - }); - } - hyphenate_de("Silbentrennung verbessert den Blocksatz."); + Hyphenopoly.hyphenators.de.then(function (dehyphenator) { + dehyphenator(document.getElementById("test7"), ".usePipe"); + }); + Hyphenopoly.hyphenators.HTML.then(function (hyphenateHTML) { + hyphenateHTML(document.getElementById("test2"), ".usePipe"); + hyphenateHTML(document.getElementById("test3"), ".usePipe"); + hyphenateHTML(document.getElementById("test4"), ".usePipe"); + hyphenateHTML(document.getElementById("test5"), ".usePipe"); + hyphenateHTML(document.getElementById("test6"), ".usePipe"); + assertAll(); + }); } } }; @@ -101,8 +101,8 @@

Test 042

Use hyphenators to hyphenate element tree.

R:

1: Hyphenate automatically

-

Silbentrennungsalgorithmus

-

Sil•ben•tren•nungs•al•go•rith•mus

+

Silbentrennungsalgorithmus hyphenation

+

Sil•ben•tren•nungs•al•go•rith•mus hy•phen•ation

2: Hyphenator with one child (lang tag)

Silbentrennung verbessert den Blocksatz.

Sil|ben|tren|nung ver|bes|sert den Block|satz.

@@ -126,6 +126,9 @@

5: Formatted HTML

6: Rehyphenate with hyphenator

Computertypographie

Com•pu•ter•ty•po•gra•phie

+

7: use H.hyphenators.de on HTMLElement

+

Computertypographie

+

Com•pu•ter•ty•po•gra•phie


Test Ref