Implemented #756, making text(String) really useful
[jquery.git] / src / jquery / jquery.js
index 30fd03d..78d1ab9 100644 (file)
@@ -388,6 +388,8 @@ jQuery.fn = jQuery.prototype = {
        /**
         * Set a single property to a value, on all matched elements.
         *
+        * Can compute values provided as ${formula}, see second example.
+        *
         * Note that you can't set the name property of input elements in IE.
         * Use $(html) or .append(html) or .html(html) to create elements
         * on the fly including the name property.
@@ -397,12 +399,34 @@ jQuery.fn = jQuery.prototype = {
         * @result <img src="test.jpg"/>
         * @desc Sets src attribute to all images.
         *
+        * @example $("img").attr("title", "${this.src}");
+        * @before <img src="test.jpg" />
+        * @result <img src="test.jpg" title="test.jpg" />
+        * @desc Sets title attribute from src attribute, a shortcut for attr(String,Function)
+        *
         * @name attr
         * @type jQuery
         * @param String key The name of the property to set.
         * @param Object value The value to set the property to.
         * @cat DOM/Attributes
         */
+        
+       /**
+        * Set a single property to a computed value, on all matched elements.
+        *
+        * Instead of a value, a function is provided, that computes the value.
+        *
+        * @example $("img").attr("title", function() { return this.src });
+        * @before <img src="test.jpg" />
+        * @result <img src="test.jpg" title="test.jpg" />
+        * @desc Sets title attribute from src attribute.
+        *
+        * @name attr
+        * @type jQuery
+        * @param String key The name of the property to set.
+        * @param Function value A function returning the value to set.
+        * @cat DOM/Attributes
+        */
        attr: function( key, value, type ) {
                // Check to see if we're setting style values
                return typeof key != "string" || value != undefined ?
@@ -413,15 +437,17 @@ jQuery.fn = jQuery.prototype = {
                                        for ( var prop in key )
                                                jQuery.attr(
                                                        type ? this.style : this,
-                                                       prop, key[prop]
+                                                       prop, jQuery.parseSetter(key[prop])
                                                );
 
                                // See if we're setting a single key/value style
-                               else
+                               else {
+                                       // convert ${this.property} to function returnung that property
                                        jQuery.attr(
                                                type ? this.style : this,
-                                               key, value
+                                               key, jQuery.parseSetter(value)
                                        );
+                               }
                        }) :
 
                        // Look for the case where we're accessing a style value
@@ -500,10 +526,19 @@ jQuery.fn = jQuery.prototype = {
         */
 
        /**
-        * Set the text contents of all matched elements. This has the same
-        * effect as html().
+        * Set the text contents of all matched elements.
+        *
+        * Similar to html(), but  escapes HTML (replace "<" and ">" with their
+        * HTML entities.
         *
-        * @example $("p").text("Some new text.");
+        * If stripTags argument is set to true, HTML is stripped.
+        *
+        * @example $("p").text("<b>Some</b> new text.");
+        * @before <p>Test Paragraph.</p>
+        * @result <p>&lt;b&gt;Some&lt;/b&gt; new text.</p>
+        * @desc Sets the text of all paragraphs.
+        *
+        * @example $("p").text("<b>Some</b> new text.", true);
         * @before <p>Test Paragraph.</p>
         * @result <p>Some new text.</p>
         * @desc Sets the text of all paragraphs.
@@ -511,13 +546,12 @@ jQuery.fn = jQuery.prototype = {
         * @name text
         * @type String
         * @param String val The text value to set the contents of the element to.
+        * @param Boolean stripTags (optional) Wheather to strip or only escape tags
         * @cat DOM/Attributes
         */
-       text: function(e) {
-               // A surprisingly high number of people expect the
-               // .text() method to do this, so lets do it!
+       text: function(e, stripTags) {
                if ( typeof e == "string" )
-                       return this.html( e );
+                       return this.html( stripTags ? e.replace(/<\/?[^>]+>/gi, '') : e.replace(/</g, "&lt;").replace(/>/g, "&gt;") );
 
                e = e || this;
                var t = "";
@@ -742,7 +776,7 @@ jQuery.fn = jQuery.prototype = {
         * @example $("p").find("span").end();
         * @before <p><span>Hello</span>, how are you?</p>
         * @result [ <p>...</p> ]
-        * desc Selects all paragraphs, finds span elements inside these, and reverts the
+        * @desc Selects all paragraphs, finds span elements inside these, and reverts the
         * selection back to the paragraphs.
         *
         * @name end
@@ -1165,15 +1199,21 @@ jQuery.extend({
         * as $().each() - which is used to iterate, exclusively, over a jQuery
         * object. This function can be used to iterate over anything.
         *
-        * @example $.each( [0,1,2], function(i){
-        *   alert( "Item #" + i + ": " + this );
+        * The callback has two arguments:the key (objects) or index (arrays) as first
+        * the first, and the value as the second.
+        *
+        * @example $.each( [0,1,2], function(i, n){
+        *   alert( "Item #" + i + ": " + n );
         * });
-        * @desc Iterates over the items in an array, accessing both the current item and its index.
+        * @desc This is an example of iterating over the items in an array,
+        * accessing both the current item and its index.
         *
-        * @example $.each( { name: "John", lang: "JS" }, function(i){
-        *   alert( "Name: " + i + ", Value: " + this );
+        * @example $.each( { name: "John", lang: "JS" }, function(i, n){
+        *   alert( "Name: " + i + ", Value: " + n );
         * });
-        * @desc Iterates over the properties in an Object, accessing both the current item and its key.
+        *
+        * @desc This is an example of iterating over the properties in an
+        * Object, accessing both the current item and its key.
         *
         * @name $.each
         * @param Object obj The object, or array, to iterate over.
@@ -1357,6 +1397,16 @@ jQuery.extend({
                return r;
        },
        
+       parseSetter: function(value) {
+               if( typeof value == "string" && value.charAt(0) == "$" ) {
+                       var m = value.match(/{(.*)}$/);
+                       if ( m && m[1] ) {
+                               value = new Function( "return " + m[1] );
+                       }
+               }
+               return value;
+       },
+       
        attr: function(elem, name, value){
                var fix = {
                        "for": "htmlFor",
@@ -1372,6 +1422,11 @@ jQuery.extend({
                        selected: "selected"
                };
                
+               // get value if a function is provided
+               if ( value && typeof value == "function" ) {
+                       value = value.apply( elem );
+               }
+               
                // IE actually uses filters for opacity ... elem is actually elem.style
                if ( name == "opacity" && jQuery.browser.msie && value != undefined ) {
                        // IE has trouble with opacity if it does not have layout
@@ -1573,7 +1628,7 @@ jQuery.extend({
                        }
                }
 
-               var r = [ result[0] ];
+               var r = result.length ? [ result[0] ] : [];
 
                check: for ( var i = 1, rl = result.length; i < rl; i++ ) {
                        for ( var j = 0; j < i; j++ )
@@ -2106,4 +2161,4 @@ jQuery.each( [ "eq", "lt", "gt", "contains" ], function(i,n){
        jQuery.fn[ n ] = function(num,fn) {
                return this.filter( ":" + n + "(" + num + ")", fn );
        };
-});
\ No newline at end of file
+});