2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('highlight-accentfold', function (Y, NAME) {
11
Adds accent-folding highlighters to `Y.Highlight`.
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 `all()`.
34
@param {String} haystack String to apply highlighting to.
35
@param {String|String[]} needles String or array of strings that should be
37
@param {Object} [options] Options object.
38
@param {Boolean} [options.startsWith=false] If `true`, matches must be
39
anchored to the beginning of the string.
40
@return {String} Escaped and highlighted copy of _haystack_.
43
allFold: function (haystack, needles, options) {
44
var template = Highlight._TEMPLATE,
47
chunk, i, len, match, result;
50
// This tells Highlight.all() not to escape HTML, in order to ensure
51
// usable match offsets. The output of all() is discarded, and we
52
// perform our own escaping before returning the highlighted string.
55
// While the highlight regex operates on the accent-folded strings,
56
// this replacer will highlight the matched positions in the
59
// Note: this implementation doesn't handle multi-character folds,
60
// like "æ" -> "ae". Doing so correctly would be prohibitively
61
// expensive both in terms of code size and runtime performance, so
62
// I've chosen to take the pragmatic route and just not do it at
63
// all. This is one of many reasons why accent folding is best done
65
replacer: function (match, p1, foldedNeedle, pos) {
68
// Ignore matches inside HTML entities.
69
if (p1 && !(/\s/).test(foldedNeedle)) {
73
len = foldedNeedle.length;
76
haystack.substring(startPos, pos), // substring between previous match and this match
77
haystack.substr(pos, len) // match to be highlighted
82
}, options || EMPTY_OBJECT);
84
// Run the highlighter on the folded strings. We don't care about the
85
// output; our replacer function will build the canonical highlighted
86
// string, with original accented characters.
87
Highlight.all(AccentFold.fold(haystack), AccentFold.fold(needles), options);
89
// Tack on the remainder of the haystack that wasn't highlighted, if
91
if (startPos < haystack.length) {
92
results.push([haystack.substr(startPos)]);
95
// Highlight and escape the string.
96
for (i = 0, len = results.length; i < len; ++i) {
97
chunk = Escape.html(results[i][0]);
99
if ((match = results[i][1])) {
100
chunk += template.replace(/\{s\}/g, Escape.html(match));
106
return results.join('');
110
Accent-folding version of `start()`.
113
@param {String} haystack String to apply highlighting to.
114
@param {String|String[]} needles String or array of strings that should be
116
@return {String} Escaped and highlighted copy of _haystack_.
119
startFold: function (haystack, needles) {
120
return Highlight.allFold(haystack, needles, {startsWith: true});
124
Accent-folding version of `words()`.
127
@param {String} haystack String to apply highlighting to.
128
@param {String|String[]} needles String or array of strings containing words
129
that should be highlighted. If a string is passed, it will be split
130
into words; if an array is passed, it is assumed to have already been
132
@return {String} Escaped and highlighted copy of _haystack_.
135
wordsFold: function (haystack, needles) {
136
var template = Highlight._TEMPLATE;
138
return Highlight.words(haystack, AccentFold.fold(needles), {
139
mapper: function (word, needles) {
140
if (needles.hasOwnProperty(AccentFold.fold(word))) {
141
return template.replace(/\{s\}/g, Escape.html(word));
144
return Escape.html(word);
151
}, '3.10.3', {"requires": ["highlight-base", "text-accentfold"]});