aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJörn Zaefferer <joern.zaefferer@gmail.com>2007-01-07 23:59:13 +0000
committerJörn Zaefferer <joern.zaefferer@gmail.com>2007-01-07 23:59:13 +0000
commit5acecf6e2814701f9e22f91f17fcb33ef910e88a (patch)
treea0da881e448ad505c45b42f3751bb9d02d24bfbb
parentc78b1fb9a1f44c640290c1ffdb16b0c1d5531b7b (diff)
downloadjquery-5acecf6e2814701f9e22f91f17fcb33ef910e88a.tar.gz
jquery-5acecf6e2814701f9e22f91f17fcb33ef910e88a.zip
Implemented #756, making text(String) really useful
-rw-r--r--ChangeLog.txt1
-rw-r--r--src/jquery/coreTest.js4
-rw-r--r--src/jquery/jquery.js22
3 files changed, 20 insertions, 7 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt
index 0d3bfd5d0..c91bf667a 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -27,6 +27,7 @@ New and Noteworthy
- Improved docs for append, prepend, before and after, merging the three pairs into one
- Improved show/hide animations to show only hidden and hide only visible elements
- Added attr(String,Function) to calculate the value and support for attr("key", "${formula}") syntax, a shortcut for the former
+ - text(String) now escapes HTML per default and optionally stris tags completely
1.0.4
-----
diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js
index 436dcc5cb..78d4a275f 100644
--- a/src/jquery/coreTest.js
+++ b/src/jquery/coreTest.js
@@ -435,3 +435,7 @@ test("removeAttr(String", function() {
ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
});
+test("text(String, Boolean)", function() {
+ ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );
+ ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>", true)[0].innerHTML == "Hello cruel world!", "Check stripped text" );
+}); \ No newline at end of file
diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js
index e38044147..78d1ab94d 100644
--- a/src/jquery/jquery.js
+++ b/src/jquery/jquery.js
@@ -526,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.
*
- * @example $("p").text("Some new text.");
+ * Similar to html(), but escapes HTML (replace "<" and ">" with their
+ * HTML entities.
+ *
+ * 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.
@@ -537,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 = "";