getting special character instead of single quote when writing to HTML file using nodeJS and cheerio
I was just playing around with node, so i wanted to replace the following html in my html file
<div class="replace" onclick="opennewtab('one')">
Wanted to replace it to
<div class="replace" onclick="replacedFunc('12345&')">
So i have the following node code:
let fs = require('fs'),
cheerio = require('cheerio');
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ) );
$('.replace').attr('onclick' , "replacedFunc('12345')");
console.log($.html());
inner_content = $.html();
fs.writeFileSync( `${__dirname}/res/newwritetojs.html` , inner_content, 'utf8');
But what i get is
<div class="replace" onclick="replacedFunc('12345')">
how can i get ' instead of ' ??
node.js html-entities cheerio
|
show 3 more comments
I was just playing around with node, so i wanted to replace the following html in my html file
<div class="replace" onclick="opennewtab('one')">
Wanted to replace it to
<div class="replace" onclick="replacedFunc('12345&')">
So i have the following node code:
let fs = require('fs'),
cheerio = require('cheerio');
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ) );
$('.replace').attr('onclick' , "replacedFunc('12345')");
console.log($.html());
inner_content = $.html();
fs.writeFileSync( `${__dirname}/res/newwritetojs.html` , inner_content, 'utf8');
But what i get is
<div class="replace" onclick="replacedFunc('12345')">
how can i get ' instead of ' ??
node.js html-entities cheerio
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
@usr2564301 i need the output to beonclick="openPage('APR_18Q2_Ramazan_02_Cevap')", currently i getonclick="openPage("APR_18Q2_Ramazan_02_Cevap")"
– Alexander Solonik
Nov 28 '18 at 9:18
|
show 3 more comments
I was just playing around with node, so i wanted to replace the following html in my html file
<div class="replace" onclick="opennewtab('one')">
Wanted to replace it to
<div class="replace" onclick="replacedFunc('12345&')">
So i have the following node code:
let fs = require('fs'),
cheerio = require('cheerio');
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ) );
$('.replace').attr('onclick' , "replacedFunc('12345')");
console.log($.html());
inner_content = $.html();
fs.writeFileSync( `${__dirname}/res/newwritetojs.html` , inner_content, 'utf8');
But what i get is
<div class="replace" onclick="replacedFunc('12345')">
how can i get ' instead of ' ??
node.js html-entities cheerio
I was just playing around with node, so i wanted to replace the following html in my html file
<div class="replace" onclick="opennewtab('one')">
Wanted to replace it to
<div class="replace" onclick="replacedFunc('12345&')">
So i have the following node code:
let fs = require('fs'),
cheerio = require('cheerio');
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ) );
$('.replace').attr('onclick' , "replacedFunc('12345')");
console.log($.html());
inner_content = $.html();
fs.writeFileSync( `${__dirname}/res/newwritetojs.html` , inner_content, 'utf8');
But what i get is
<div class="replace" onclick="replacedFunc('12345')">
how can i get ' instead of ' ??
node.js html-entities cheerio
node.js html-entities cheerio
edited Nov 29 '18 at 8:25
Nedko Dimitrov
533716
533716
asked Nov 28 '18 at 8:30
Alexander SolonikAlexander Solonik
3,46263590
3,46263590
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
@usr2564301 i need the output to beonclick="openPage('APR_18Q2_Ramazan_02_Cevap')", currently i getonclick="openPage("APR_18Q2_Ramazan_02_Cevap")"
– Alexander Solonik
Nov 28 '18 at 9:18
|
show 3 more comments
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
@usr2564301 i need the output to beonclick="openPage('APR_18Q2_Ramazan_02_Cevap')", currently i getonclick="openPage("APR_18Q2_Ramazan_02_Cevap")"
– Alexander Solonik
Nov 28 '18 at 9:18
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
@usr2564301 i need the output to be
onclick="openPage('APR_18Q2_Ramazan_02_Cevap')" , currently i get onclick="openPage("APR_18Q2_Ramazan_02_Cevap")"– Alexander Solonik
Nov 28 '18 at 9:18
@usr2564301 i need the output to be
onclick="openPage('APR_18Q2_Ramazan_02_Cevap')" , currently i get onclick="openPage("APR_18Q2_Ramazan_02_Cevap")"– Alexander Solonik
Nov 28 '18 at 9:18
|
show 3 more comments
1 Answer
1
active
oldest
votes
Cheerio is decoding HTML Entities by default, when needed you can turn off this feature by passing decodeEntities: false option
Here is an example:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515136%2fgetting-special-character-instead-of-single-quote-when-writing-to-html-file-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cheerio is decoding HTML Entities by default, when needed you can turn off this feature by passing decodeEntities: false option
Here is an example:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
add a comment |
Cheerio is decoding HTML Entities by default, when needed you can turn off this feature by passing decodeEntities: false option
Here is an example:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
add a comment |
Cheerio is decoding HTML Entities by default, when needed you can turn off this feature by passing decodeEntities: false option
Here is an example:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
Cheerio is decoding HTML Entities by default, when needed you can turn off this feature by passing decodeEntities: false option
Here is an example:
$ = cheerio.load( fs.readFileSync( `${__dirname}/res/writetojs.html` ), {decodeEntities: false} );
answered Nov 29 '18 at 8:15
Nedko DimitrovNedko Dimitrov
533716
533716
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515136%2fgetting-special-character-instead-of-single-quote-when-writing-to-html-file-usin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
how do you send it to the browser? The code you've posted does not do any escaping
– Nedko Dimitrov
Nov 28 '18 at 8:44
i write it to a html file check edit
– Alexander Solonik
Nov 28 '18 at 8:52
I cannot reproduce the issue, there something more to it. Here is the test I've done jsfiddle.net/mvwf19js There no escaped quoted in the browser nor in the index.html file
– Nedko Dimitrov
Nov 28 '18 at 9:01
Why is the usual XML character escaping a problem for you?
– usr2564301
Nov 28 '18 at 9:16
@usr2564301 i need the output to be
onclick="openPage('APR_18Q2_Ramazan_02_Cevap')", currently i getonclick="openPage("APR_18Q2_Ramazan_02_Cevap")"– Alexander Solonik
Nov 28 '18 at 9:18