r69564 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69563‎ | r69564 | r69565 >
Date:19:44, 19 July 2010
Author:adam
Status:ok
Tags:
Comment:
Follow up to r69165. Adding better comments and trimming out some unnecissary code
Modified paths:
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.highlightText.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.highlightText.js
@@ -16,32 +16,37 @@
1717 }
1818 return node;
1919 },
20 - // adapted from Johann Burkard's highlight plugin
21 - // http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
2220 // scans a node looking for the pattern and wraps a span around each match
2321 innerHighlight: function( node, pat ) {
24 - var skip = 0;
 22+ // if this is a text node
2523 if ( node.nodeType == 3 ) {
2624 // TODO - need to be smarter about the character matching here.
2725 // non latin characters can make regex think a new word has begun.
 26+ // look for an occurence of our pattern and store the starting position
2827 var pos = node.data.search( new RegExp( "\\b" + RegExp.escape( pat ), "i" ) );
2928 if ( pos >= 0 ) {
 29+ // create the span wrapper for the matched text
3030 var spannode = document.createElement( 'span' );
3131 spannode.className = 'highlight';
 32+ // shave off the characters preceding the matched text
3233 var middlebit = node.splitText( pos );
33 - var endbit = middlebit.splitText( pat.length );
 34+ // shave off any unmatched text off the end
 35+ middlebit.splitText( pat.length );
 36+ // clone for appending to our span
3437 var middleclone = middlebit.cloneNode( true );
 38+ // append the matched text node to the span
3539 spannode.appendChild( middleclone );
 40+ // replace the matched node, with our span-wrapped clone of the matched node
3641 middlebit.parentNode.replaceChild( spannode, middlebit );
37 - skip = 1;
3842 }
 43+ // if this is an element with childnodes, and not a script, style or an element we created
3944 } else if ( node.nodeType == 1 && node.childNodes && !/(script|style)/i.test( node.tagName )
4045 && !( node.tagName.toLowerCase() == 'span' && node.className.match( 'highlight' ) ) ) {
4146 for ( var i = 0; i < node.childNodes.length; ++i ) {
42 - i += $.highlightText.innerHighlight( node.childNodes[i], pat );
 47+ // call the highlight function for each child node
 48+ $.highlightText.innerHighlight( node.childNodes[i], pat );
4349 }
4450 }
45 - return skip;
4651 }
4752 };
4853

Follow-up revisions

RevisionCommit summaryAuthorDate
r69565recombining for r69564adam19:46, 19 July 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r69165Adding a new plugin to handle match highlighting better in simplesearch. Firs...adam23:26, 7 July 2010

Status & tagging log