/**************** Preamble ****************/ // This script is licensed under the creative commons license // Attribution-Share Alike 2.5 Switzerland // You are free to share and to remix this work under the // following conditions: Attribution and Share-Alike // See http://creativecommons.org/licenses/by-sa/2.5/ch/deed.en // Thus you are free to use it for commercial purposes. // // Dieses Script steht unter der Creative Commons-Lizenz // Attribution-Share Alike 2.5 Switzerland // Sie dürfen das Werk vervielfältigen, verbreiten und // öffentlich zugänglich machen, // sowie Bearbeitungen des Werkes anfertigen // Zu den folgenden Bedingungen: // Namensnennung und Weitergabe unter gleichen Bedingungen. // Siehe http://creativecommons.org/licenses/by-sa/2.5/ch/ // Somit sind Sie frei, das Script für kommerzielle Zwecke zu nutzen. // // Mathias Nater, Zürich, 2009 // mathias at mnn dot ch // // Comments are jsdoctoolkit formatted. See jsdoctoolkit.org /**************** Preamble ****************/ /** * @fileOverview * A script that does hyphenation in (X)HTML files * @author Mathias Nater, mathias@mnn.ch * @version Beta12 */ /** * @description Provides all functionality to do hyphenation * @namespace Holds all methods and properties * @constructor * @example * <script src = "Hyphenator.js" type = "text/javascript"></script>  * <script type = "text/javascript">  *   Hyphenator.run();  * </script> */ var Hyphenator = function () { /** * @name Hyphenator-SUPPORTEDLANG * @fieldOf Hyphenator * @description * A key-value object that stores supported languages. * If you add hyphenation patterns change this object. * @type object * @private * @example * Check if language lang is supported: * if (SUPPORTEDLANG[lang]) */ var SUPPORTEDLANG = {'de': true, 'en': true, 'es': true, 'fr': true, 'nl': true, 'ml': true, 'hi': true, 'bn': true, 'gu': true, 'ta': true, 'ka': true, 'te': true, 'or': true, 'pa': true, 'sv': true, 'it': true}; //you may delete languages that you won't use (for better performance) /** * @name Hyphenator-LANGUAGEHINT * @fieldOf Hyphenator * @description * A string to be displayed in a prompt if the language can't be guessed. * If you add hyphenation patterns change this string. * @type string * @private * @see Hyphenator-autoSetMainLanguage */ var LANGUAGEHINT = 'Deutsch: de\tEnglish: en\tEspa%F1ol: es\tFran%E7ais: fr\tNederlands: nl\tSvenska: sv\tMalayalam: ml\tHindi: hi\tBengali: bn\tGujarati : gu\tTamil: ta\tOriya: or\tPanjabi: pa\tTelugu: te\tKannada: kn\tItaliano: it'; /** * @name Hyphenator-PROMPTERSTRINGS * @fieldOf Hyphenator * @description * A key-value object holding the strings to be displayed if the language can't be guessed * If you add hyphenation patterns change this string. * @type object * @private * @see Hyphenator-autoSetMainLanguage */ var PROMPTERSTRINGS = {'de': 'Die Sprache dieser Webseite konnte nicht automatisch bestimmt werden. Bitte Sprache angeben: \n\n' + LANGUAGEHINT, 'en': 'The language of this website could not be determined automatically. Please indicate main language: \n\n' + LANGUAGEHINT, 'es': 'El idioma del sitio no pudo determinarse autom%E1ticamente. Por favor, indique el idioma principal: \n\n'+LANGUAGEHINT, 'fr': 'La langue de ce site n%u2019a pas pu %EAtre d%E9termin%E9e automatiquement. Veuillez indiquer une langue%A0: \n\n' + LANGUAGEHINT, 'nl': 'De taal van deze website kan niet automatisch worden bepaald. Geef de hoofdtaal op: \n\n' + LANGUAGEHINT, 'sv': 'Spr%E5ket p%E5 den h%E4r webbplatsen kunde inte avg%F6ras automatiskt. V%E4nligen ange: \n\n' + LANGUAGEHINT, 'ml': 'ഈ വെബ്‌സൈറ്റിന്റെ ഭാഷ കണ്ടുപിടിയ്ക്കാന്‍ കഴിഞ്ഞില്ല. ഭാഷ ഏതാണെന്നു തിരഞ്ഞെടുക്കുക: \n\n' + LANGUAGEHINT, 'it': 'Lingua del sito sconosciuta. Indicare una lingua, per favore: \n\n' + LANGUAGEHINT}; /** * @name Hyphenator-BASEPATH * @fieldOf Hyphenator * @description * A string storing the basepath from where Hyphenator.js was loaded. * This is used to load the patternfiles. * The basepath is determined dynamically ba searching all script-tags for Hyphenator.js * If the path cannot be determined http://hyphenator.googlecode.com/svn/trunk/ is used as fallback. * @type string * @private * @see Hyphenator-loadPatterns */ var BASEPATH = function () { var s = document.getElementsByTagName('script'), i = 0, p, t; while (!!(t = s[i++].src)) { p = t.indexOf('Hyphenator.js'); if (p !== -1) { return t.substring(0, p); } } return 'http://hyphenator.googlecode.com/svn/trunk/'; }(); /** * @name Hyphenator-DONTHYPHENATE * @fieldOf Hyphenator * @description * A key-value object containing all html-text their content sould not be hyphenated * @type object * @private * @see Hyphenator.hyphenateElement */ var DONTHYPHENATE = {'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}; /** * @name Hyphenator-exceptions * @fieldOf Hyphenator * @description * A key-value object containing manually added exceptions * @type object * @private * @see Hyphenator.addExceptions * @see Hyphenator.hyphenateWord */ var exceptions = {}; /** * @name Hyphenator-enableCache * @fieldOf Hyphenator * @description * A variable to set if caching is enabled or not * @type boolean * @default true * @private * @see Hyphenator.setEnableCache * @see Hyphenator.hyphenateWord */ var enableCache = true; /** * @name Hyphenator-cache * @fieldOf Hyphenator * @description * A key-value object containing key-value objects of already hyphenated words for each language * @type object * @private * @see Hyphenator.hyphenateWord */ var cache = function () { if (!enableCache) { return undefined; } var r = {}, l; for (l in SUPPORTEDLANG) { if (SUPPORTEDLANG.hasOwnProperty(l)) { r[l] = {}; } } return r; }(); /** * @name Hyphenator-enableRemoteLoading * @fieldOf Hyphenator * @description * A variable to set if pattern files should be loaded remotely or not * @type boolean * @default true * @private * @see Hyphenator.setRemoteLoading * @see Hyphenator-loadPatterns */ var enableRemoteLoading = true; /** * @name Hyphenator-displayToggleBox * @fieldOf Hyphenator * @description * A variable to set if the togglebox should be displayed or not * @type boolean * @default false * @private * @see Hyphenator.setDisplayToggleBox * @see Hyphenator-switchToggleBox */ var displayToggleBox = false; /** * @name Hyphenator-hyphenateclass * @fieldOf Hyphenator * @description * A string containing the css-class-name for the hyphenate class * @type string * @default 'hyphenate' * @private * @see Hyphenator.setClassName * @see Hyphenator-switchToggleBox */ var hyphenateclass = 'hyphenate'; // the CSS-Classname of Elements that should be hyphenated eg.

Text

/** * @name Hyphenator-hyphen * @fieldOf Hyphenator * @description * A string containing the character for in-word-hyphenation * @type string * @default the soft hyphen * @private * @see Hyphenator.setHyphenChar */ var hyphen = String.fromCharCode(173); /** * @name Hyphenator-urlhyphen * @fieldOf Hyphenator * @description * A string containing the character for url/mail-hyphenation * @type string * @default the zero width space * @private * @see Hyphenator.setUrlHyphenChar * @see Hyphenator-createZeroWidthSpace */ var urlhyphen = ''; /** * @name Hyphenator-min * @fieldOf Hyphenator * @description * A number wich indicates the minimal length of words to hyphenate. * @type number * @default 6 * @private */ var min = 6; /** * @name Hyphenator-bookmarklet * @fieldOf Hyphenator * @description * Indicates if Hyphanetor runs as bookmarklet or not. * @type boolean * @default false * @private */ var bookmarklet = false; /** * @name Hyphenator-patternsloaded * @fieldOf Hyphenator * @description * An object of key-value pairs, where key is the language and value a boolean. * Members are set by the external pattern files to indicate that they finished loading. * @type object * @private */ var patternsloaded = {}; // this is set when the patterns are loaded /** * @name Hyphenator-preparestate * @fieldOf Hyphenator * @description * A number that inidcates the current state of the scripts pattern-loading routine * 0: not initialized * 1: loading patterns * 2: ready * @type number * @private */ var preparestate = 0; //0: not initialized, 1: loading patterns, 2: ready /** * @name Hyphenator-mainlanguage * @fieldOf Hyphenator * @description * The general language of the document * @type number * @private * @see Hyphenator-_setMainLanguage */ var mainlanguage = null; /** * @name Hyphenator-url * @fieldOf Hyphenator * @description * A string containing a RegularExpression to match URL's * @type string * @private */ var url = '(\\w*:\/\/)((\\w*:)?(\\w*)@)?([\\w\\.]*)?(:\\d*)?(\/[\\w#!:\\.?\\+=&%@!\\-]*)*'; /** * @name Hyphenator-mail * @fieldOf Hyphenator * @description * A string containing a RegularExpression to match mail-adresses * @type string * @private */ var mail = '[\\w-\\.]+@[\\w\\.]+'; /** * @name Hyphenator-urlRE * @fieldOf Hyphenator * @description * A RegularExpressions-Object for url-matching * @type object * @private */ var urlRE = new RegExp(url, 'i'); /** * @name Hyphenator-mailRE * @fieldOf Hyphenator * @description * A RegularExpressions-Object for mail-adress-matching * @type object * @private */ var mailRE = new RegExp(mail, 'i'); /** * @name Hyphenator-zerowidthspace * @fieldOf Hyphenator * @description * A string that holds a char. * Depending on the browser, this is the zero with space or an empty string. * The zerowidthspace is inserted after a '-' in compound words, so even FF and IE * will break after a '-' if necessary. * zerowidthspace is also used to break URLs * @see Hyphenator-createZeroWidthSpace * @type string * @private */ var zerowidthspace = ''; /** * @name Hyphenator-createZeroWidthSpace * @methodOf Hyphenator * @description * Sets the variable {@link Hyphenator-zerowidthspace} depending on the browser version * MSIE <= 6 and 8b2 does NOT support zerowidthspace * @private * @returns string A string containing zerowidthspace or an empty string. */ function createZeroWidthSpace() { var ua = navigator.userAgent.toLowerCase(); if (ua.indexOf('msie 6') === -1 && ua.indexOf('msie 8') === -1) { zerowidthspace = String.fromCharCode(8203); //Unicode zero width space } else { zerowidthspace = ''; } return zerowidthspace; } /** * @name Hyphenator-checkIfBookmarklet * @methodOf Hyphenator * @description * Checks if Hyphenator is run as bookmarklet. * When using the bookmarklet there's a query-string appended to the url. * The function sets {@link Hyphenator-bookmarklet} * @private */ function checkIfBookmarklet() { var loc = null; var jsArray = document.getElementsByTagName('script'); for (var i = 0, l = jsArray.length; i < l; i++) { if (!!jsArray[i].getAttribute('src')) { loc = jsArray[i].getAttribute('src'); } if (!loc) { continue; } else if (loc.indexOf('Hyphenator.js?bm=true') !== -1) { bookmarklet = true; } } } /** * @name Hyphenator-getLang * @methodOf Hyphenator * @description * Gets the language of an element. If no language is set, it may use the {@link Hyphenator-mainlanguage}. * @param object The first parameter is an DOM-Element-Object * @param boolean The second parameter is a boolean to tell if the function should return the {@link Hyphenator-mainlanguage} * if there's no language found for the element. * @private */ function getLang(el, nofallback) { if (!!el.getAttribute('lang')) { return el.getAttribute('lang').substring(0, 2); } // The following doesn't work in IE due to a bug when getAttribute('xml:lang') in a table /*if (!!el.getAttribute('xml:lang')) { return el.getAttribute('xml:lang').substring(0, 2); }*/ //instead, we have to do this (thanks to borgzor): try { if (!!el.getAttribute('xml:lang')) { return el.getAttribute('xml:lang').substring(0, 2); } } catch (ex) {} if (el.tagName != 'HTML' && nofallback) { return getLang(el.parentNode); } if (!nofallback && mainlanguage) { return mainlanguage; } return null; } /** * @name Hyphenator-autoSetMainLanguage * @methodOf Hyphenator * @description * Retrieves the language of the document from the DOM. * The function looks in the following places: *