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

fix(text): Include script and style contents #2509

Merged
merged 2 commits into from
Apr 30, 2022
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
17 changes: 4 additions & 13 deletions src/__tests__/deprecated.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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