Skip to content

Commit 54359c9

Browse files
jugglinmikefb55
authored andcommittedJun 12, 2016
Document, test, and extend static $.text method (#855)
* Document and test static `text` method * Extend `$.text()` to operate on current root Update `$.root` to return the rendered text content of the current root element when no argument is specified. This increases API parity with the existing static methods `$.html` and``$.xml`. This change technically breaks backwards compatability, but the previous behavior was undocumented and untested.
1 parent c6612f3 commit 54359c9

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed
 

‎Readme.md

+15
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,21 @@ $.xml()
822822
//=> <media:thumbnail url="http://www.foo.com/keyframe.jpg" width="75" height="50" time="12:05:01.123"/>
823823
```
824824

825+
You may also render the text content of a Cheerio object using the `text` static method:
826+
827+
```js
828+
$ = cheerio.load('This is <em>content</em>.')
829+
$.text()
830+
//=> This is content.
831+
```
832+
833+
The method may be called on the Cheerio module itself--be sure to pass a collection of nodes!
834+
835+
```js
836+
$ = cheerio.load('<div>This is <em>content</em>.</div>')
837+
cheerio.text($('div'))
838+
//=> This is content.
839+
```
825840

826841
### Miscellaneous
827842
DOM element methods that don't fit anywhere else

‎lib/static.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ exports.xml = function(dom) {
109109
*/
110110

111111
exports.text = function(elems) {
112-
if (!elems) return '';
112+
if (!elems) {
113+
elems = this.root();
114+
}
113115

114116
var ret = '',
115117
len = elems.length,

‎test/api/utils.js

+22
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ describe('cheerio', function() {
3535
});
3636

3737

38+
describe('.text', function() {
39+
it('(cheerio object) : should return the text contents of the specified elements', function() {
40+
var $ = cheerio.load('<a>This is <em>content</em>.</a>');
41+
expect($.text($('a'))).to.equal('This is content.');
42+
});
43+
44+
it('(cheerio object) : should omit comment nodes', function() {
45+
var $ = cheerio.load('<a>This is <!-- a comment --> not a comment.</a>');
46+
expect($.text($('a'))).to.equal('This is not a comment.');
47+
});
48+
49+
it('(cheerio object) : should include text contents of children recursively', function() {
50+
var $ = cheerio.load('<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>');
51+
expect($.text($('a'))).to.equal('This is a child with another child and not a comment followed by one last child and some final text.');
52+
});
53+
54+
it('() : should return the rendered text content of the root', function() {
55+
var $ = cheerio.load('<a>This is <div>a child with <span>another child and <!-- a comment --> not a comment</span> followed by <em>one last child</em> and some final</div> text.</a>');
56+
expect($.text()).to.equal('This is a child with another child and not a comment followed by one last child and some final text.');
57+
});
58+
});
59+
3860

3961
describe('.load', function() {
4062

0 commit comments

Comments
 (0)
Please sign in to comment.