 1/*!
2 * jQuery replaceText - v1.1 - 11/21/2009
3 * http://benalman.com/projects/jquery-replacetext-plugin/
4 *
5 * Copyright (c) 2009 "Cowboy" Ben Alman
6 * Dual licensed under the MIT and GPL licenses.
7 * http://benalman.com/about/license/
8 */
9
10// Script: jQuery replaceText: String replace for your jQueries!
11//
12// *Version: 1.1, Last updated: 11/21/2009*
13//
14// Project Home - http://benalman.com/projects/jquery-replacetext-plugin/
15// GitHub - http://github.com/cowboy/jquery-replacetext/
16// Source - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.js
17// (Minified) - http://github.com/cowboy/jquery-replacetext/raw/master/jquery.ba-replacetext.min.js (0.5kb)
18//
19// About: License
20//
21// Copyright (c) 2009 "Cowboy" Ben Alman,
22// Dual licensed under the MIT and GPL licenses.
23// http://benalman.com/about/license/
24//
25// About: Examples
26//
27// This working example, complete with fully commented code, illustrates one way
28// in which this plugin can be used.
29//
30// replaceText - http://benalman.com/code/projects/jquery-replacetext/examples/replacetext/
31//
32// About: Support and Testing
33//
34// Information about what version or versions of jQuery this plugin has been
35// tested with, and what browsers it has been tested in.
36//
37// jQuery Versions - 1.3.2, 1.4.1
38// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.
39//
40// About: Release History
41//
42// 1.1 - (11/21/2009) Simplified the code and API substantially.
43// 1.0 - (11/21/2009) Initial release
44
45(function($){
46 '$:nomunge'; // Used by YUI compressor.
47
48 // Method: jQuery.fn.replaceText
49 //
50 // Replace text in specified elements. Note that only text content will be
51 // modified, leaving all tags and attributes untouched. The new text can be
52 // either text or HTML.
53 //
54 // Uses the String prototype replace method, full documentation on that method
55 // can be found here:
56 //
57 // https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/String/Replace
58 //
59 // Usage:
60 //
61 // > jQuery('selector').replaceText( search, replace [, text_only ] );
62 //
63 // Arguments:
64 //
65 // search - (RegExp|String) A RegExp object or substring to be replaced.
66 // Because the String prototype replace method is used internally, this
67 // argument should be specified accordingly.
68 // replace - (String|Function) The String that replaces the substring received
69 // from the search argument, or a function to be invoked to create the new
70 // substring. Because the String prototype replace method is used internally,
71 // this argument should be specified accordingly.
72 // text_only - (Boolean) If true, any HTML will be rendered as text. Defaults
73 // to false.
74 //
75 // Returns:
76 //
77 // (jQuery) The initial jQuery collection of elements.
78
79 $.fn.replaceText = function( search, replace, text_only ) {
80 return this.each(function(){
81 var node = this.firstChild,
82 val,
83 new_val,
84
85 // Elements to be removed at the end.
86 remove = [];
87
88 // Only continue if firstChild exists.
89 if ( node ) {
90
91 // Loop over all childNodes.
92 do {
93
94 // Only process text nodes.
95 if ( node.nodeType === 3 ) {
96
97 // The original node value.
98 val = node.nodeValue;
99
100 // The new value.
101 new_val = val.replace( search, replace );
102
103 // Only replace text if the new value is actually different!
104 if ( new_val !== val ) {
105
106 if ( !text_only && /</.test( new_val ) ) {
107 // The new value contains HTML, set it in a slower but far more
108 // robust way.
109 $(node).before( new_val );
110
111 // Don't remove the node yet, or the loop will lose its place.
112 remove.push( node );
113 } else {
114 // The new value contains no HTML, so it can be set in this
115 // very fast, simple way.
116 node.nodeValue = new_val;
117 }
118 }
119 }
120
121 } while ( node = node.nextSibling );
122 }
123
124 // Time to remove those elements!
125 remove.length && $(remove).remove();
126 });
127 };
128
129})(jQuery); 