diff options
Diffstat (limited to 'resources/src/jquery/jquery.highlightText.js')
-rw-r--r-- | resources/src/jquery/jquery.highlightText.js | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/resources/src/jquery/jquery.highlightText.js b/resources/src/jquery/jquery.highlightText.js index 13382182..e37f19b0 100644 --- a/resources/src/jquery/jquery.highlightText.js +++ b/resources/src/jquery/jquery.highlightText.js @@ -3,7 +3,7 @@ * TODO: Add a function for restoring the previous text. * TODO: Accept mappings for converting shortcuts like WP: to Wikipedia:. */ -( function ( $ ) { +( function ( $, mw ) { $.highlightText = { @@ -12,10 +12,10 @@ var i, patArray = pat.split( ' ' ); for ( i = 0; i < patArray.length; i++ ) { - if ( patArray[i].length === 0 ) { + if ( patArray[ i ].length === 0 ) { continue; } - $.highlightText.innerHighlight( node, patArray[i] ); + $.highlightText.innerHighlight( node, patArray[ i ] ); } return node; }, @@ -23,15 +23,14 @@ // 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; - // if this is a text node - if ( node.nodeType === 3 ) { + if ( node.nodeType === Node.TEXT_NODE ) { // TODO - need to be smarter about the character matching here. // 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)' + $.escapeRE( pat ), 'i' ) ); + match = node.data.match( new RegExp( '(^|\\s)' + mw.RegExp.escape( pat ), 'i' ) ); if ( match ) { - pos = match.index + match[1].length; // include length of any matched spaces + pos = match.index + match[ 1 ].length; // include length of any matched spaces // create the span wrapper for the matched text spannode = document.createElement( 'span' ); spannode.className = 'highlight'; @@ -46,8 +45,8 @@ // replace the matched node, with our span-wrapped clone of the matched node middlebit.parentNode.replaceChild( spannode, middlebit ); } - // if this is an element with childnodes, and not a script, style or an element we created - } else if ( node.nodeType === 1 + } 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' @@ -56,7 +55,7 @@ ) { for ( i = 0; i < node.childNodes.length; ++i ) { // call the highlight function for each child node - $.highlightText.innerHighlight( node.childNodes[i], pat ); + $.highlightText.innerHighlight( node.childNodes[ i ], pat ); } } } @@ -70,4 +69,4 @@ } ); }; -}( jQuery ) ); +}( jQuery, mediaWiki ) ); |