This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Hyphenator.js
3517 lines (3359 loc) · 135 KB
/
Hyphenator.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @license Hyphenator 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
*/
/*
* Comments are jsdoc3 formatted. See http://usejsdoc.org
* Use mergeAndPack.html to get rid of the comments and to reduce the file size of this script!
*/
/* The following comment is for JSLint: */
/*jslint browser: true, multivar: true, eval: true*/
/*global Hyphenator window*/
/**
* @desc Provides all functionality to do hyphenation, except the patterns that are loaded externally
* @global
* @namespace Hyphenator
* @author Mathias Nater, <[email protected]>
* @version 5.2.0(devel)
* @example
* <script src = "Hyphenator.js" type = "text/javascript"></script>
* <script type = "text/javascript">
* Hyphenator.run();
* </script>
*/
var Hyphenator;
Hyphenator = (function (window) {
"use strict";
/**
* @member Hyphenator~contextWindow
* @access private
* @desc
* contextWindow stores the window for the actual document to be hyphenated.
* If there are frames this will change.
* So use contextWindow instead of window!
*/
var contextWindow = window;
/**
* @member {Object.<string, Hyphenator~supportedLangs~supportedLanguage>} Hyphenator~supportedLangs
* @desc
* A generated key-value object that stores supported languages and meta data.
* The key is the {@link http://tools.ietf.org/rfc/bcp/bcp47.txt bcp47} code of the language and the value
* is an object of type {@link Hyphenator~supportedLangs~supportedLanguage}
* @namespace Hyphenator~supportedLangs
* @access private
* //Check if language lang is supported:
* if (supportedLangs.hasOwnProperty(lang))
*/
var supportedLangs = (function () {
/**
* @typedef {Object} Hyphenator~supportedLangs~supportedLanguage
* @property {string} file - The name of the pattern file
* @property {number} script - The script type of the language (e.g. "latin" for english), this type is abbreviated by an id
* @property {string} prompt - The sentence prompted to the user, if Hyphenator.js doesn"t find a language hint
*/
/**
* @lends Hyphenator~supportedLangs
*/
var r = {},
/**
* @method Hyphenator~supportedLangs~o
* @desc
* Sets a value of Hyphenator~supportedLangs
* @access protected
* @param {string} code The {@link http://tools.ietf.org/rfc/bcp/bcp47.txt bcp47} code of the language
* @param {string} file The name of the pattern file
* @param {Number} script A shortcut for a specific script: latin:0, cyrillic: 1, arabic: 2, armenian:3, bengali: 4, devangari: 5, greek: 6
* gujarati: 7, kannada: 8, lao: 9, malayalam: 10, oriya: 11, persian: 12, punjabi: 13, tamil: 14, telugu: 15, georgian: 16
* @param {string} prompt The sentence prompted to the user, if Hyphenator.js doesn"t find a language hint
*/
o = function (code, file, script, prompt) {
r[code] = {"file": file, "script": script, "prompt": prompt};
};
o("be", "be.js", 1, "Мова гэтага сайта не можа быць вызначаны аўтаматычна. Калі ласка пакажыце мову:");
o("ca", "ca.js", 0, "");
o("cs", "cs.js", 0, "Jazyk této internetové stránky nebyl automaticky rozpoznán. Určete prosím její jazyk:");
o("cu", "cu.js", 1, "Ꙗ҆зы́къ сегѡ̀ са́йта не мо́жетъ ѡ҆предѣле́нъ бы́ти. Прошꙋ́ тѧ ᲂу҆каза́ти ꙗ҆зы́къ:");
o("da", "da.js", 0, "Denne websides sprog kunne ikke bestemmes. Angiv venligst sprog:");
o("bn", "bn.js", 4, "");
o("de", "de.js", 0, "Die Sprache dieser Webseite konnte nicht automatisch bestimmt werden. Bitte Sprache angeben:");
o("el", "el-monoton.js", 6, "");
o("el-monoton", "el-monoton.js", 6, "");
o("el-polyton", "el-polyton.js", 6, "");
o("en", "en-us.js", 0, "The language of this website could not be determined automatically. Please indicate the main language:");
o("en-gb", "en-gb.js", 0, "The language of this website could not be determined automatically. Please indicate the main language:");
o("en-us", "en-us.js", 0, "The language of this website could not be determined automatically. Please indicate the main language:");
o("eo", "eo.js", 0, "La lingvo de ĉi tiu retpaĝo ne rekoneblas aŭtomate. Bonvolu indiki ĝian ĉeflingvon:");
o("es", "es.js", 0, "El idioma del sitio no pudo determinarse autom%E1ticamente. Por favor, indique el idioma principal:");
o("et", "et.js", 0, "Veebilehe keele tuvastamine ebaõnnestus, palun valige kasutatud keel:");
o("fi", "fi.js", 0, "Sivun kielt%E4 ei tunnistettu automaattisesti. M%E4%E4rit%E4 sivun p%E4%E4kieli:");
o("fr", "fr.js", 0, "La langue de ce site n%u2019a pas pu %EAtre d%E9termin%E9e automatiquement. Veuillez indiquer une langue, s.v.p.%A0:");
o("ga", "ga.js", 0, "Níorbh fhéidir teanga an tsuímh a fháil go huathoibríoch. Cuir isteach príomhtheanga an tsuímh:");
o("grc", "grc.js", 6, "");
o("gu", "gu.js", 7, "");
o("hi", "hi.js", 5, "");
o("hu", "hu.js", 0, "A weboldal nyelvét nem sikerült automatikusan megállapítani. Kérem adja meg a nyelvet:");
o("hy", "hy.js", 3, "Չհաջողվեց հայտնաբերել այս կայքի լեզուն։ Խնդրում ենք նշեք հիմնական լեզուն՝");
o("it", "it.js", 0, "Lingua del sito sconosciuta. Indicare una lingua, per favore:");
o("ka", "ka.js", 16, "");
o("kn", "kn.js", 8, "ಜಾಲ ತಾಣದ ಭಾಷೆಯನ್ನು ನಿರ್ಧರಿಸಲು ಸಾಧ್ಯವಾಗುತ್ತಿಲ್ಲ. ದಯವಿಟ್ಟು ಮುಖ್ಯ ಭಾಷೆಯನ್ನು ಸೂಚಿಸಿ:");
o("la", "la.js", 0, "");
o("lt", "lt.js", 0, "Nepavyko automatiškai nustatyti šios svetainės kalbos. Prašome įvesti kalbą:");
o("lv", "lv.js", 0, "Šīs lapas valodu nevarēja noteikt automātiski. Lūdzu norādiet pamata valodu:");
o("ml", "ml.js", 10, "ഈ വെ%u0D2C%u0D4D%u200Cസൈറ്റിന്റെ ഭാഷ കണ്ടുപിടിയ്ക്കാ%u0D28%u0D4D%u200D കഴിഞ്ഞില്ല. ഭാഷ ഏതാണെന്നു തിരഞ്ഞെടുക്കുക:");
o("nb", "nb-no.js", 0, "Nettstedets språk kunne ikke finnes automatisk. Vennligst oppgi språk:");
o("no", "nb-no.js", 0, "Nettstedets språk kunne ikke finnes automatisk. Vennligst oppgi språk:");
o("nb-no", "nb-no.js", 0, "Nettstedets språk kunne ikke finnes automatisk. Vennligst oppgi språk:");
o("nl", "nl.js", 0, "De taal van deze website kan niet automatisch worden bepaald. Geef de hoofdtaal op:");
o("or", "or.js", 11, "");
o("pa", "pa.js", 13, "");
o("pl", "pl.js", 0, "Języka tej strony nie można ustalić automatycznie. Proszę wskazać język:");
o("pt", "pt.js", 0, "A língua deste site não pôde ser determinada automaticamente. Por favor indique a língua principal:");
o("ru", "ru.js", 1, "Язык этого сайта не может быть определен автоматически. Пожалуйста укажите язык:");
o("sk", "sk.js", 0, "");
o("sl", "sl.js", 0, "Jezika te spletne strani ni bilo mogoče samodejno določiti. Prosim navedite jezik:");
o("sr-cyrl", "sr-cyrl.js", 1, "Језик овог сајта није детектован аутоматски. Молим вас наведите језик:");
o("sr-latn", "sr-latn.js", 0, "Jezika te spletne strani ni bilo mogoče samodejno določiti. Prosim navedite jezik:");
o("sv", "sv.js", 0, "Spr%E5ket p%E5 den h%E4r webbplatsen kunde inte avg%F6ras automatiskt. V%E4nligen ange:");
o("ta", "ta.js", 14, "");
o("te", "te.js", 15, "");
o("tr", "tr.js", 0, "Bu web sitesinin dili otomatik olarak tespit edilememiştir. Lütfen dökümanın dilini seçiniz%A0:");
o("uk", "uk.js", 1, "Мова цього веб-сайту не може бути визначена автоматично. Будь ласка, вкажіть головну мову:");
o("ro", "ro.js", 0, "Limba acestui sit nu a putut fi determinată automat. Alege limba principală:");
return r;
}());
/**
* @member {Object} Hyphenator~locality
* @desc
* An object storing isBookmarklet, basePath and isLocal
* @access private
* @see {@link Hyphenator~loadPatterns}
*/
var locality = (function getLocality() {
var r = {
isBookmarklet: false,
basePath: "//mnater.github.io/Hyphenator/",
isLocal: false
};
var fullPath;
function getBasePath(path) {
if (!path) {
return r.basePath;
}
return path.substring(0, path.lastIndexOf("/") + 1);
}
function findCurrentScript() {
var scripts = contextWindow.document.getElementsByTagName("script");
var num = scripts.length - 1;
var currScript;
var src;
while (num >= 0) {
currScript = scripts[num];
if ((currScript.src || currScript.hasAttribute("src")) && currScript.src.indexOf("Hyphenator") !== -1) {
src = currScript.src;
break;
}
num -= 1;
}
return src;
}
if (!!document.currentScript) {
fullPath = document.currentScript.src;
} else {
fullPath = findCurrentScript();
}
r.basePath = getBasePath(fullPath);
if (fullPath && fullPath.indexOf("bm=true") !== -1) {
r.isBookmarklet = true;
}
if (window.location.href.indexOf(r.basePath) !== -1) {
r.isLocal = true;
}
return r;
}());
/**
* @member {string} Hyphenator~basePath
* @desc
* A string storing the basepath from where Hyphenator.js was loaded.
* This is used to load the pattern files.
* The basepath is determined dynamically in getLocality by searching all script-tags for Hyphenator.js
* If the path cannot be determined {@link http://mnater.github.io/Hyphenator/} is used as fallback.
* @access private
* @see {@link Hyphenator~loadPatterns}
*/
var basePath = locality.basePath;
/**
* @member {boolean} Hyphenator~isLocal
* @access private
* @desc
* This is computed by getLocality.
* isLocal is true, if Hyphenator is loaded from the same domain, as the webpage, but false, if
* it"s loaded from an external source (i.e. directly from github)
*/
var isLocal = locality.isLocal;
/**
* @member {boolean} Hyphenator~documentLoaded
* @access private
* @desc
* documentLoaded is true, when the DOM has been loaded. This is set by {@link Hyphenator~runWhenLoaded}
*/
var documentLoaded = false;
/**
* @member {boolean} Hyphenator~persistentConfig
* @access private
* @desc
* if persistentConfig is set to true (defaults to false), config options and the state of the
* toggleBox are stored in DOM-storage (according to the storage-setting). So they haven"t to be
* set for each page.
* @default false
* @see {@link Hyphenator.config}
*/
var persistentConfig = false;
/**
* @member {boolean} Hyphenator~doFrames
* @access private
* @desc
* switch to control if frames/iframes should be hyphenated, too.
* defaults to false (frames are a bag of hurt!)
* @default false
* @see {@link Hyphenator.config}
*/
var doFrames = false;
/**
* @member {Object.<string,boolean>} Hyphenator~dontHyphenate
* @desc
* A key-value object containing all html-tags whose content should not be hyphenated
* @access private
*/
var dontHyphenate = {"video": true, "audio": true, "script": true, "code": true, "pre": true, "img": true, "br": true, "samp": true, "kbd": true, "var": true, "abbr": true, "acronym": true, "sub": true, "sup": true, "button": true, "option": true, "label": true, "textarea": true, "input": true, "math": true, "svg": true, "style": true};
/**
* @member {boolean} Hyphenator~enableCache
* @desc
* A variable to set if caching is enabled or not
* @default true
* @access private
* @see {@link Hyphenator.config}
*/
var enableCache = true;
/**
* @member {string} Hyphenator~storageType
* @desc
* A variable to define what html5-DOM-Storage-Method is used ("none", "local" or "session")
* @default "local"
* @access private
* @see {@link Hyphenator.config}
*/
var storageType = "local";
/**
* @member {Object|undefined} Hyphenator~storage
* @desc
* An alias to the storage defined in storageType. This is set by {@link Hyphenator~createStorage}.
* Set by {@link Hyphenator.run}
* @default null
* @access private
* @see {@link Hyphenator~createStorage}
*/
var storage;
/**
* @member {boolean} Hyphenator~enableReducedPatternSet
* @desc
* A variable to set if storing the used patterns is set
* @default false
* @access private
* @see {@link Hyphenator.config}
* @see {@link Hyphenator.getRedPatternSet}
*/
var enableReducedPatternSet = false;
/**
* @member {boolean} Hyphenator~enableRemoteLoading
* @desc
* A variable to set if pattern files should be loaded remotely or not
* @default true
* @access private
* @see {@link Hyphenator.config}
*/
var enableRemoteLoading = true;
/**
* @member {boolean} Hyphenator~displayToggleBox
* @desc
* A variable to set if the togglebox should be displayed or not
* @default false
* @access private
* @see {@link Hyphenator.config}
*/
var displayToggleBox = false;
/**
* @method Hyphenator~onError
* @desc
* A function that can be called upon an error.
* @see {@link Hyphenator.config}
* @access private
*/
var onError = function (e) {
window.alert("Hyphenator.js says:\n\nAn Error occurred:\n" + e.message);
};
/**
* @method Hyphenator~onWarning
* @desc
* A function that can be called upon a warning.
* @see {@link Hyphenator.config}
* @access private
*/
var onWarning = function (e) {
window.console.log(e.message);
};
/**
* @method Hyphenator~createElem
* @desc
* A function alias to document.createElementNS or document.createElement
* @access private
*/
function createElem(tagname, context) {
context = context || contextWindow;
var el;
if (window.document.createElementNS) {
el = context.document.createElementNS("http://www.w3.org/1999/xhtml", tagname);
} else if (window.document.createElement) {
el = context.document.createElement(tagname);
}
return el;
}
/**
* @method Hyphenator~forEachKey
* @desc
* Calls the function f on every property of o
* @access private
*/
function forEachKey(o, f) {
var k;
if (Object.hasOwnProperty("keys")) {
Object.keys(o).forEach(f);
} else {
for (k in o) {
if (o.hasOwnProperty(k)) {
f(k);
}
}
}
}
/**
* @member {boolean} Hyphenator~css3
* @desc
* A variable to set if css3 hyphenation should be used
* @default false
* @access private
* @see {@link Hyphenator.config}
*/
var css3 = false;
/**
* @method Hyphenator~css3_gethsupport
* @desc
* This function returns a {@link Hyphenator~css3_hsupport} object for the current UA
* @type function
* @access private
* @see Hyphenator~css3_h9n
*/
function css3_gethsupport() {
var support = false,
supportedBrowserLangs = {},
property = "",
checkLangSupport,
createLangSupportChecker = function (prefix) {
var testStrings = [
//latin: 0
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
//cyrillic: 1
"абвгдеёжзийклмнопрстуфхцчшщъыьэюя",
//arabic: 2
"أبتثجحخدذرزسشصضطظعغفقكلمنهوي",
//armenian: 3
"աբգդեզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆ",
//bengali: 4
"ঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ",
//devangari: 5
"ँंःअआइईउऊऋऌएऐओऔकखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसहऽािीुूृॄेैोौ्॒॑ॠॡॢॣ",
//greek: 6
"αβγδεζηθικλμνξοπρσςτυφχψω",
//gujarati: 7
"બહઅઆઇઈઉઊઋૠએઐઓઔાિીુૂૃૄૢૣેૈોૌકખગઘઙચછજઝઞટઠડઢણતથદધનપફસભમયરલળવશષ",
//kannada: 8
"ಂಃಅಆಇಈಉಊಋಌಎಏಐಒಓಔಕಖಗಘಙಚಛಜಝಞಟಠಡಢಣತಥದಧನಪಫಬಭಮಯರಱಲಳವಶಷಸಹಽಾಿೀುೂೃೄೆೇೈೊೋೌ್ೕೖೞೠೡ",
//lao: 9
"ກຂຄງຈຊຍດຕຖທນບປຜຝພຟມຢຣລວສຫອຮະັາິີຶືຸູົຼເແໂໃໄ່້໊໋ໜໝ",
//malayalam: 10
"ംഃഅആഇഈഉഊഋഌഎഏഐഒഓഔകഖഗഘങചഛജഝഞടഠഡഢണതഥദധനപഫബഭമയരറലളഴവശഷസഹാിീുൂൃെേൈൊോൌ്ൗൠൡൺൻർൽൾൿ",
//oriya: 11
"ଁଂଃଅଆଇଈଉଊଋଌଏଐଓଔକଖଗଘଙଚଛଜଝଞଟଠଡଢଣତଥଦଧନପଫବଭମଯରଲଳଵଶଷସହାିୀୁୂୃେୈୋୌ୍ୗୠୡ",
//persian: 12
"أبتثجحخدذرزسشصضطظعغفقكلمنهوي",
//punjabi: 13
"ਁਂਃਅਆਇਈਉਊਏਐਓਔਕਖਗਘਙਚਛਜਝਞਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਲ਼ਵਸ਼ਸਹਾਿੀੁੂੇੈੋੌ੍ੰੱ",
//tamil: 14
"ஃஅஆஇஈஉஊஎஏஐஒஓஔகஙசஜஞடணதநனபமயரறலளழவஷஸஹாிீுூெேைொோௌ்ௗ",
//telugu: 15
"ఁంఃఅఆఇఈఉఊఋఌఎఏఐఒఓఔకఖగఘఙచఛజఝఞటఠడఢణతథదధనపఫబభమయరఱలళవశషసహాిీుూృౄెేైొోౌ్ౕౖౠౡ",
//georgian: 16
"აიერთხტუფბლდნვკწსგზმქყშჩცძჭჯოღპჟჰ"
],
f = function (lang) {
var shadow,
computedHeight,
bdy,
r = false;
//check if lang has already been tested
if (supportedBrowserLangs.hasOwnProperty(lang)) {
r = supportedBrowserLangs[lang];
} else if (supportedLangs.hasOwnProperty(lang)) {
//create and append shadow-test-element
bdy = window.document.getElementsByTagName("body")[0];
shadow = createElem("div", window);
shadow.id = "Hyphenator_LanguageChecker";
shadow.style.width = "5em";
shadow.style.padding = "0";
shadow.style.border = "none";
shadow.style[prefix] = "auto";
shadow.style.hyphens = "auto";
shadow.style.fontSize = "12px";
shadow.style.lineHeight = "12px";
shadow.style.wordWrap = "normal";
shadow.style.wordBreak = "normal";
shadow.style.visibility = "hidden";
shadow.lang = lang;
shadow.style["-webkit-locale"] = "\"" + lang + "\"";
shadow.innerHTML = testStrings[supportedLangs[lang].script];
bdy.appendChild(shadow);
//measure its height
computedHeight = shadow.offsetHeight;
//remove shadow element
bdy.removeChild(shadow);
r = !!(computedHeight > 12);
supportedBrowserLangs[lang] = r;
} else {
r = false;
}
return r;
};
return f;
},
s;
if (window.getComputedStyle) {
s = window.getComputedStyle(window.document.getElementsByTagName("body")[0], null);
if (s.hyphens !== undefined) {
support = true;
property = "hyphens";
checkLangSupport = createLangSupportChecker("hyphens");
} else if (s["-webkit-hyphens"] !== undefined) {
support = true;
property = "-webkit-hyphens";
checkLangSupport = createLangSupportChecker("-webkit-hyphens");
} else if (s.MozHyphens !== undefined) {
support = true;
property = "-moz-hyphens";
checkLangSupport = createLangSupportChecker("MozHyphens");
} else if (s["-ms-hyphens"] !== undefined) {
support = true;
property = "-ms-hyphens";
checkLangSupport = createLangSupportChecker("-ms-hyphens");
}
} //else we just return the initial values because ancient browsers don"t support css3 anyway
return {
support: support,
property: property,
supportedBrowserLangs: supportedBrowserLangs,
checkLangSupport: checkLangSupport
};
}
/**
* @typedef {Object} Hyphenator~css3_hsupport
* @property {boolean} support - if css3-hyphenation is supported
* @property {string} property - the css property name to access hyphen-settings (e.g. -webkit-hyphens)
* @property {Object.<string, boolean>} supportedBrowserLangs - an object caching tested languages
* @property {function} checkLangSupport - a method that checks if the browser supports a requested language
*/
/**
* @member {Hyphenator~css3_h9n} Hyphenator~css3_h9n
* @desc
* A generated object containing information for CSS3-hyphenation support
* This is set by {@link Hyphenator~css3_gethsupport}
* @default undefined
* @access private
* @see {@link Hyphenator~css3_gethsupport}
* @example
* //Check if browser supports a language
* css3_h9n.checkLangSupport(<lang>)
*/
var css3_h9n;
/**
* @member {string} Hyphenator~hyphenateClass
* @desc
* A string containing the css-class-name for the hyphenate class
* @default "hyphenate"
* @access private
* @example
* <p class = "hyphenate">Text</p>
* @see {@link Hyphenator.config}
*/
var hyphenateClass = "hyphenate";
/**
* @member {string} Hyphenator~urlHyphenateClass
* @desc
* A string containing the css-class-name for the urlhyphenate class
* @default "urlhyphenate"
* @access private
* @example
* <p class = "urlhyphenate">Text</p>
* @see {@link Hyphenator.config}
*/
var urlHyphenateClass = "urlhyphenate";
/**
* @member {string} Hyphenator~classPrefix
* @desc
* A string containing a unique className prefix to be used
* whenever Hyphenator sets a CSS-class
* @access private
*/
var classPrefix = "Hyphenator" + Math.round(Math.random() * 1000);
/**
* @member {string} Hyphenator~hideClass
* @desc
* The name of the class that hides elements
* @access private
*/
var hideClass = classPrefix + "hide";
/**
* @member {RegExp} Hyphenator~hideClassRegExp
* @desc
* RegExp to remove hideClass from a list of classes
* @access private
*/
var hideClassRegExp = new RegExp("\\s?\\b" + hideClass + "\\b", "g");
/**
* @member {string} Hyphenator~hideClass
* @desc
* The name of the class that unhides elements
* @access private
*/
var unhideClass = classPrefix + "unhide";
/**
* @member {RegExp} Hyphenator~hideClassRegExp
* @desc
* RegExp to remove unhideClass from a list of classes
* @access private
*/
var unhideClassRegExp = new RegExp("\\s?\\b" + unhideClass + "\\b", "g");
/**
* @member {string} Hyphenator~css3hyphenateClass
* @desc
* The name of the class that hyphenates elements with css3
* @access private
*/
var css3hyphenateClass = classPrefix + "css3hyphenate";
/**
* @member {CSSEdit} Hyphenator~css3hyphenateClass
* @desc
* The var where CSSEdit class is stored
* @access private
*/
var css3hyphenateClassHandle;
/**
* @member {string} Hyphenator~dontHyphenateClass
* @desc
* A string containing the css-class-name for elements that should not be hyphenated
* @default "donthyphenate"
* @access private
* @example
* <p class = "donthyphenate">Text</p>
* @see {@link Hyphenator.config}
*/
var dontHyphenateClass = "donthyphenate";
/**
* @member {number} Hyphenator~min
* @desc
* A number wich indicates the minimal length of words to hyphenate.
* @default 6
* @access private
* @see {@link Hyphenator.config}
*/
var min = 6;
/**
* @member {number} Hyphenator~leftmin
* @desc
* A number wich indicates the minimal length of characters before the first hyphenation.
* This value is only used if it is greater than the value in the pattern file.
* @default given by pattern file
* @access private
* @see {@link Hyphenator.config}
*/
var leftmin = 0;
/**
* @member {number} Hyphenator~rightmin
* @desc
* A number wich indicates the minimal length of characters after the last hyphenation.
* This value is only used if it is greater than the value in the pattern file.
* @default given by pattern file
* @access private
* @see {@link Hyphenator.config}
*/
var rightmin = 0;
/**
* @member {number} Hyphenator~rightmin
* @desc
* Control how compound words are hyphenated.
* "auto": factory-made -> fac-tory-made ("old" behaviour of Hyphenator.js)
* "all": factory-made -> fac-tory-[ZWSP]made ("made".length < minWordLength)
* "hyphen": factory-made -> factory-[ZWSP]made (Zero Width Space inserted after "-" to provide line breaking opportunity)
* @default "auto"
* @access private
* @see {@link Hyphenator.config}
*/
var compound = "auto";
/**
* @member {number} Hyphenator~orphanControl
* @desc
* Control how the last words of a line are handled:
* level 1 (default): last word is hyphenated
* level 2: last word is not hyphenated
* level 3: last word is not hyphenated and last space is non breaking
* @default 1
* @access private
*/
var orphanControl = 1;
/**
* @member {boolean} Hyphenator~isBookmarklet
* @desc
* This is computed by getLocality.
* True if Hyphanetor runs as bookmarklet.
* @access private
*/
var isBookmarklet = locality.isBookmarklet;
/**
* @member {string|null} Hyphenator~mainLanguage
* @desc
* The general language of the document. In contrast to {@link Hyphenator~defaultLanguage},
* mainLanguage is defined by the client (i.e. by the html or by a prompt).
* @access private
* @see {@link Hyphenator~autoSetMainLanguage}
*/
var mainLanguage = null;
/**
* @member {string|null} Hyphenator~defaultLanguage
* @desc
* The language defined by the developper. This language setting is defined by a config option.
* It is overwritten by any html-lang-attribute and only taken in count, when no such attribute can
* be found (i.e. just before the prompt).
* @access private
* @see {@link Hyphenator.config}
* @see {@link Hyphenator~autoSetMainLanguage}
*/
var defaultLanguage = "";
/**
* @member {ElementCollection} Hyphenator~elements
* @desc
* A class representing all elements (of type Element) that have to be hyphenated. This var is filled by
* {@link Hyphenator~gatherDocumentInfos}
* @access private
*/
var elements = (function () {
/**
* @constructor Hyphenator~elements~ElementCollection~Element
* @desc represents a DOM Element with additional information
* @access private
*/
var makeElement = function (element) {
return {
/**
* @member {Object} Hyphenator~elements~ElementCollection~Element~element
* @desc A DOM Element
* @access protected
*/
element: element,
/**
* @member {boolean} Hyphenator~elements~ElementCollection~Element~hyphenated
* @desc Marks if the element has been hyphenated
* @access protected
*/
hyphenated: false,
/**
* @member {boolean} Hyphenator~elements~ElementCollection~Element~treated
* @desc Marks if information of the element has been collected but not hyphenated (e.g. dohyphenation is off)
* @access protected
*/
treated: false
};
},
/**
* @constructor Hyphenator~elements~ElementCollection
* @desc A collection of Elements to be hyphenated
* @access protected
*/
makeElementCollection = function () {
/**
* @member {number} Hyphenator~elements~ElementCollection~counters
* @desc Array of [number of collected elements, number of hyphenated elements]
* @access protected
*/
var counters = [0, 0],
/**
* @member {Object.<string, Array.<Element>>} Hyphenator~elements~ElementCollection~list
* @desc The collection of elements, where the key is a language code and the value is an array of elements
* @access protected
*/
list = {},
/**
* @method Hyphenator~elements~ElementCollection.prototype~add
* @augments Hyphenator~elements~ElementCollection
* @access protected
* @desc adds a DOM element to the collection
* @param {Object} el - The DOM element
* @param {string} lang - The language of the element
*/
add = function (el, lang) {
var elo = makeElement(el);
if (!list.hasOwnProperty(lang)) {
list[lang] = [];
}
list[lang].push(elo);
counters[0] += 1;
return elo;
},
/**
* @callback Hyphenator~elements~ElementCollection.prototype~each~callback fn - The callback that is executed for each element
* @param {string} [k] The key (i.e. language) of the collection
* @param {Hyphenator~elements~ElementCollection~Element} element
*/
/**
* @method Hyphenator~elements~ElementCollection.prototype~each
* @augments Hyphenator~elements~ElementCollection
* @access protected
* @desc takes each element of the collection as an argument of fn
* @param {Hyphenator~elements~ElementCollection.prototype~each~callback} fn - A function that takes an element as an argument
*/
each = function (fn) {
forEachKey(list, function (k) {
if (fn.length === 2) {
fn(k, list[k]);
} else {
fn(list[k]);
}
});
};
return {
counters: counters,
list: list,
add: add,
each: each
};
};
return makeElementCollection();
}());
/**
* @member {Object.<sting, string>} Hyphenator~exceptions
* @desc
* An object containing exceptions as comma separated strings for each language.
* When the language-objects are loaded, their exceptions are processed, copied here and then deleted.
* Exceptions can also be set by the user.
* @see {@link Hyphenator~prepareLanguagesObj}
* @access private
*/
var exceptions = {};
/**
* @member {Object.<string, boolean>} Hyphenator~docLanguages
* @desc
* An object holding all languages used in the document. This is filled by
* {@link Hyphenator~gatherDocumentInfos}
* @access private
*/
var docLanguages = {};
/**
* @member {string} Hyphenator~url
* @desc
* A string containing a insane RegularExpression to match URL"s
* @access private
*/
var url = "(?:\\w*:\/\/)?(?:(?:\\w*:)?(?:\\w*)@)?(?:(?:(?:[\\d]{1,3}\\.){3}(?:[\\d]{1,3}))|(?:(?:www\\.|[a-zA-Z]\\.)?[a-zA-Z0-9\\-]+(?:\\.[a-z]{2,})+))(?::\\d*)?(?:\/[\\w#!:\\.?\\+=&%@!\\-]*)*";
// protocoll usr pwd ip or host tld port path
/**
* @member {string} Hyphenator~mail
* @desc
* A string containing a RegularExpression to match mail-adresses
* @access private
*/
var mail = "[\\w-\\.]+@[\\w\\.]+";
/**
* @member {string} Hyphenator~zeroWidthSpace
* @desc
* A string that holds a char.
* Depending on the browser, this is the zero with space or an empty string.
* zeroWidthSpace is used to break URLs
* @access private
*/
var zeroWidthSpace = (function () {
var zws, ua = window.navigator.userAgent.toLowerCase();
zws = String.fromCharCode(8203); //Unicode zero width space
if (ua.indexOf("msie 6") !== -1) {
zws = ""; //IE6 doesn"t support zws
}
if (ua.indexOf("opera") !== -1 && ua.indexOf("version/10.00") !== -1) {
zws = ""; //opera 10 on XP doesn"t support zws
}
return zws;
}());
/**
* @method Hyphenator~onBeforeWordHyphenation
* @desc
* This method is called just before a word is hyphenated.
* It is called with two parameters: the word and its language.
* The method must return a string (aka the word).
* @see {@link Hyphenator.config}
* @access private
* @param {string} word
* @param {string} lang
* @return {string} The word that goes into hyphenation
*/
var onBeforeWordHyphenation = function (word) {
return word;
};
/**
* @method Hyphenator~onAfterWordHyphenation
* @desc
* This method is called for each word after it is hyphenated.
* Takes the word as a first parameter and its language as a second parameter.
* Returns a string that will replace the word that has been hyphenated.
* @see {@link Hyphenator.config}
* @access private
* @param {string} word
* @param {string} lang
* @return {string} The word that goes into hyphenation
*/
var onAfterWordHyphenation = function (word) {
return word;
};
/**
* @method Hyphenator~onHyphenationDone
* @desc
* A method to be called, when the last element has been hyphenated.
* If there are frames the method is called for each frame.
* Therefore the location.href of the contextWindow calling this method is given as a parameter
* @see {@link Hyphenator.config}
* @param {string} context
* @access private
*/
var onHyphenationDone = function (context) {
return context;
};
/**
* @name Hyphenator~selectorFunction
* @desc
* A function set by the user that has to return a HTMLNodeList or array of Elements to be hyphenated.
* By default this is set to false so we can check if a selectorFunction is set…
* @see {@link Hyphenator.config}
* @see {@link Hyphenator~mySelectorFunction}
* @default false
* @type {function|boolean}
* @access private
*/
var selectorFunction = false;
/**
* @name Hyphenator~flattenNodeList
* @desc
* Takes a nodeList and returns an array with all elements that are not contained by another element in the nodeList
* By using this function the elements returned by selectElements can be "flattened".
* @see {@link Hyphenator~selectElements}
* @param {nodeList} nl
* @return {Array} Array of "parent"-elements
* @access private
*/
function flattenNodeList(nl) {
var parentElements = [],
i = 1,
j = 0,
isParent = true;
parentElements.push(nl[0]); //add the first item, since this is always an parent
while (i < nl.length) { //cycle through nodeList
while (j < parentElements.length) { //cycle through parentElements
if (parentElements[j].contains(nl[i])) {
isParent = false;
break;
}
j += 1;
}
if (isParent) {
parentElements.push(nl[i]);
}
isParent = true;
i += 1;
}
return parentElements;
}
/**
* @method Hyphenator~mySelectorFunction
* @desc
* A function that returns a HTMLNodeList or array of Elements to be hyphenated.
* By default it uses the classname ("hyphenate") to select the elements.
* @access private
*/
function mySelectorFunction(hyphenateClass) {
var tmp,
el = [],
i = 0;
if (window.document.getElementsByClassName) {
el = contextWindow.document.getElementsByClassName(hyphenateClass);
} else if (window.document.querySelectorAll) {
el = contextWindow.document.querySelectorAll("." + hyphenateClass);
} else {
tmp = contextWindow.document.getElementsByTagName("*");
while (i < tmp.length) {
if (tmp[i].className.indexOf(hyphenateClass) !== -1 && tmp[i].className.indexOf(dontHyphenateClass) === -1) {
el.push(tmp[i]);
}
i += 1;
}
}
return el;
}
/**
* @method Hyphenator~selectElements
* @desc
* A function that uses either selectorFunction set by the user
* or the default mySelectorFunction.
* @access private
*/
function selectElements() {
var elems;
if (selectorFunction) {
elems = selectorFunction();
} else {