From b8d7206ace219099d59bee86ac3c0ee3edcf5a1c Mon Sep 17 00:00:00 2001 From: fntzr Date: Mon, 1 Oct 2012 20:25:20 +0300 Subject: [PATCH 1/4] add some methods for Hash --- src/prototype/lang/hash.js | 64 ++++++++++++++++++++++++++++++++++++++ test/unit/hash_test.js | 30 ++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/prototype/lang/hash.js b/src/prototype/lang/hash.js index d934327c0..c7e4f2ce6 100644 --- a/src/prototype/lang/hash.js +++ b/src/prototype/lang/hash.js @@ -104,6 +104,67 @@ var Hash = Class.create(Enumerable, (function() { } } + /** + * Hash#each_key(iterator[, context]) -> Hash + * + * Iterates over the keys in the hash. + * + * ##### Example + * + * var hash = $H({England: 'London', Poland: 'Warsaw'}); + * + * h.each_key(function(country) { + * alert(country); + * }); + * // Alerts: England + * // Alerts: Poland + * + **/ + function each_key(iterator, context) { + this.keys().each(iterator, context); + } + + /** + * Hash#each_value(iterator[, context]) -> Hash + * + * Iterates over the values in the hash. + * + * ##### Example + * + * var hash = $H({England: 'London', Poland: 'Warsaw'}); + * + * h.each_value(function(capital) { + * alert(capital); + * }); + * // Alerts: London + * // Alerts: Warsaw + **/ + function each_value(iterator, context) { + this.values().each(iterator, context); + } + + /** + * Hash#each_pair(iterator[, context]) -> Hash + * + * Iterates over the key/value pairs in the hash. + * + * ##### Example + * + * var hash = $H({England: 'London', Poland: 'Warsaw'}); + * + * h.each_pair(function(country, capital) { + * alert(capital + "is the capital of " + country); + * }); + * //Alerts: London is the capital of England + * //Alerts: Warsaw is the capital of Poland + * + **/ + function each_pair(iterator, context) { + this.each(function(pair) { + iterator.call(context, pair.key, pair.value) + }); + } + /** * Hash#set(key, value) -> value * - key (String): The key to use for this value. @@ -388,6 +449,9 @@ var Hash = Class.create(Enumerable, (function() { return { initialize: initialize, _each: _each, + each_key: each_key, + each_value: each_value, + each_pair: each_pair, set: set, get: get, unset: unset, diff --git a/test/unit/hash_test.js b/test/unit/hash_test.js index d39f07840..fa863c55a 100644 --- a/test/unit/hash_test.js +++ b/test/unit/hash_test.js @@ -191,6 +191,36 @@ new Test.Unit.Runner({ result.push(i); }); this.assertEnumEqual([0,1], result); + }, + + testIterationWithEachKey: function() { + var hash = $H({a:1, b:2, c:3}); + var keys = []; + hash.each_key(function(key) { + keys.push(key); + }); + this.assertEnumEqual(['a', 'b', 'c'], keys); + }, + + testIterationWithEachValue: function() { + var hash = $H({a:1, b:2, c:3}); + var values = []; + hash.each_value(function(value) { + values.push(value); + }); + this.assertEnumEqual([1, 2, 3], values); + }, + + testIterationWithEachPair: function() { + var hash = $H({a:1, b:2, c:3}); + var keys = []; + var values = []; + hash.each_pair(function(key, value) { + keys.push(key); + values.push(value); + }); + this.assertEnumEqual([1, 2, 3], values); + this.assertEnumEqual(['a', 'b', 'c'], keys); } }); \ No newline at end of file From aa4d8869489965c210545742a23027b69d5d188b Mon Sep 17 00:00:00 2001 From: fntzr Date: Mon, 1 Oct 2012 21:36:15 +0300 Subject: [PATCH 2/4] add content method --- src/prototype/dom/dom.js | 18 +++++++++++++++++- test/unit/dom_test.js | 22 +++++++++++++++++++++- test/unit/fixtures/dom.html | 16 +++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/prototype/dom/dom.js b/src/prototype/dom/dom.js index acc42dbeb..219156409 100644 --- a/src/prototype/dom/dom.js +++ b/src/prototype/dom/dom.js @@ -1211,6 +1211,21 @@ return null; } + + /** + * Element.content(@element[, withoutChild]) -> String + * + **/ + function content(element, withoutChild) { + var element = $(element); + + if (!element.hasChildNodes()) + return ''; + + return $A(element.childNodes).inject('', function(acc,node) { + return acc += node.nodeType == 3? node.nodeValue : withoutChild == true? '' : content(node); + }); + } Object.extend(methods, { remove: remove, @@ -1221,7 +1236,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..53dde49bd 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 contentWithoutChild = Element.content('testContent', true); + var arr = contentWithoutChild = contentWithoutChild.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 From 1a868cd55fd7a0901cee8fead85d3291da14355f Mon Sep 17 00:00:00 2001 From: fntzr Date: Mon, 1 Oct 2012 21:56:53 +0300 Subject: [PATCH 3/4] add docs --- src/prototype/dom/dom.js | 29 ++++++++++++++++++++++++++--- test/unit/dom_test.js | 4 ++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/prototype/dom/dom.js b/src/prototype/dom/dom.js index 219156409..242945ae9 100644 --- a/src/prototype/dom/dom.js +++ b/src/prototype/dom/dom.js @@ -1213,17 +1213,40 @@ } /** - * Element.content(@element[, withoutChild]) -> String + * 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, withoutChild) { + 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 : withoutChild == true? '' : content(node); + return acc += node.nodeType == 3? node.nodeValue : nested == true? '' : content(node); }); } diff --git a/test/unit/dom_test.js b/test/unit/dom_test.js index 53dde49bd..03593c40c 100644 --- a/test/unit/dom_test.js +++ b/test/unit/dom_test.js @@ -1720,8 +1720,8 @@ new Test.Unit.Runner({ this.assertEqual(true, Object.isString(content)); this.assertEnumEqual(['test#a', 'testem', 'zed', 'span', 'test#b'], arr_content); - var contentWithoutChild = Element.content('testContent', true); - var arr = contentWithoutChild = contentWithoutChild.split('\n').findAll(function(a) { + var contentWithoutNested = Element.content('testContent', true); + var arr = contentWithoutNested.split('\n').findAll(function(a) { if (!a.strip().blank()) return a; }).invoke('strip'); From 515f183986a4444f7ee4028432553f903095148a Mon Sep 17 00:00:00 2001 From: fntzr Date: Tue, 2 Oct 2012 20:45:37 +0300 Subject: [PATCH 4/4] remove changes files --- src/prototype/lang/hash.js | 64 -------------------------------------- test/unit/hash_test.js | 30 ------------------ 2 files changed, 94 deletions(-) diff --git a/src/prototype/lang/hash.js b/src/prototype/lang/hash.js index c7e4f2ce6..d934327c0 100644 --- a/src/prototype/lang/hash.js +++ b/src/prototype/lang/hash.js @@ -104,67 +104,6 @@ var Hash = Class.create(Enumerable, (function() { } } - /** - * Hash#each_key(iterator[, context]) -> Hash - * - * Iterates over the keys in the hash. - * - * ##### Example - * - * var hash = $H({England: 'London', Poland: 'Warsaw'}); - * - * h.each_key(function(country) { - * alert(country); - * }); - * // Alerts: England - * // Alerts: Poland - * - **/ - function each_key(iterator, context) { - this.keys().each(iterator, context); - } - - /** - * Hash#each_value(iterator[, context]) -> Hash - * - * Iterates over the values in the hash. - * - * ##### Example - * - * var hash = $H({England: 'London', Poland: 'Warsaw'}); - * - * h.each_value(function(capital) { - * alert(capital); - * }); - * // Alerts: London - * // Alerts: Warsaw - **/ - function each_value(iterator, context) { - this.values().each(iterator, context); - } - - /** - * Hash#each_pair(iterator[, context]) -> Hash - * - * Iterates over the key/value pairs in the hash. - * - * ##### Example - * - * var hash = $H({England: 'London', Poland: 'Warsaw'}); - * - * h.each_pair(function(country, capital) { - * alert(capital + "is the capital of " + country); - * }); - * //Alerts: London is the capital of England - * //Alerts: Warsaw is the capital of Poland - * - **/ - function each_pair(iterator, context) { - this.each(function(pair) { - iterator.call(context, pair.key, pair.value) - }); - } - /** * Hash#set(key, value) -> value * - key (String): The key to use for this value. @@ -449,9 +388,6 @@ var Hash = Class.create(Enumerable, (function() { return { initialize: initialize, _each: _each, - each_key: each_key, - each_value: each_value, - each_pair: each_pair, set: set, get: get, unset: unset, diff --git a/test/unit/hash_test.js b/test/unit/hash_test.js index fa863c55a..d39f07840 100644 --- a/test/unit/hash_test.js +++ b/test/unit/hash_test.js @@ -191,36 +191,6 @@ new Test.Unit.Runner({ result.push(i); }); this.assertEnumEqual([0,1], result); - }, - - testIterationWithEachKey: function() { - var hash = $H({a:1, b:2, c:3}); - var keys = []; - hash.each_key(function(key) { - keys.push(key); - }); - this.assertEnumEqual(['a', 'b', 'c'], keys); - }, - - testIterationWithEachValue: function() { - var hash = $H({a:1, b:2, c:3}); - var values = []; - hash.each_value(function(value) { - values.push(value); - }); - this.assertEnumEqual([1, 2, 3], values); - }, - - testIterationWithEachPair: function() { - var hash = $H({a:1, b:2, c:3}); - var keys = []; - var values = []; - hash.each_pair(function(key, value) { - keys.push(key); - values.push(value); - }); - this.assertEnumEqual([1, 2, 3], values); - this.assertEnumEqual(['a', 'b', 'c'], keys); } }); \ No newline at end of file