]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/highlight/highlight-accentfold.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / highlight / highlight-accentfold.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('highlight-accentfold', function(Y) {
9
10 /**
11  * Adds accent-folding highlighters to <code>Y.Highlight</code>.
12  *
13  * @module highlight
14  * @submodule highlight-accentfold
15  */
16
17 /**
18  * @class Highlight
19  * @static
20  */
21
22 var AccentFold = Y.Text.AccentFold,
23     Escape     = Y.Escape,
24
25     EMPTY_OBJECT = {},
26
27 Highlight = Y.mix(Y.Highlight, {
28     // -- Public Static Methods ------------------------------------------------
29
30     /**
31      * Accent-folding version of <code>all()</code>.
32      *
33      * @method allFold
34      * @param {String} haystack String to apply highlighting to.
35      * @param {String|Array} needles String or array of strings that should be
36      *   highlighted.
37      * @param {Object} options (optional) Options object, which may contain
38      *   zero or more of the following properties:
39      *
40      * <dl>
41      *   <dt>startsWith (Boolean)<dt>
42      *   <dd>
43      *     By default, needles are highlighted wherever they appear in the
44      *     haystack. If <code>startsWith</code> is <code>true</code>, matches
45      *     must be anchored to the beginning of the string.
46      *   </dd>
47      * </dl>
48      *
49      * @return {String} Escaped and highlighted copy of <em>haystack</em>.
50      * @static
51      */
52     allFold: function (haystack, needles, options) {
53         var template = Highlight._TEMPLATE,
54             result   = [],
55             startPos = 0;
56
57         options = Y.merge({
58             // While the highlight regex operates on the accent-folded strings,
59             // this replacer will highlight the matched positions in the
60             // original string.
61             //
62             // Note: this implementation doesn't handle multi-character folds,
63             // like "æ" -> "ae". Doing so correctly would be prohibitively
64             // expensive both in terms of code size and runtime performance, so
65             // I've chosen to take the pragmatic route and just not do it at
66             // all. This is one of many reasons why accent folding is best done
67             // on the server.
68             replacer: function (match, p1, foldedNeedle, pos) {
69                 var len;
70
71                 // Ignore matches inside HTML entities.
72                 if (p1 && !(/\s/).test(foldedNeedle)) {
73                     return match;
74                 }
75
76                 len = foldedNeedle.length;
77
78                 result.push(haystack.substring(startPos, pos) +
79                         template.replace(/\{s\}/g, haystack.substr(pos, len)));
80
81                 startPos = pos + len;
82             }
83         }, options || EMPTY_OBJECT);
84
85         // Run the highlighter on the folded strings. We don't care about the
86         // output; our replacer function will build the canonical highlighted
87         // string, with original accented characters.
88         Highlight.all(AccentFold.fold(haystack), AccentFold.fold(needles),
89                 options);
90
91         // Tack on the remainder of the haystack that wasn't highlighted, if
92         // any.
93         if (startPos < haystack.length - 1) {
94             result.push(haystack.substr(startPos));
95         }
96
97         return result.join('');
98     },
99
100     /**
101      * Accent-folding version of <code>start()</code>.
102      *
103      * @method startFold
104      * @param {String} haystack String to apply highlighting to.
105      * @param {String|Array} needles String or array of strings that should be
106      *   highlighted.
107      * @return {String} Escaped and highlighted copy of <em>haystack</em>.
108      * @static
109      */
110     startFold: function (haystack, needles) {
111         return Highlight.allFold(haystack, needles, {startsWith: true});
112     },
113
114     /**
115      * Accent-folding version of <code>words()</code>.
116      *
117      * @method wordsFold
118      * @param {String} haystack String to apply highlighting to.
119      * @param {String|Array} needles String or array of strings containing words
120      *   that should be highlighted. If a string is passed, it will be split
121      *   into words; if an array is passed, it is assumed to have already been
122      *   split.
123      * @return {String} Escaped and highlighted copy of <em>haystack</em>.
124      * @static
125      */
126     wordsFold: function (haystack, needles) {
127         var template = Highlight._TEMPLATE;
128
129         return Highlight.words(haystack, AccentFold.fold(needles), {
130             mapper: function (word, needles) {
131                 if (needles.hasOwnProperty(AccentFold.fold(word))) {
132                     return template.replace(/\{s\}/g, Escape.html(word));
133                 }
134
135                 return Escape.html(word);
136             }
137         });
138     }
139 });
140
141
142 }, '3.3.0' ,{requires:['highlight-base', 'text-accentfold']});