X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fjquery%2Fjquery.highlightText.js;h=a14e3ebd3820c09c34d28784c281c3bce8a5f8a0;hb=4bd4d01360161625d76cf14209d6dbd4f8491634;hp=e37f19b04a67b3c2973dbe762b8d9c682fb33057;hpb=cb2896f90ebef77c02c9da4067b0e56e37471092;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/jquery/jquery.highlightText.js b/resources/src/jquery/jquery.highlightText.js index e37f19b04a..a14e3ebd38 100644 --- a/resources/src/jquery/jquery.highlightText.js +++ b/resources/src/jquery/jquery.highlightText.js @@ -8,18 +8,28 @@ $.highlightText = { // Split our pattern string at spaces and run our highlight function on the results - splitAndHighlight: function ( node, pat ) { + splitAndHighlight: function ( node, text ) { var i, - patArray = pat.split( ' ' ); - for ( i = 0; i < patArray.length; i++ ) { - if ( patArray[ i ].length === 0 ) { + words = text.split( ' ' ); + for ( i = 0; i < words.length; i++ ) { + if ( words[ i ].length === 0 ) { continue; } - $.highlightText.innerHighlight( node, patArray[ i ] ); + $.highlightText.innerHighlight( + node, + new RegExp( '(^|\\s)' + mw.RegExp.escape( words[ i ] ), 'i' ) + ); } return node; }, + prefixHighlight: function ( node, prefix ) { + $.highlightText.innerHighlight( + node, + new RegExp( '(^)' + mw.RegExp.escape( prefix ), 'i' ) + ); + }, + // scans a node looking for the pattern and wraps a span around each match innerHighlight: function ( node, pat ) { var i, match, pos, spannode, middlebit, middleclone; @@ -28,7 +38,7 @@ // non latin characters can make regex think a new word has begun: do not use \b // http://stackoverflow.com/questions/3787072/regex-wordwrap-with-utf8-characters-in-js // look for an occurrence of our pattern and store the starting position - match = node.data.match( new RegExp( '(^|\\s)' + mw.RegExp.escape( pat ), 'i' ) ); + match = node.data.match( pat ); if ( match ) { pos = match.index + match[ 1 ].length; // include length of any matched spaces // create the span wrapper for the matched text @@ -37,7 +47,7 @@ // shave off the characters preceding the matched text middlebit = node.splitText( pos ); // shave off any unmatched text off the end - middlebit.splitText( pat.length ); + middlebit.splitText( match[ 0 ].length - match[ 1 ].length ); // clone for appending to our span middleclone = middlebit.cloneNode( true ); // append the matched text node to the span @@ -45,12 +55,14 @@ // replace the matched node, with our span-wrapped clone of the matched node middlebit.parentNode.replaceChild( spannode, middlebit ); } - } else if ( node.nodeType === Node.ELEMENT_NODE + } else if ( + node.nodeType === Node.ELEMENT_NODE && // element with childnodes, and not a script, style or an element we created - && node.childNodes - && !/(script|style)/i.test( node.tagName ) - && !( node.tagName.toLowerCase() === 'span' - && node.className.match( /\bhighlight/ ) + node.childNodes && + !/(script|style)/i.test( node.tagName ) && + !( + node.tagName.toLowerCase() === 'span' && + node.className.match( /\bhighlight/ ) ) ) { for ( i = 0; i < node.childNodes.length; ++i ) { @@ -61,11 +73,24 @@ } }; - $.fn.highlightText = function ( matchString ) { + /** + * Highlight certain text in current nodes (by wrapping it in `...`). + * + * @param {string} matchString String to match + * @param {Object} [options] + * @param {string} [options.method='splitAndHighlight'] Method of matching to use, one of: + * - 'splitAndHighlight': Split `matchString` on spaces, then match each word separately. + * - 'prefixHighlight': Match `matchString` at the beginning of text only. + * @return {jQuery} + * @chainable + */ + $.fn.highlightText = function ( matchString, options ) { + options = options || {}; + options.method = options.method || 'splitAndHighlight'; return this.each( function () { var $el = $( this ); $el.data( 'highlightText', { originalText: $el.text() } ); - $.highlightText.splitAndHighlight( this, matchString ); + $.highlightText[ options.method ]( this, matchString ); } ); };