2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('highlight-accentfold', function(Y) {
11 * Adds accent-folding highlighters to <code>Y.Highlight</code>.
14 * @submodule highlight-accentfold
22 var AccentFold = Y.Text.AccentFold,
27 Highlight = Y.mix(Y.Highlight, {
28 // -- Public Static Methods ------------------------------------------------
31 * Accent-folding version of <code>all()</code>.
34 * @param {String} haystack String to apply highlighting to.
35 * @param {String|Array} needles String or array of strings that should be
37 * @param {Object} options (optional) Options object, which may contain
38 * zero or more of the following properties:
41 * <dt>startsWith (Boolean)<dt>
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.
49 * @return {String} Escaped and highlighted copy of <em>haystack</em>.
52 allFold: function (haystack, needles, options) {
53 var template = Highlight._TEMPLATE,
58 // While the highlight regex operates on the accent-folded strings,
59 // this replacer will highlight the matched positions in the
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
68 replacer: function (match, p1, foldedNeedle, pos) {
71 // Ignore matches inside HTML entities.
72 if (p1 && !(/\s/).test(foldedNeedle)) {
76 len = foldedNeedle.length;
78 result.push(haystack.substring(startPos, pos) +
79 template.replace(/\{s\}/g, haystack.substr(pos, len)));
83 }, options || EMPTY_OBJECT);
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),
91 // Tack on the remainder of the haystack that wasn't highlighted, if
93 if (startPos < haystack.length - 1) {
94 result.push(haystack.substr(startPos));
97 return result.join('');
101 * Accent-folding version of <code>start()</code>.
104 * @param {String} haystack String to apply highlighting to.
105 * @param {String|Array} needles String or array of strings that should be
107 * @return {String} Escaped and highlighted copy of <em>haystack</em>.
110 startFold: function (haystack, needles) {
111 return Highlight.allFold(haystack, needles, {startsWith: true});
115 * Accent-folding version of <code>words()</code>.
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
123 * @return {String} Escaped and highlighted copy of <em>haystack</em>.
126 wordsFold: function (haystack, needles) {
127 var template = Highlight._TEMPLATE;
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));
135 return Escape.html(word);
142 }, '3.3.0' ,{requires:['highlight-base', 'text-accentfold']});