discourse/vendor/assets/javascripts/jquery.ba-replacetext.js

130 lines
4.1 KiB
JavaScript
Raw Normal View History

2013-02-05 14:16:51 -05:00
/*!
* jQuery replaceText - v1.1 - 11/21/2009
* http://benalman.com/projects/jquery-replacetext-plugin/
2013-02-25 11:42:20 -05:00
*
2013-02-05 14:16:51 -05:00
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// Script: jQuery replaceText: String replace for your jQueries!
//
// *Version: 1.1, Last updated: 11/21/2009*
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Project Home - http://benalman.com/projects/jquery-replacetext-plugin/
// GitHub - http://github.com/cowboy/jquery-replacetext/
// Source - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.js
// (Minified) - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.min.js (0.5kb)
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// About: License
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Copyright (c) 2009 "Cowboy" Ben Alman,
// Dual licensed under the MIT and GPL licenses.
// http://benalman.com/about/license/
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// About: Examples
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// This working example, complete with fully commented code, illustrates one way
// in which this plugin can be used.
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// replaceText - http://benalman.com/code/projects/jquery-replacetext/examples/replacetext/
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// About: Support and Testing
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Information about what version or versions of jQuery this plugin has been
// tested with, and what browsers it has been tested in.
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// jQuery Versions - 1.3.2, 1.4.1
// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// About: Release History
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// 1.1 - (11/21/2009) Simplified the code and API substantially.
// 1.0 - (11/21/2009) Initial release
(function($){
'$:nomunge'; // Used by YUI compressor.
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Method: jQuery.fn.replaceText
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Replace text in specified elements. Note that only text content will be
// modified, leaving all tags and attributes untouched. The new text can be
// either text or HTML.
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Uses the String prototype replace method, full documentation on that method
2013-02-25 11:42:20 -05:00
// can be found here:
//
2013-02-05 14:16:51 -05:00
// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Usage:
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// > jQuery('selector').replaceText( search, replace [, text_only ] );
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Arguments:
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// search - (RegExp|String) A RegExp object or substring to be replaced.
// Because the String prototype replace method is used internally, this
// argument should be specified accordingly.
// replace - (String|Function) The String that replaces the substring received
// from the search argument, or a function to be invoked to create the new
// substring. Because the String prototype replace method is used internally,
// this argument should be specified accordingly.
// text_only - (Boolean) If true, any HTML will be rendered as text. Defaults
// to false.
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// Returns:
2013-02-25 11:42:20 -05:00
//
2013-02-05 14:16:51 -05:00
// (jQuery) The initial jQuery collection of elements.
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
$.fn.replaceText = function( search, replace, text_only ) {
return this.each(function(){
var node = this.firstChild,
val,
new_val,
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Elements to be removed at the end.
remove = [];
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Only continue if firstChild exists.
if ( node ) {
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Loop over all childNodes.
do {
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Only process text nodes.
if ( node.nodeType === 3 ) {
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// The original node value.
val = node.nodeValue;
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// The new value.
new_val = val.replace( search, replace );
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Only replace text if the new value is actually different!
if ( new_val !== val ) {
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
if ( !text_only && /</.test( new_val ) ) {
// The new value contains HTML, set it in a slower but far more
// robust way.
$(node).before( new_val );
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Don't remove the node yet, or the loop will lose its place.
remove.push( node );
} else {
// The new value contains no HTML, so it can be set in this
// very fast, simple way.
node.nodeValue = new_val;
}
}
}
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
} while ( node = node.nextSibling );
}
2013-02-25 11:42:20 -05:00
2013-02-05 14:16:51 -05:00
// Time to remove those elements!
remove.length && $(remove).remove();
});
2013-02-25 11:42:20 -05:00
};
2013-02-05 14:16:51 -05:00
})(jQuery);