Skip to content

Commit

Permalink
merge develop to master
Browse files Browse the repository at this point in the history
*   Add support for Macedonian
*   Update patterns for Spanish
*   bump devDependencies
*   configurable RequestCredentials
*   hyphenate childNodes in hyphenators
*   don't try to hyphenate whitespace-only text

fixes issue #96
fixes issue #98
  • Loading branch information
mnater committed Mar 27, 2020
1 parent 57988ab commit a1a4457
Show file tree
Hide file tree
Showing 27 changed files with 1,402 additions and 883 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Version History

## Version 4.2.0 (2020-03-27)
### Added
* Added support for Macedonian

### Changed
* RequestCredentials are now configurable. See [doc](https://mnater.github.io/Hyphenopoly/Setup.html#corscredentials) (issue #98)
* Hyphenators now hyphenate content of childNodes, too (#issue96)
* Update patterns for Spanish

### Fixed
* Don't try to hyphenate whitespace-only text nodes
* bump devDependencies

## Version 4.1.0 (2020-02-19)
### Changed
* Hyphenopoly.unhyphenate now returns `elements`: [doc](https://mnater.github.io/Hyphenopoly/Global-Hyphenopoly-Object.html#unhyphenate)
Expand Down
49 changes: 28 additions & 21 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license Hyphenopoly 4.1.0 - client side hyphenation for webbrowsers
* @license Hyphenopoly 4.2.0 - client side hyphenation for webbrowsers
* ©2020 Mathias Nater, Güttingen (mathiasnater at gmail dot com)
* https://github.com/mnater/Hyphenopoly
*
Expand Down Expand Up @@ -69,8 +69,7 @@
div.appendChild(docFrag);
const selectedHTML = div.innerHTML;
const selectedText = sel.toString();
// eslint-disable-next-line security/detect-non-literal-regexp
const re = new RegExp(SOFTHYPHEN, "g");
const re = RegExp(SOFTHYPHEN, "g");
e.clipboardData.setData("text/plain", selectedText.replace(re, ""));
e.clipboardData.setData("text/html", selectedHTML.replace(re, ""));
},
Expand Down Expand Up @@ -345,7 +344,7 @@
* and add them to elements.
* @returns {undefined}
*/
function collectElements() {
function collectElements(parent = null, selector = null) {
const elements = makeElementCollection();

const dontHyphenateSelector = (() => {
Expand Down Expand Up @@ -407,12 +406,16 @@
}
});
}
C.selectors.forEach((sel) => {
w.document.querySelectorAll(sel).forEach((n) => {
processElements(n, getLang(n, true), sel, false);
if (parent === null) {
C.selectors.forEach((sel) => {
w.document.querySelectorAll(sel).forEach((n) => {
processElements(n, getLang(n, true), sel, false);
});
});
});
H.res.set("els", Promise.resolve(elements));
} else {
processElements(parent, getLang(parent, true), selector, true);
}
return elements;
}

const wordHyphenatorPool = new Map();
Expand Down Expand Up @@ -545,9 +548,7 @@
// \u00A0 = no-break space (nbsp)
leadingWhiteSpace = "\u00A0";
}
/* eslint-disable security/detect-non-literal-regexp */
return leadingWhiteSpace + lastWord.replace(new RegExp(selSettings.hyphen, "g"), "") + trailingWhiteSpace;
/* eslint-enable security/detect-non-literal-regexp */
return leadingWhiteSpace + lastWord.replace(RegExp(selSettings.hyphen, "g"), "") + trailingWhiteSpace;
}
orphanControllerPool.set(sel, controlOrphans);
return controlOrphans;
Expand Down Expand Up @@ -613,6 +614,7 @@
el.childNodes.forEach((n) => {
if (
n.nodeType === 3 &&
(/\S/).test(n.data) &&
n.data.length >= minWordLength
) {
n.data = hyphenateText(n.data);
Expand Down Expand Up @@ -640,6 +642,15 @@

H.createHyphenator = ((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);
});
});
return null;
}
return hyphenate(lang, sel, entity);
});
});
Expand All @@ -649,9 +660,7 @@
elements.each((lang, els) => {
els.forEach((elo) => {
const n = elo.element.firstChild;
/* eslint-disable security/detect-non-literal-regexp */
n.data = n.data.replace(new RegExp(C[elo.selector].hyphen, "g"), "");
/* eslint-enable security/detect-non-literal-regexp */
n.data = n.data.replace(RegExp(C[elo.selector].hyphen, "g"), "");
});
});
return elements;
Expand All @@ -676,7 +685,7 @@
}
H.res.get("els").then((elements) => {
if (elements.counter[0] === 0) {
w.clearTimeout(C.timeOutHandler);
w.clearTimeout(H.timeOutHandler);
if (C.hide !== 0) {
H.hide(0, null);
}
Expand Down Expand Up @@ -784,9 +793,7 @@
* that follow a character that is not in the `alphabet`.
* Word delimiters are not taken in account.
*/
/* eslint-disable security/detect-non-literal-regexp */
lo.re.set(sel, new RegExp(`[${alphabet}\u200C-]{${selSettings.minWordLength},}`, "gi"));
/* eslint-enable security/detect-non-literal-regexp */
lo.re.set(sel, RegExp(`[${alphabet}\u200C-]{${selSettings.minWordLength},}`, "gi"));
});
lo.ready = true;
// eslint-disable-next-line security/detect-object-injection
Expand Down Expand Up @@ -898,7 +905,7 @@
});
});
H.hyphenators[lang].reject({
"msg": `1 File ${lang}.wasm can't be loaded from ${H.paths.patterndir}`
"msg": `File ${lang}.wasm can't be loaded from ${H.paths.patterndir}`
});
/* eslint-enable security/detect-object-injection */
}
Expand All @@ -910,7 +917,7 @@
if (!mainLanguage && C.defaultLanguage !== "") {
mainLanguage = C.defaultLanguage;
}
collectElements();
H.res.set("els", Promise.resolve(collectElements()));
H.res.get("els").then((elements) => {
elements.each((lang, values) => {
if (H.languages &&
Expand Down
Loading

0 comments on commit a1a4457

Please sign in to comment.