question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maintain tag attribute quote characters

See original GitHub issue

Cheerio changes attributes with single quotes into double quotes.

var cheerio = require('cheerio');
// uses 'single quotes'
var $ = cheerio.load('<div attr=\'value\'></div>')
$.html();
// => <div attr="value"></div>
// has "double quotes"

This is useful for me, as I use JSON in HTML attributes for widget settings.

<div data-settings='{ "option": true }'></div>

Which is encoded with HTML entities (possibly breaking JSON) as

<div data-options="{ &quot;option&quot;: true }"></div>

Setting decodeEntities: false is encoded and breaks HTML

<div data-options="{ "option": true }"></div>

Ideally, cheerio would preserve which quote character is used. I understand this is an edge case, so I’m reporting it in case others run into it. Similar to #460

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:5
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
gaecomcommented, Dec 26, 2016

I fix this. steps:

  1. find the file node_modules/dom-serializer/index.js
  2. location the line at 68,change
else {
output += key + "='" + (opts.decodeEntities ? entities.encodeXML(value) : value) + "'";
}

To

else {
  if(/[^\\]\"/.test(value)){
        output += key + "='" + (opts.decodeEntities ? entities.encodeXML(value) : value) + "'";
      }else {
        output += key + '="' + (opts.decodeEntities ? entities.encodeXML(value) : value) + '"';

      }
}
1reaction
ederchronocommented, Jul 27, 2018

I just had this problem and fixed it by adding decodeEntities: false to the props when loading the html:

this.$ = cheerio.load(myHtml, {decodeEntities: false})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unquoted attribute values in HTML and CSS/JS selectors
Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value...
Read more >
Do HTML attributes need quotation marks? - Stack Overflow
Attributes are placed inside the start tag, and consist of a name and a value, separated by an "=" character. The attribute value...
Read more >
Special Characters in HTML - DeGraeve.com
Special Characters in HTML ; right double quote, ” ; double low-9 quote, „ ; dagger, † ; double dagger, ‡ ; per...
Read more >
Quoting in HTML: Quotations, Citations, and Blockquotes
Blockquote tags are used for distinguishing quoted text from the rest of ... Both <q> and blockquotes can use a citation ( cite...
Read more >
Escape Quotation Marks in Attribute Values - InformIT
I usually don't bother. Even inside attribute values, you only need to escape the kind of quote that delimits the attribute value. Because ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found