getting special character instead of single quote when writing to HTML file using nodeJS and cheerio












1















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(&apos;12345&apos;)">


how can i get ' instead of &apos; ??










share|improve this question

























  • 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 get onclick="openPage(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

    – Alexander Solonik
    Nov 28 '18 at 9:18


















1















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(&apos;12345&apos;)">


how can i get ' instead of &apos; ??










share|improve this question

























  • 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 get onclick="openPage(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

    – Alexander Solonik
    Nov 28 '18 at 9:18
















1












1








1


0






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(&apos;12345&apos;)">


how can i get ' instead of &apos; ??










share|improve this question
















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(&apos;12345&apos;)">


how can i get ' instead of &apos; ??







node.js html-entities cheerio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 be onclick="openPage('APR_18Q2_Ramazan_02_Cevap')" , currently i get onclick="openPage(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

    – 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











  • 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 get onclick="openPage(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

    – 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(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

– 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(&quot;APR_18Q2_Ramazan_02_Cevap&quot;)"

– Alexander Solonik
Nov 28 '18 at 9:18














1 Answer
1






active

oldest

votes


















3














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} );





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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} );





    share|improve this answer




























      3














      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} );





      share|improve this answer


























        3












        3








        3







        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} );





        share|improve this answer













        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} );






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 8:15









        Nedko DimitrovNedko Dimitrov

        533716




        533716
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista