Skip to content

Commit

Permalink
feat: Add "loadError"event to Recover from 404
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mnater committed Mar 21, 2019
1 parent b8049de commit 91b4622
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 46 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

## Unreleased
### Changed
* implement new hyphenation pattern binary (.hpb) format (issue #61)
* 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
* tearDown event (issue #67)
* feat: tearDown event (issue #67)
* feat: loadError event (issue #59)

### Removed
* remove `Hyphenopoly.setup.classnames` – use [selectors](https://github.com/mnater/Hyphenopoly/wiki/Global-Hyphenopoly-Object#new-with-version-260-selectors) instead
* 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)
Expand Down
11 changes: 11 additions & 0 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,17 @@
false
);

H.events.define(
"loadError",
function onLoadError(e) {
if (e.msg !== "wasm") {
elements.list.delete(e.name);
elements.counter[0] -= 1;
}
},
false
);

H.events.define(
"engineReady",
function onEngineReady(e) {
Expand Down
17 changes: 17 additions & 0 deletions Hyphenopoly_Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@
false
);

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

define(
"tearDown",
null,
Expand Down Expand Up @@ -410,6 +420,13 @@
);
});
}
} else {
H.events.dispatch("loadError", {
"file": f,
"msg": m,
"name": n,
"path": p
});
}
}
);
Expand Down
101 changes: 101 additions & 0 deletions testsuite/test34.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test 034</title>
<script>
var Hyphenopoly = {
require: {
"foobar": "garzabariktimalborsi",
"en-us": "FORCEHYPHENOPOLY"
},
paths: {
maindir: "../",
patterndir: "../patterns/"
},
setup: {
safeCopy: false,
checkRes: true
},
handleEvent: {
hyphenopolyEnd: function (e) {
assert();
}
}
};
function assert() {
var tests = 2;
var i = 1;
var test = "";
var ref = "";
var result = true;
var lang = "";
while (i <= tests) {
lang = document.getElementById("test" + i).lang;
if (Hyphenopoly.clientFeat.langs[lang] === "CSS") {
document.getElementById("result").innerHTML += "<p style=\"background-color: #d6ffd6\">" + i + " passed (CSS)</p>";
result = result && true;
} else {
test = document.getElementById("test" + i).innerHTML;
ref = document.getElementById("ref" + i).innerHTML;
if (test === ref) {
document.getElementById("result").innerHTML += "<p style=\"background-color: #d6ffd6\">" + i + " passed</p>";
result = result && true;
} else {
document.getElementById("result").innerHTML += "<p style=\"background-color: #ffd6d6\">" + i + " failed</p>";
result = false;
}
}
i += 1;
}
if (parent != window) {
parent.postMessage(JSON.stringify({
desc: document.getElementById("desc").innerHTML,
index: 34,
result: (result ? "passed" : "failed")
}), window.location.href);
}
}
</script>
<script src="../Hyphenopoly_Loader.js"></script>
<style type="text/css">
body {
width:50%;
margin-left:25%;
margin-right:25%;
}

.test {
background-color: #D8E2F9;
}
.ref {
background-color: #FEEFC0;
}

.hyphenate {
hyphens: auto;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
}
</style>
</head>
<body>
<div id="navigate"><a href="index.html">&Larr;&nbsp;Index</a>&nbsp;|&nbsp;<a href="test33.html">&larr;&nbsp;Prev</a>&nbsp;|&nbsp;<a href="test35.html">Next&nbsp;&rarr;</a></div>

<h1>Test 034</h1>
<p id="desc">Recover from 404 if .hpb doesn't exist.</p>
<div id="result"></div>
<hr>
<h2>1: foobar</h2>
<p id="test1" class="test hyphenate" lang="foobar">Izgarz blonab schub ei follar quabsi zubur ma na fnaghi. Dalla burksi fad pelo i fnagha werksnar.</p>
<p id="ref1" class="ref" lang="foobar">Izgarz blonab schub ei follar quabsi zubur ma na fnaghi. Dalla burksi fad pelo i fnagha werksnar.</p>
<h2>2: en</h2>
<p id="test2" class="test hyphenate" lang="en-us">A hyphenation algorithm is a set of rules that decides at which points a word can be broken over two lines with a hyphen.</p>
<p id="ref2" class="ref" lang="en">A hy&shy;phen&shy;ation al&shy;go&shy;rithm is a set of rules that de&shy;cides at which points a word can be bro&shy;ken over two lines with a hy&shy;phen.</p>

<hr>
<div><span class="test">Test</span> <span class="ref">Ref</span></div>

</body>
</html>
96 changes: 53 additions & 43 deletions testsuite/testdriver.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,88 @@
/*jslint browser*/
/*global window*/
//clear Storage
/* eslint-disable object-property-newline */
/* eslint-disable no-var */

(function testDriver() {
"use strict";
var tests = [
{exec: false, path: "test0.html"}, //dont exec!
{exec: true, path: "test1.html"},
{exec: true, path: "test2.html"},
{exec: true, path: "test3.html"},
{exec: true, path: "test4.html"},
{exec: true, path: "test5.html"},
{exec: true, path: "test6.html"},
{exec: true, path: "test7.html"},
{exec: true, path: "test8.html"},
{exec: true, path: "test9.html"},
{exec: true, path: "test10.html"},
{exec: true, path: "test11.html"},
{exec: true, path: "test12.html"},
{exec: true, path: "test13.html"},
{exec: true, path: "test14.html"},
{exec: true, path: "test15.html"},
{exec: true, path: "test16.html"},
{exec: true, path: "test17.html"},
{exec: true, path: "test18.html"},
{exec: true, path: "test19.html"},
{exec: true, path: "test20.html"},
{exec: true, path: "test21.html"},
{exec: true, path: "test22.html"},
{exec: true, path: "test23.html"},
{exec: true, path: "test24.html"},
{exec: true, path: "test25.html"},
{exec: true, path: "test26.html"},
{exec: true, path: "test27.html"},
{exec: true, path: "test28.html"},
{exec: true, path: "test29.html"},
{exec: true, path: "test30.html"},
{exec: true, path: "test31.html"},
{exec: true, path: "test32.html"},
{exec: true, path: "test33.html"}
{"exec": false, "path": "test0.html"},
{"exec": true, "path": "test1.html"},
{"exec": true, "path": "test2.html"},
{"exec": true, "path": "test3.html"},
{"exec": true, "path": "test4.html"},
{"exec": true, "path": "test5.html"},
{"exec": true, "path": "test6.html"},
{"exec": true, "path": "test7.html"},
{"exec": true, "path": "test8.html"},
{"exec": true, "path": "test9.html"},
{"exec": true, "path": "test10.html"},
{"exec": true, "path": "test11.html"},
{"exec": true, "path": "test12.html"},
{"exec": true, "path": "test13.html"},
{"exec": true, "path": "test14.html"},
{"exec": true, "path": "test15.html"},
{"exec": true, "path": "test16.html"},
{"exec": true, "path": "test17.html"},
{"exec": true, "path": "test18.html"},
{"exec": true, "path": "test19.html"},
{"exec": true, "path": "test20.html"},
{"exec": true, "path": "test21.html"},
{"exec": true, "path": "test22.html"},
{"exec": true, "path": "test23.html"},
{"exec": true, "path": "test24.html"},
{"exec": true, "path": "test25.html"},
{"exec": true, "path": "test26.html"},
{"exec": true, "path": "test27.html"},
{"exec": true, "path": "test28.html"},
{"exec": true, "path": "test29.html"},
{"exec": true, "path": "test30.html"},
{"exec": true, "path": "test31.html"},
{"exec": true, "path": "test32.html"},
{"exec": true, "path": "test33.html"},
{"exec": true, "path": "test34.html"}
];
var testframe = document.getElementById("testframe");
var currentTest = 1;
var total = "passed";

/**
* Add test result to the DOM
* @param {string} name – Filename
* @param {string} desc - Test description
* @param {string} result - Result (failed or passed)
*/
function addTestResult(name, desc, result) {
var dl = document.getElementById("testresults");
var template = document.getElementById("template").innerHTML;
template = template.replace(/@file@/g, name);
template = template.replace(/@desc@/, desc);
template = template.replace(/@result@/g, result);
dl.innerHTML = dl.innerHTML + template;
dl.innerHTML += template;
window.scrollBy(0, 20);
}

/**
* Runs tests
* @param {number} index - Index of the test
*/
function run(index) {
/* eslint-disable security/detect-object-injection */
if (tests[index]) {
currentTest = index;
if (tests[index].exec) {
window.setTimeout(function () {
window.setTimeout(function defer() {
testframe.src = tests[index].path;
}, 0);
} else {
addTestResult(tests[index].path, "omitted", "omitted");
run(index + 1);
return;
}
} else {
addTestResult("", navigator.userAgent, total);
return;
}
/* eslint-enable security/detect-object-injection */
}

window.addEventListener("message", function (e) {
window.addEventListener("message", function onMessage(e) {
var msg = JSON.parse(e.data);
addTestResult(tests[msg.index].path, msg.desc, msg.result);
if (msg.result === "failed") {
Expand All @@ -82,4 +92,4 @@
}, false);

run(currentTest);
}());
}());

0 comments on commit 91b4622

Please sign in to comment.