diff --git a/src/prototype/dom/dom.js b/src/prototype/dom/dom.js
index acc42dbeb..242945ae9 100644
--- a/src/prototype/dom/dom.js
+++ b/src/prototype/dom/dom.js
@@ -1211,6 +1211,44 @@
return null;
}
+
+ /**
+ * Element.content(@element[, withoutNested]) -> String
+ * - withoutNested (Boolean)
+ *
+ * Returns the text content of the @element and all nested elements.
+ * If `withoutNested` set to true (default: false) returns the text content only for @element.
+ *
+ * ##### Examples
+ *
+ * language: html
+ *
+ * Lorem ipsum
+ *
dolor sit amet
+ *
+ *
+ * Get content:
+ *
+ * $('test').content()
+ * // => Lorem ipsum
+ * // dolor sit amet
+ *
+ * Get content only from #test
+ *
+ * $('test').content(true)
+ * // Lorem ipsum
+ *
+ **/
+ function content(element, nested) {
+ var element = $(element);
+
+ if (!element.hasChildNodes())
+ return '';
+
+ return $A(element.childNodes).inject('', function(acc,node) {
+ return acc += node.nodeType == 3? node.nodeValue : nested == true? '' : content(node);
+ });
+ }
Object.extend(methods, {
remove: remove,
@@ -1221,7 +1259,8 @@
cleanWhitespace: cleanWhitespace,
empty: empty,
clone: clone,
- purge: purge
+ purge: purge,
+ content: content
});
diff --git a/test/unit/dom_test.js b/test/unit/dom_test.js
index b9ef36b6f..03593c40c 100644
--- a/test/unit/dom_test.js
+++ b/test/unit/dom_test.js
@@ -1707,7 +1707,27 @@ new Test.Unit.Runner({
// unregistered.
simulateClick(child);
this.assert(!trigger, "fired event should not have triggered handler");
- }
+ },
+
+ testElementContent: function() {
+ var content = $('testContent').content();
+ var arr_content = content.split('\n').findAll(function(a) {
+ if (!a.strip().blank()){
+ return a;
+ }
+ }).invoke('strip');
+
+ this.assertEqual(true, Object.isString(content));
+ this.assertEnumEqual(['test#a', 'testem', 'zed', 'span', 'test#b'], arr_content);
+
+ var contentWithoutNested = Element.content('testContent', true);
+ var arr = contentWithoutNested.split('\n').findAll(function(a) {
+ if (!a.strip().blank())
+ return a;
+ }).invoke('strip');
+
+ this.assertEnumEqual(['test#a', 'test#b'], arr);
+ }
});
function preservingBrowserDimensions(callback) {
diff --git a/test/unit/fixtures/dom.html b/test/unit/fixtures/dom.html
index 99395eeca..f58940f3a 100644
--- a/test/unit/fixtures/dom.html
+++ b/test/unit/fixtures/dom.html
@@ -275,4 +275,18 @@
-
\ No newline at end of file
+
+
+
+ test#a
+
+ testem
+
+ zed
+
+ span
+
+
+
+ test#b
+
\ No newline at end of file