Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.highlightText.js |
— | — | @@ -16,32 +16,37 @@ |
17 | 17 | } |
18 | 18 | return node; |
19 | 19 | }, |
20 | | - // adapted from Johann Burkard's highlight plugin |
21 | | - // http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html |
22 | 20 | // scans a node looking for the pattern and wraps a span around each match |
23 | 21 | innerHighlight: function( node, pat ) { |
24 | | - var skip = 0; |
| 22 | + // if this is a text node |
25 | 23 | if ( node.nodeType == 3 ) { |
26 | 24 | // TODO - need to be smarter about the character matching here. |
27 | 25 | // 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 |
28 | 27 | var pos = node.data.search( new RegExp( "\\b" + RegExp.escape( pat ), "i" ) ); |
29 | 28 | if ( pos >= 0 ) { |
| 29 | + // create the span wrapper for the matched text |
30 | 30 | var spannode = document.createElement( 'span' ); |
31 | 31 | spannode.className = 'highlight'; |
| 32 | + // shave off the characters preceding the matched text |
32 | 33 | 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 |
34 | 37 | var middleclone = middlebit.cloneNode( true ); |
| 38 | + // append the matched text node to the span |
35 | 39 | spannode.appendChild( middleclone ); |
| 40 | + // replace the matched node, with our span-wrapped clone of the matched node |
36 | 41 | middlebit.parentNode.replaceChild( spannode, middlebit ); |
37 | | - skip = 1; |
38 | 42 | } |
| 43 | + // if this is an element with childnodes, and not a script, style or an element we created |
39 | 44 | } else if ( node.nodeType == 1 && node.childNodes && !/(script|style)/i.test( node.tagName ) |
40 | 45 | && !( node.tagName.toLowerCase() == 'span' && node.className.match( 'highlight' ) ) ) { |
41 | 46 | 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 ); |
43 | 49 | } |
44 | 50 | } |
45 | | - return skip; |
46 | 51 | } |
47 | 52 | }; |
48 | 53 | |