/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html version: 3.3.0 build: 3167 */ YUI.add('highlight-base', function(Y) { /** * Provides methods for highlighting strings within other strings by wrapping * them in HTML. * * @module highlight * @since 3.3.0 */ /** *
* Provides methods for highlighting strings within other strings by wrapping * them in HTML. *
* *
* The highlight methods first escape any special HTML characters in the input
* strings and then highlight the appropriate substrings by wrapping them in a
* <b class="yui3-highlight"></b>
element. The
* <b>
element is used rather than
* <strong>
in accordance with HTML5's definition of
* <b>
as being purely presentational, which is exactly what
* highlighting is.
*
* Regular expression template for highlighting a match that occurs anywhere
* in a string. The placeholder %needles
will be replaced with
* a list of needles to match, joined by |
characters.
*
* This regex should have two capturing subpatterns: the first should match
* an unclosed HTML entity (e.g. "&" without a ";" at the end) 0 or 1
* times; the second should contain the %needles
placeholder.
* The first subpattern match is used to emulate a negative lookbehind
* assertion, in order to prevent highlighting inside HTML entities.
*
* Regular expression template for highlighting start-of-string matches
* (i.e., only matches that occur at the beginning of a string). The
* placeholder %needles
will be replaced with a list of needles
* to match, joined by |
characters.
*
* See _REGEX
for a description of the capturing subpatterns
* this regex should contain.
*
{s}
will be replaced with the
* matched substring.
*
* @property _TEMPLATE
* @type {String}
* @default '{s}'
* @protected
* @static
* @final
*/
_TEMPLATE: '{s}',
// -- Public Static Methods ------------------------------------------------
/**
* Highlights all occurrences in the haystack string of the items
* in the needles array, regardless of where they occur. The
* returned string will have all HTML characters escaped except for the
* highlighting markup.
*
* @method all
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @param {Object} options (optional) Options object, which may contain
* zero or more of the following properties:
*
* true
, matching will be case-sensitive. Default is
* false
.
* startsWith
is true
, matches
* must be anchored to the beginning of the string.
* all()
, but case-sensitive by default.
*
* @method allCase
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @param {Object} options (optional) Options object. See all()
* for details.
* @return {String} Escaped and highlighted copy of haystack.
* @static
*/
allCase: function (haystack, needles, options) {
return Highlight.all(haystack, needles,
Y.merge(options || EMPTY_OBJECT, {caseSensitive: true}));
},
/**
* Highlights needles that occur at the start of haystack.
* The returned string will have all HTML characters escaped except for the
* highlighting markup.
*
* @method start
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @param {Object} options (optional) Options object, which may contain
* zero or more of the following properties:
*
* true
, matching will be case-sensitive. Default is
* false
.
* start()
, but case-sensitive by default.
*
* @method startCase
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @return {String} Escaped and highlighted copy of haystack.
* @static
*/
startCase: function (haystack, needles) {
// No options passthru for now, since it would be redundant. If start()
// ever supports more options than caseSensitive, then we'll start
// passing the options through.
return Highlight.start(haystack, needles, {caseSensitive: true});
},
/**
* Highlights complete words in the haystack string that are also
* in the needles array. The returned string will have all HTML
* characters escaped except for the highlighting markup.
*
* @method words
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings containing words
* that should be highlighted. If a string is passed, it will be split
* into words; if an array is passed, it is assumed to have already been
* split.
* @param {Object} options (optional) Options object, which may contain
* zero or more of the following properties:
*
* true
, matching will be case-sensitive. Default is
* false
.
* words()
, but case-sensitive by default.
*
* @method wordsCase
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings containing words
* that should be highlighted. If a string is passed, it will be split
* into words; if an array is passed, it is assumed to have already been
* split.
* @return {String} Escaped and highlighted copy of haystack.
* @static
*/
wordsCase: function (haystack, needles) {
// No options passthru for now, since it would be redundant. If words()
// ever supports more options than caseSensitive, then we'll start
// passing the options through.
return Highlight.words(haystack, needles, {caseSensitive: true});
}
};
Y.Highlight = Highlight;
}, '3.3.0' ,{requires:['array-extras', 'escape', 'text-wordbreak']});
YUI.add('highlight-accentfold', function(Y) {
/**
* Adds accent-folding highlighters to Y.Highlight
.
*
* @module highlight
* @submodule highlight-accentfold
*/
/**
* @class Highlight
* @static
*/
var AccentFold = Y.Text.AccentFold,
Escape = Y.Escape,
EMPTY_OBJECT = {},
Highlight = Y.mix(Y.Highlight, {
// -- Public Static Methods ------------------------------------------------
/**
* Accent-folding version of all()
.
*
* @method allFold
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @param {Object} options (optional) Options object, which may contain
* zero or more of the following properties:
*
* startsWith
is true
, matches
* must be anchored to the beginning of the string.
* start()
.
*
* @method startFold
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings that should be
* highlighted.
* @return {String} Escaped and highlighted copy of haystack.
* @static
*/
startFold: function (haystack, needles) {
return Highlight.allFold(haystack, needles, {startsWith: true});
},
/**
* Accent-folding version of words()
.
*
* @method wordsFold
* @param {String} haystack String to apply highlighting to.
* @param {String|Array} needles String or array of strings containing words
* that should be highlighted. If a string is passed, it will be split
* into words; if an array is passed, it is assumed to have already been
* split.
* @return {String} Escaped and highlighted copy of haystack.
* @static
*/
wordsFold: function (haystack, needles) {
var template = Highlight._TEMPLATE;
return Highlight.words(haystack, AccentFold.fold(needles), {
mapper: function (word, needles) {
if (needles.hasOwnProperty(AccentFold.fold(word))) {
return template.replace(/\{s\}/g, Escape.html(word));
}
return Escape.html(word);
}
});
}
});
}, '3.3.0' ,{requires:['highlight-base', 'text-accentfold']});
YUI.add('highlight', function(Y){}, '3.3.0' ,{use:['highlight-base', 'highlight-accentfold']});