Skip to content

Commit

Permalink
fix(text): Include script and style contents (#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 30, 2022
1 parent 17cb83f commit 2e9fd63
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
17 changes: 4 additions & 13 deletions src/__tests__/deprecated.spec.ts
Expand Up @@ -340,25 +340,16 @@ describe('deprecated APIs', () => {
);
});

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

it('(cheerio object) : should omit style tags', () => {
const $ = cheerio.load(
'<style type="text/css">.cf-hidden { display: none; } .cf-invisible { visibility: hidden; }</style>'
);
expect($.text()).toBe('');
});

it('(cheerio object) : should include text contents of children omitting style and script tags', () => {
const $ = 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()).toBe(
'Welcome Hello, testing text function,End of messege'
'<style type="text/css">.cf-hidden { display: none; }</style>'
);
expect($.text()).toBe('.cf-hidden { display: none; }');
});
});
});
Expand Down
24 changes: 24 additions & 0 deletions src/api/attributes.spec.ts
Expand Up @@ -305,6 +305,19 @@ describe('$(...)', () => {
expect($(script).prop('textContent')).toBe('A var foo = "bar";B');
});

it('("textContent") : should include style and script tags', () => {
const $ = 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 message</body>'
);
expect($('body').prop('textContent')).toBe(
'Welcome Hello, testing text function,console.log("hello").cf-hidden { display: none; }End of message'
);
expect($('style').prop('textContent')).toBe(
'.cf-hidden { display: none; }'
);
expect($('script').prop('textContent')).toBe('console.log("hello")');
});

it('("innerText") : should render properly', () => {
expect(selectMenu.children().prop('innerText')).toBe(
'Option not selected'
Expand All @@ -313,6 +326,17 @@ describe('$(...)', () => {
expect($(script).prop('innerText')).toBe('AB');
});

it('("innerText") : should omit style and script tags', () => {
const $ = 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 message</body>'
);
expect($('body').prop('innerText')).toBe(
'Welcome Hello, testing text function,End of message'
);
expect($('style').prop('innerText')).toBe('');
expect($('script').prop('innerText')).toBe('');
});

it('(inherited properties) : prop should support inherited properties', () => {
expect(selectMenu.prop('childNodes')).toBe(selectMenu[0].childNodes);
});
Expand Down
17 changes: 4 additions & 13 deletions src/static.spec.ts
Expand Up @@ -77,25 +77,16 @@ describe('cheerio', () => {
);
});

it('(cheerio object) : should omit script tags', () => {
it('(cheerio object) : should not omit script tags', () => {
const $ = cheerio.load('<script>console.log("test")</script>');
expect(cheerio.text($.root())).toBe('');
expect(cheerio.text($.root())).toBe('console.log("test")');
});

it('(cheerio object) : should omit style tags', () => {
const $ = cheerio.load(
'<style type="text/css">.cf-hidden { display: none; } .cf-invisible { visibility: hidden; }</style>'
);
expect($.text()).toBe('');
});

it('(cheerio object) : should include text contents of children omitting style and script tags', () => {
const $ = 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(cheerio.text($.root())).toBe(
'Welcome Hello, testing text function,End of messege'
'<style type="text/css">.cf-hidden { display: none; }</style>'
);
expect($.text()).toBe('.cf-hidden { display: none; }');
});

it('() : does not crash with `null` as `this` value', () => {
Expand Down
18 changes: 7 additions & 11 deletions src/static.ts
@@ -1,13 +1,13 @@
import type { BasicAcceptedElems } from './types.js';
import type { CheerioAPI, Cheerio } from '.';
import { AnyNode, Document, isText, hasChildren } from 'domhandler';
import type { AnyNode, Document } from 'domhandler';
import { textContent } from 'domutils';
import {
InternalOptions,
CheerioOptions,
default as defaultOptions,
flatten as flattenOptions,
} from './options.js';
import { ElementType } from 'htmlparser2';

/**
* Helper function to render a DOM.
Expand Down Expand Up @@ -109,6 +109,10 @@ export function xml(
/**
* Render the document as text.
*
* This returns the `textContent` of the passed elements. The result will
* include the contents of `script` and `stype` elements. To avoid this, use
* `.prop('innerText')` instead.
*
* @param elements - Elements to render.
* @returns The rendered document.
*/
Expand All @@ -121,15 +125,7 @@ export function text(
let ret = '';

for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (isText(elem)) ret += elem.data;
else if (
hasChildren(elem) &&
elem.type !== ElementType.Script &&
elem.type !== ElementType.Style
) {
ret += text(elem.children);
}
ret += textContent(elems[i]);
}

return ret;
Expand Down

0 comments on commit 2e9fd63

Please sign in to comment.