From 4aeae2a3d401a300815f9117fb2986f9f59ee5fb Mon Sep 17 00:00:00 2001 From: Dreamiko Date: Fri, 5 May 2017 00:29:15 -0300 Subject: [PATCH 1/2] text function ignores script and style tags --- lib/static.js | 4 ++-- test/api/utils.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/static.js b/lib/static.js index 947d440550..bdd6f55057 100644 --- a/lib/static.js +++ b/lib/static.js @@ -119,8 +119,8 @@ 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') { + 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') { ret += exports.text(elem.children); } } diff --git a/test/api/utils.js b/test/api/utils.js index 64f5ecb187..55f9a4cbee 100644 --- a/test/api/utils.js +++ b/test/api/utils.js @@ -55,6 +55,17 @@ describe('cheerio', function() { var $ = cheerio.load('This is
a child with another child and not a comment followed by one last child and some final
text.
'); 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(''); + expect($.text()).to.equal(''); + }); + + it('(cheerio object) : should omit style tags', function(){ + var $ = cheerio.load(''); + expect($.text()).to.equal(''); + }); + }); From 71ec6bb70df40084fc11441e09d3ff586fdf5bd2 Mon Sep 17 00:00:00 2001 From: Dreamiko Date: Sat, 6 May 2017 11:18:52 -0300 Subject: [PATCH 2/2] .text() ignores script and style tags --- lib/static.js | 4 ++-- test/api/utils.js | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/static.js b/lib/static.js index bdd6f55057..679930be99 100644 --- a/lib/static.js +++ b/lib/static.js @@ -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); } } diff --git a/test/api/utils.js b/test/api/utils.js index 55f9a4cbee..d6983c8a1c 100644 --- a/test/api/utils.js +++ b/test/api/utils.js @@ -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('Welcome
Hello, testing text function,
End of messege'); + expect($.text()).to.equal('Welcome Hello, testing text function,End of messege'); + }); + });