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(1) does not convert a number to a string #2026

Closed
dorward opened this issue Aug 2, 2021 · 0 comments · Fixed by #2047
Closed

text(1) does not convert a number to a string #2026

dorward opened this issue Aug 2, 2021 · 0 comments · Fixed by #2047

Comments

@dorward
Copy link

dorward commented Aug 2, 2021

In Cheerio 1.0.0-rc.10, when passed a number, the text() method does not convert it to a string.

This throws errors if you later try to serialize it to HTML.

(I think this is a regression and the program that I used text(a_number) in was working last year).

Example program

const cheerio = require('cheerio');
const $ = cheerio.load('<p>A program containing <span>a placeholder</span></p>');
$('span').text(1);
const html = $.html();
console.log(html);

Output


    str = str.replace(AMP_REGEX, '&amp;').replace(NBSP_REGEX, '&nbsp;');
              ^

TypeError: str.replace is not a function
    at Function.Serializer.escapeString (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:165:15)
    at Serializer._serializeTextNode (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:148:37)
    at Serializer._serializeChildNodes (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:52:26)
    at Serializer._serializeElement (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:93:18)
    at Serializer._serializeChildNodes (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:50:26)
    at Serializer._serializeElement (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:93:18)
    at Serializer._serializeChildNodes (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:50:26)
    at Serializer._serializeElement (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:93:18)
    at Serializer._serializeChildNodes (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:50:26)
    at Serializer._serializeElement (/Users/david/prog/cheerio-test-case/node_modules/parse5/lib/serializer/index.js:93:18)     

… because it stores 1 internally and does not convert it to a string. Numbers do not have a replacemethod.

Expected behaviour

text(1) would convert the number to a string and the output would be:

<html><head></head><body><p>A program containing <span>1</span></p></body></html>

(It would also be OK if it type checked the input and throw an exception if you tried to set a non-string with text())

Workaround

Explicitly passing a string works around the issue (this isn't ideal of course, but it serves to prove the source of the problem).

$('span').text(`${1}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant