Skip to content
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

.text() ignores script and style tags #1018

Merged
merged 2 commits into from May 7, 2017
Merged
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
2 changes: 1 addition & 1 deletion lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ exports.text = function(elems) {
for (var i = 0; i < len; i++) {
elem = elems[i];
if (elem.type === 'text') ret += elem.data;
else if (elem.children && elem.type !== 'comment') {
else if (elem.children && elem.type !== 'comment' && elem.tagName !== 'script' && elem.tagName !== 'style') {
ret += exports.text(elem.children);
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ describe('cheerio', function() {
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>');
expect($.text()).to.equal('This is a child with another child and not a comment followed by one last child and some final text.');
});

it('(cheerio object) : should omit script tags', function(){
var $ = cheerio.load('<script>console.log("test")</script>');
expect($.text()).to.equal('');
});

it('(cheerio object) : should omit style tags', function(){
var $ = cheerio.load('<style type="text/css">.cf-hidden { display: none; } .cf-invisible { visibility: hidden; }</style>');
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