Skip to content

Commit

Permalink
Added each2 and use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
FWeinb committed May 10, 2012
1 parent fd7166b commit 8e6fd12
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -3,6 +3,8 @@ jQuery Highlight

Find some text in the DOM. The difference between this and [other](http://www.gotoquiz.com/web-coding/programming/javascript/highlight-words-in-text-with-jquery/) [Plugins](http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html) is that it looks for text in multiple nodes. It uses [DOMRanges](http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html) and therefor it doesn't works in IE < 10.

Add [jquery.ba-each2.js](http://benalman.com/projects/jquery-misc-plugins/#each2) to improve the speed.

## Usage ##

You can find some examples [here](http://fweinb.github.com/jqueryhighlight/).
Expand Down
38 changes: 38 additions & 0 deletions jquery.ba-each2.js
@@ -0,0 +1,38 @@
/*!
* jQuery each2 - v0.2 - 8/02/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Inspired by James Padolsey's quickEach
* http://gist.github.com/500145
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/

(function($) {

// Create a placeholder jQuery object with a length of 1. The single item
// is completely arbitrary and will be replaced.
var jq = $([1]);

$.fn.each2 = function( fn ) {
var i = -1;

while (
// Set both the first element AND context property of the placeholder
// jQuery object to the DOM element. When i has been incremented past the
// end, this[++i] will return undefined and abort the while loop.
( jq.context = jq[0] = this[++i] )

// Invoke the callback function in the context of the DOM element,
// passing both the index and the placeholder jQuery object in. Like
// .each, if the callback returns `false`, abort the while loop.
&& fn.call( jq[0], i, jq ) !== false
) {}

// Return the initial jQuery object for chainability.
return this;
};

})(jQuery);
12 changes: 12 additions & 0 deletions jquery.ba-each2.min.js
@@ -0,0 +1,12 @@
/*
* jQuery each2 - v0.2 - 8/02/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Inspired by James Padolsey's quickEach
* http://gist.github.com/500145
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function(a){var b=a([1]);a.fn.each2=function(d){var c=-1;while((b.context=b[0]=this[++c])&&d.call(b[0],c,b)!==false){}return this}})(jQuery);
12 changes: 9 additions & 3 deletions jquery.highlight.js
@@ -1,3 +1,9 @@
/*!
* jQuery jquery.highlight - v0.3 - 10/05/2012
* requires jquery.ba-each2.js
*
* Copyright (c) 2012 Fabrice Weinberg
*/
(function ($) {
$.fn.extend({
highlight : function (searchStrings, options) {
Expand Down Expand Up @@ -30,7 +36,7 @@
});
},
startSearch : function ($node, callback) {
$node.contents().each(function () {
$node.contents().each2(function (i, jq) {
if (core.found && options.onlyFirst) {return false; }
if (this.nodeType === 3) {
var nodeText = this.textContent,
Expand Down Expand Up @@ -63,14 +69,14 @@
}
}
} else if (!options.ignoredTags.test(this.tagName) && !((options.ignorePrevFounds === true) && typeof(this.className) === 'string' && ~this.className.indexOf(options.className))) {
core.startSearch($(this), callback);
core.startSearch(jq, callback);
}
});
},
surroundContent : function (range) {
var cont = range.extractContents(),
$cont = $(cont).contents(),
className = options.className + " " + options.classCountPrefix + core.count,
className = options.className + ' ' + options.classCountPrefix + core.count,
newEl;

$cont.each(
Expand Down

0 comments on commit 8e6fd12

Please sign in to comment.