Skip to content

Commit

Permalink
.text() ignores script and style tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamiko committed May 6, 2017
1 parent 4aeae2a commit 71ec6bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ exports.text = function(elems) {

for (var i = 0; i < len; i++) {
elem = elems[i];
if (elem.type === 'text' && elem.tagName != 'script' && elem.tagName != 'style') ret += elem.data;
else if (elem.children && elem.type !== 'comment' && elem.tagName != 'script' && elem.tagName != 'style') {
if (elem.type === 'text') ret += elem.data;
else if (elem.children && elem.type !== 'comment' && elem.tagName !== 'script' && elem.tagName !== 'style') {
ret += exports.text(elem.children);
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ describe('cheerio', function() {
expect($.text()).to.equal('');
});

it('(cheerio object) : should include text contents of children omiting style and script tags', function(){
var $ = cheerio.load('<body>Welcome <div>Hello, testing text function,<script>console.log("hello")</script></div><style type="text/css">.cf-hidden { display: none; }</style>End of messege</body>');
expect($.text()).to.equal('Welcome Hello, testing text function,End of messege');
});

});


Expand Down

0 comments on commit 71ec6bb

Please sign in to comment.