Skip to content

Commit

Permalink
Release 3.0.0 (#68)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: new hyphenopoly pattern binary format

implement new hyphenation pattern binary (.hpb) format
    *   update patterns to new format and include left-/rightmin according to source
    *   update hyphenEngine.asm/.wasm to consume new pattern format
    *   implement .hpb-version-check in Hyphenopoly.js and hyphenopoly.module.js

fixes issue #61, fixes issue #65

* feat: add tearDown event

Dispatch "tearDown"-event just before Hyphenopoly gets cleared
if native CSS-hyphenation is available.

fixes issue #67

* Update changelog

* BREAKING CHANGE: remove `H.setup.classnames`

Classnames have been deprecated since 2.6.0 – Use selectors instead.

* feat: Add "loadError"event to Recover from 404

Dispatch non-cancellable "loadError"-event,
if a .hpb doesn't exist. This allows to recover
from a 404, without a FOUHC-timeout.

fixes isse #59

* fix incorrect elements.count

* update pattern format description

* switch -> if/else

* Change patternFormat

0 (zero) marks the beginning of a new group with same length,
255 marks a new prefix group.

fixes issue #61

* dispatch loadError in requestBinary

* Update Readme with new sizes

* lint

* lint

* CHANGE how module reacts when browserified

* REMOVE support for cop and la-medi

* CHANGE how loader-mode is set

Add new setting 'loader' (default 'fs')
Add test to keep 100% coverage

* Bump versions

* Bump versions in lock file

* lint
  • Loading branch information
mnater authored Mar 28, 2019
1 parent 683c2a9 commit 1773a5e
Show file tree
Hide file tree
Showing 92 changed files with 701 additions and 368 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Version History

## Version 3.0.0 (2019-03-29)
### Changed
* BREAKING CHANGE: implement new hyphenation pattern binary (.hpb) format (issue #61)
* update patterns to new format and include left-/rightmin according to source
* update hyphenEngine.asm/.wasm to consume new pattern format
* implement .hpb-version-check in Hyphenopoly.js and hyphenopoly.module.js
* fix issue #65

### Added
* feat: tearDown event (issue #67)
* feat: loadError event (issue #59)
* feat: hyphenopoly.module is now browserifyable. Added new config option "loader"

### Removed
* BREAKING CHANGE: remove `Hyphenopoly.setup.classnames` – use [selectors](https://github.com/mnater/Hyphenopoly/wiki/Global-Hyphenopoly-Object#new-with-version-260-selectors) instead

## Version 2.8.0 (Feb 28, 2019)
* the error event now accepts a `lvl` field ("info"/"warn"/"error") and logs accordingly (issue #56)
* add list of supported languages in hyphenopoly.module.js (issue #57)
Expand Down
44 changes: 42 additions & 2 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license Hyphenopoly 2.8.1-devel - client side hyphenation for webbrowsers
* @license Hyphenopoly 3.0.0 - client side hyphenation for webbrowsers
* ©2019 Mathias Nater, Zürich (mathiasnater at gmail dot com)
* https://github.com/mnater/Hyphenopoly
*
Expand Down Expand Up @@ -189,6 +189,22 @@
return elo;
}

/**
* Removes elements from the list and updates the counter
* @param {string} lang - The lang of the elements to remove
*/
function rem(lang) {
let langCount = 0;
if (list.has(lang)) {
langCount = list.get(lang).length;
list.delete(lang);
counter[0] -= langCount;
if (counter[0] === 0) {
H.events.dispatch("hyphenopolyEnd");
}
}
}

/**
* Execute fn for each element
* @param {function} fn The function to execute
Expand All @@ -204,7 +220,8 @@
"add": add,
"counter": counter,
"each": each,
"list": list
"list": list,
"rem": rem
};
}

Expand Down Expand Up @@ -511,6 +528,7 @@
}
if (classSettings.orphanControl !== 1) {
tn = tn.replace(
// eslint-disable-next-line prefer-named-capture-group
/(\u0020*)(\S+)(\s*)$/,
orphanController
);
Expand Down Expand Up @@ -828,6 +846,18 @@
*/
function calculateBaseData(hpbBuf) {
const hpbMetaData = new Uint32Array(hpbBuf).subarray(0, 8);
if (hpbMetaData[0] !== 40005736) {
/*
* Pattern files must begin with "hpb2"
* Get current utf8 values with
* `new Uint8Array(Uint32Array.of(hpbMetaData[0]).buffer)`
*/
H.events.dispatch("error", {
"lvl": "error",
"msg": "Pattern file format error: " + new Uint8Array(Uint32Array.of(hpbMetaData[0]).buffer)
});
throw new Error("Pattern file format error!");
}
const valueStoreLength = hpbMetaData[7];
const valueStoreOffset = 1280;
const patternTrieOffset = valueStoreOffset + valueStoreLength +
Expand Down Expand Up @@ -1058,6 +1088,16 @@
false
);

H.events.define(
"loadError",
function onLoadError(e) {
if (e.msg !== "wasm") {
elements.rem(e.name);
}
},
false
);

H.events.define(
"engineReady",
function onEngineReady(e) {
Expand Down
67 changes: 45 additions & 22 deletions Hyphenopoly_Loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @license Hyphenopoly_Loader 2.8.1-devel - client side hyphenation
* @license Hyphenopoly_Loader 3.0.0 - client side hyphenation
* ©2019 Mathias Nater, Zürich (mathiasnater at gmail dot com)
* https://github.com/mnater/Hyphenopoly
*
Expand Down Expand Up @@ -63,16 +63,6 @@
(function configSetup() {
if (H.setup) {
H.setup.selectors = H.setup.selectors || {".hyphenate": {}};
if (H.setup.classnames) {
// Convert classnames to selectors
eachKey(H.setup.classnames, function cn2sel(cn) {
/* eslint-disable security/detect-object-injection */
H.setup.selectors["." + cn] = H.setup.classnames[cn];
/* eslint-enable security/detect-object-injection */
});
H.setup.classnames = null;
delete H.setup.classnames;
}
H.setup.timeout = H.setup.timeout || 1000;
H.setup.hide = H.setup.hide || "all";
} else {
Expand Down Expand Up @@ -223,6 +213,22 @@
false
);

define(
"loadError",
function def(e) {
deferred.push({
"data": e,
"name": "loadError"
});
}
);

define(
"tearDown",
null,
true
);

/**
* Dispatch event <name> with arguments <data>
* @param {string} name The name of the event
Expand Down Expand Up @@ -414,6 +420,13 @@
);
});
}
} else {
H.events.dispatch("loadError", {
"file": f,
"msg": m,
"name": n,
"path": p
});
}
}
);
Expand All @@ -436,17 +449,26 @@
loadedBins.set(f, [m]);
const xhr = new XMLHttpRequest();
xhr.onload = function onload() {
loadedBins.get(f).
forEach(function eachHpb(rn) {
H.binaries.set(
rn,
xhr.response
);
H.events.dispatch(
"hpbLoaded",
{"msg": rn}
);
if (xhr.statusText === "OK") {
loadedBins.get(f).
forEach(function eachHpb(rn) {
H.binaries.set(
rn,
xhr.response
);
H.events.dispatch(
"hpbLoaded",
{"msg": rn}
);
});
} else {
H.events.dispatch("loadError", {
"file": f,
"msg": m,
"name": n,
"path": p
});
}
};
xhr.open("GET", p + f);
xhr.responseType = "arraybuffer";
Expand Down Expand Up @@ -712,7 +734,7 @@
* @returns {undefined}
*/
function handleDCL() {
if (H.setup.hide.match(/^(element|text)$/)) {
if (H.setup.hide.match(/^(?:element|text)$/)) {
H.toggle("off");
}
H.events.dispatch(
Expand Down Expand Up @@ -744,6 +766,7 @@
handleDCL();
}
} else {
H.events.dispatch("tearDown", {});
window.Hyphenopoly = null;
}

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 Mathias Nater
Copyright (c) 2019 Mathias Nater

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
Hyphenopoly.js is a __JavaScript-polyfill for hyphenation in HTML__: it hyphenates text if the user agent does not support CSS-hyphenation at all or not for the required languages and it is a __Node.js-module__.

The package consists of the following parts:
- _Hyphenopoly_Loader.js_ (~24KB unpacked, ~3KB minified and compressed): feature-checks the client and loads other resources if necessary.
- _Hyphenopoly.js_ (~41KB unpacked, ~4KB minified and compressed): does the whole DOM-foo and wraps (w)asm.
- _Hyphenopoly_Loader.js_ (~27KB unpacked, ~4KB minified and compressed): feature-checks the client and loads other resources if necessary.
- _Hyphenopoly.js_ (~45KB unpacked, ~4KB minified and compressed): does the whole DOM-foo and wraps (w)asm.
- _hyphenEngine.wasm_ (~1KB uncompressed): wasm code for creating pattern trie and finding hyphenation points.
- _hyphenEngine.asm.js_ (~10KB uncompressed, ~1KB minified and compressed): fallback for clients that don't support wasm.
- _pattern.hpb_ (sizes differ! e.g. en-us.hpb: ~27KB uncompressed, ~16KB compressed): space saving binary format of the hyphenation patterns (including their license).
- _hyphenEngine.asm.js_ (~11KB uncompressed, ~1KB minified and compressed): fallback for clients that don't support wasm.
- _pattern.hpb_ (sizes differ! e.g. en-us.hpb: ~23KB uncompressed, ~14KB compressed): space saving binary format of the hyphenation patterns (including their license).
- _hyphenopoly.module.js_: the node module

## Usage (Browser)
Expand Down
19 changes: 16 additions & 3 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
/* eslint-env node */
/* eslint no-console: 0 */

"use strict";

// For RunKit:
const hyphenopoly = require("hyphenopoly");

// For local node:
// const hyphenopoly = require("./hyphenopoly.module.js");

const hyphenator = hyphenopoly.config({
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
},
"hyphen": "•",
"require": ["de", "en-us"]
});

/**
* Asyncly hyphenate english text
* @param {string} text - Words to by hyphenated
*/
async function hyphenateEn(text) {
const hyphenateText = await hyphenator.get("en-us");
console.log(hyphenateText(text));
}

/**
* Asyncly hyphenate germam text
* @param {string} text - Words to by hyphenated
*/
async function hyphenateDe(text) {
const hyphenateText = await hyphenator.get("de");
console.log(hyphenateText(text));
Expand Down
3 changes: 1 addition & 2 deletions example1.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
},
setup: {
selectors: {
".container": {
}
".container": {}
}
}
};
Expand Down
20 changes: 13 additions & 7 deletions exampleSync.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
/* eslint-env node */
/* eslint no-console: 0 */
"use strict";
// For RunKit:
const hyphenopoly = require("hyphenopoly");

// For local node:
// const hyphenopoly = require("./hyphenopoly.module.js");
/*
* For local node:
* const hyphenopoly = require("./hyphenopoly.module.js");
*/

const hyphenator = hyphenopoly.config({
"sync": true,
"require": ["de", "en-us"],
"hyphen": "•",
"exceptions": {
"en-us": "en-han-ces"
}
},
"hyphen": "•",
"require": ["de", "en-us"],
"sync": true
});

const hy1 = hyphenator.get("en-us")("hyphenation enhances justification.");
const hy2 = hyphenator.get("de")("Silbentrennung verbessert den Blocksatz.");


console.log(hy1);
console.log(hy2);
console.log(hy2);
Loading

0 comments on commit 1773a5e

Please sign in to comment.