forked from mnater/Hyphenator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hyphenator_Loader.js
140 lines (128 loc) · 5.17 KB
/
Hyphenator_Loader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/** @license Hyphenator_Loader 5.2.0(devel) - client side hyphenation for webbrowsers
* Copyright (C) 2015 Mathias Nater, Zürich (mathiasnater at gmail dot com)
* https://github.com/mnater/Hyphenator
*
* Released under the MIT license
* http://mnater.github.io/Hyphenator/LICENSE.txt
*/
/**
* @constructor
* @description Checks if there's CSS-hyphenation available for the given languages and
* loads and runs Hyphenator if there's no CSS-hyphenation
* @author Mathias Nater, <a href = "mailto:[email protected]">[email protected]</a>
* @version 5.2.0(devel)
* @namespace Holds all methods and properties
*/
/* The following comment is for JSLint: */
/*jslint browser: true*/
/*global window Hyphenator*/
var Hyphenator_Loader = (function (window) {
'use strict';
var languages,
config,
path,
/**
* @name Hyphenator-createElem
* @description
* A function alias to document.createElementNS or document.createElement
* @param {string} tagname the Element to create
* @type {function({string})}
* @private
*/
createElem = function (tagname) {
var r;
if (window.document.createElementNS) {
r = window.document.createElementNS('http://www.w3.org/1999/xhtml', tagname);
} else if (window.document.createElement) {
r = window.document.createElement(tagname);
}
return r;
},
/**
* @name Hyphenator-loadNrunHyphenator
* @description Loads Hyphenator.js and runs it with the given configuration
* @type {function({object})}
* @param {object} config the configuration object for Hyphenator.js
* @private
*/
loadNrunHyphenator = function (config) {
var head, script, done = false;
head = window.document.getElementsByTagName('head').item(0);
script = createElem('script');
script.src = path;
script.type = 'text/javascript';
script.onreadystatechange = function () {
if (!done && (!script.readyState || script.readyState === "loaded" || script.readyState === "complete")) {
done = true;
Hyphenator.config(config);
Hyphenator.run();
// Handle memory leak in IE
script.onreadystatechange = null;
script.onload = null;
if (head && script.parentNode) {
head.removeChild(script);
}
}
};
script.onload = script.onreadystatechange;
head.appendChild(script);
},
/**
* @name Hyphenator-checkLangSupport
* @description
* Checks if hyphenation for all languages are supported:
* If body is present (i.e. DOMContentLoaded) a hidden div is added to the body and the height of probably hyphenated text is measured.
* Else a fake body is inserted and used instead of the 'real' body. It will later be removed.
* @type {function()}
* @return {bool}
* @private
*/
checkLangSupport = function () {
var shadowContainer,
shadow,
lang,
fakeBdy = createElem('body');
shadowContainer = createElem('div');
shadowContainer.style.visibility = 'hidden';
fakeBdy.appendChild(shadowContainer);
window.document.documentElement.appendChild(fakeBdy);
for (lang in languages) {
if (languages.hasOwnProperty(lang)) {
shadow = createElem('div');
shadow.style.MozHyphens = 'auto';
shadow.style['-webkit-hyphens'] = 'auto';
shadow.style['-ms-hyphens'] = 'auto';
shadow.style.hyphens = 'auto';
shadow.style.width = '5em';
shadow.style.lineHeight = '12px';
shadow.style.border = 'none';
shadow.style.padding = '0';
shadow.style.wordWrap = 'normal';
shadow.style['-webkit-locale'] = "'" + lang + "'";
shadow.lang = lang;
shadow.appendChild(window.document.createTextNode(languages[lang]));
shadowContainer.appendChild(shadow);
if (shadow.offsetHeight === 12) {
loadNrunHyphenator(config);
break;
}
}
}
fakeBdy.parentNode.removeChild(fakeBdy);
};
return {
/**
* @name Hyphenator_Loader.init
* @description Bootstrap function that inits the loader
* @param {Object} languages an object with the language as key and a long word as value
* @param {Object} config the Hyphenator.js configuration object
* @public
*/
init: function (langs, p, configs) {
languages = langs;
path = p;
config = configs || {};
checkLangSupport();
}
};
}(window));