You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
constcheerio=require('cheerio');const$=cheerio.load('<p>A program containing <span>a placeholder</span></p>');$('span').text(1);consthtml=$.html();console.log(html);
Output
str = str.replace(AMP_REGEX, '&').replace(NBSP_REGEX, ' ');
^
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}`);
The text was updated successfully, but these errors were encountered:
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
Output
… because it stores
1
internally and does not convert it to a string. Numbers do not have areplace
method.Expected behaviour
text(1)
would convert the number to a string and the output would be:(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).
The text was updated successfully, but these errors were encountered: