Skip to content

Add method for Element. #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/prototype/dom/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <div id="test">
* Lorem ipsum
* <p><span>dolor</span> sit amet</p>
* </div>
*
* 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,
Expand All @@ -1221,7 +1259,8 @@
cleanWhitespace: cleanWhitespace,
empty: empty,
clone: clone,
purge: purge
purge: purge,
content: content
});


Expand Down
22 changes: 21 additions & 1 deletion test/unit/dom_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion test/unit/fixtures/dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,18 @@
</div>

<div id='elementToViewportDimensions' style='display: none'></div>
<div id="auto_dimensions" style="height:auto"></div>
<div id="auto_dimensions" style="height:auto"></div>

<div id="testContent">
test#a
<p>
<em>testem</em>
<div>
zed
<span>
span
</span>
</div>
</p>
test#b
</div>