Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit cb9186ad57db51b82b454c06306b686f157afa64
Author: mnater <mathiasnater@gmail.com>
Date:   Fri Nov 1 22:32:46 2019 +0100

    add docs for module

commit 777b909e05c0836f4f1781274bde374a88d119fb
Author: mnater <mathiasnater@gmail.com>
Date:   Fri Nov 1 22:28:18 2019 +0100

    implement option mixedCase

    fixes issue #91

commit 325c0a763afa50af9d1d0d15595836fdfe570092
Author: mnater <mathiasnater@gmail.com>
Date:   Fri Nov 1 22:16:48 2019 +0100

    implement mixedCase option

    fixes issue #91
  • Loading branch information
mnater committed Nov 1, 2019
1 parent e27b59e commit 82fc22b
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Version History

## Version 3.4.0 (2019-11-xx)
### Added
* Option to disable mixed cased words (issue #91)

## Version 3.3.0 (2019-10-14)
### Added
* Add configuration option `keepAlive` (issue #69)
Expand Down
19 changes: 19 additions & 0 deletions Hyphenopoly.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"leftmin": setProp(0, 2),
"leftminPerLang": setProp(0, 2),
"minWordLength": setProp(6, 2),
"mixedCase": setProp(true, 2),
"orphanControl": setProp(1, 2),
"rightmin": setProp(0, 2),
"rightminPerLang": setProp(0, 2)
Expand Down Expand Up @@ -418,13 +419,30 @@
return word;
}

/**
* Checks if a string is mixed case
* @param {string} s The string
* @returns {boolean} true if s is mixed case
*/
function isMixedCase(s) {
return Array.prototype.map.call(s, function mapper(c) {
return (c === c.toLowerCase());
}).some(function checker(v, i, a) {
return (v !== a[0]);
});
}

/* eslint-disable complexity */
/**
* HyphenateFunction for words (compound or not)
* @param {string} word The word
* @returns {string} The hyphenated word
*/
function hyphenator(word) {
let hw = lo.cache.get(sel).get(word);
if (!classSettings.mixedCase && isMixedCase(word)) {
hw = word;
}
if (!hw) {
if (lo.exceptions.has(word)) {
hw = lo.exceptions.get(word).replace(
Expand Down Expand Up @@ -455,6 +473,7 @@
}
return hw;
}
/* eslint-enable complexity */
wordHyphenatorPool.set(lang + "-" + sel, hyphenator);
return hyphenator;
}
Expand Down
2 changes: 2 additions & 0 deletions docs/Node-Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Defaults:
"leftmin": 0,
"loader": "fs",
"minWordLength": 6,
"mixedCase": true,
"normalize": false,
"orphanControl": 1,
"paths": {
Expand Down Expand Up @@ -127,6 +128,7 @@ For documentation about the other options see the `Hyphenopoly.js`-documentation
- [hyphen](./Setup.md#hyphen)
- [leftmin](./Setup.md#leftmin-and-rightmin)
- [minWordLength](./Setup.md#minwordlength)
- [mixedCase](./Setup.md#mixedcase)
- [normalize](./Setup.md#normalize)
- [orphanControl](./Setup.md#orphancontrol)
- [paths](./Global-Hyphenopoly-Object.md#paths)
Expand Down
26 changes: 26 additions & 0 deletions docs/Setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ These page documents the optional fields in `setup`:
* [leftmin and rightmin](#leftmin-and-rightmin)
* [leftminPerLang and rightminPerLang](#leftminperlang-and-rightminperlang)
* [minWordLength](#minwordlength)
* [mixedCase](#mixedcase)
* [orphanControl](#orphancontrol)

## Global Settings
Expand Down Expand Up @@ -401,6 +402,31 @@ var Hyphenopoly = {
</script>
````

### mixedCase
````
type: boolean
default: true
````
If set to false, prevents hyphenation of mixed case words.
````html
<script>
var Hyphenopoly = {
require: {...},
paths: {...},
setup: {
selectors: {
".hyphenate": {
mixedCase: false
}
}
}
};
</script>
````
According to a [note in the css-text draft](https://drafts.csswg.org/css-text-3/#valdef-hyphens-auto)
mixed case words may not be hyphenated. This setting defaults to true, because this simple heuristic
excludes words at the beginning of a sentence from being hyphenated.

### orphanControl
````
type: number (1 | 2 | 3)
Expand Down
19 changes: 19 additions & 0 deletions hyphenopoly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,30 @@ function createWordHyphenator(lo, lang) {
return word;
}

/**
* Checks if a string is mixed case
* @param {string} s The string
* @returns {boolean} true if s is mixed case
*/
function isMixedCase(s) {
return Array.prototype.map.call(s, function mapper(c) {
return (c === c.toLowerCase());
}).some(function checker(v, i, a) {
return (v !== a[0]);
});
}

/* eslint-disable complexity */
/**
* HyphenateFunction for words (compound or not)
* @param {string} word The word
* @returns {string} The hyphenated word
*/
function hyphenator(word) {
let hw = lo.cache.get(word);
if (!H.c.mixedCase && isMixedCase(word)) {
hw = word;
}
if (!hw) {
if (lo.exceptions.has(word)) {
hw = lo.exceptions.get(word).replace(
Expand All @@ -624,6 +641,7 @@ function createWordHyphenator(lo, lang) {
}
return hw;
}
/* eslint-enable complexity */
wordHyphenatorPool.set(lang, hyphenator);
return hyphenator;
}
Expand Down Expand Up @@ -795,6 +813,7 @@ H.config = function config(userConfig) {
"leftmin": setProp(0, 2),
"loader": setProp("fs", 2),
"minWordLength": setProp(6, 2),
"mixedCase": setProp(true, 2),
"normalize": setProp(false, 2),
"orphanControl": setProp(1, 2),
"paths": setProp(Object.create(null, {
Expand Down
25 changes: 25 additions & 0 deletions test/configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,31 @@ t.test("set options: minWordLength", function (t) {
t.end();
});

t.test("set options: mixedCase", function (t) {
let H9Y = null;
t.beforeEach(function setup(done) {
H9Y = require("../hyphenopoly.module");
done();
});

t.afterEach(function tearDown(done) {
H9Y = null;
delete require.cache[require.resolve("../hyphenopoly.module")];
done();
});

t.test("mixedCase: false", async function (t) {
const hyphenator = await H9Y.config({
"hyphen": "•",
"mixedCase": false,
"require": ["de"]
});
t.equal(hyphenator("silbentrennen Silbentrennung camelCase"), "sil•ben•tren•nen Silbentrennung camelCase");
t.end();
});
t.end();
});

t.test("set options: normalize", function (t) {
let H9Y = null;
t.beforeEach(function setup(done) {
Expand Down
102 changes: 102 additions & 0 deletions testsuite/test40.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Test 040</title>
<script>
var Hyphenopoly = {
require: {
"en-us": "FORCEHYPHENOPOLY"
},
setup: {
selectors: {
".class1": {
hyphen: "·"
},
".class2": {
hyphen: "·",
mixedCase: false
}
}
},
handleEvent: {
hyphenopolyEnd: function (e) {
assert();
}
}
};
</script>
<script src="../Hyphenopoly_Loader.js"></script>
<script>
function assert() {
var tests = 2;
var i = 1;
var test = "";
var ref = "";
var result = true;
while (i <= tests) {
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: 40,
result: (result ? "passed" : "failed")
}), window.location.href);
}
}
if (!Hyphenopoly) {
document.addEventListener("DOMContentLoaded", function() {
assert();
});
}
</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="test39.html">&larr;&nbsp;Prev</a>&nbsp;|&nbsp;<a href="test41.html">Next&nbsp;&rarr;</a></div>

<h1>Test 040</h1>
<p id="desc">Handle mixed cased words</p>
<div id="result"></div>
<hr>
<h2>1: hyphenate mixed case</h2>
<p id="test1" class="test class1" lang="en-us">Hyphenation algorithm camelCase</p>
<p id="ref1" class="ref" lang="en-us">Hy·phen·ation al·go·rithm camel·Case</p>
<h2>2: don't hyphenate mixed case</h2>
<p id="test2" class="test class2" lang="en-us">Hyphenation algorithm camelCase</p>
<p id="ref2" class="ref" lang="en-us">Hyphenation al·go·rithm camelCase</p>
<hr>
<div><span class="test">Test</span> <span class="ref">Ref</span></div>
</body>
</html>
3 changes: 2 additions & 1 deletion testsuite/testdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
{"exec": true, "path": "test36.html"},
{"exec": true, "path": "test37.html"},
{"exec": true, "path": "test38.html"},
{"exec": true, "path": "test39.html"}
{"exec": true, "path": "test39.html"},
{"exec": true, "path": "test40.html"}
];
var testframe = document.getElementById("testframe");
var currentTest = 1;
Expand Down

0 comments on commit 82fc22b

Please sign in to comment.