Skip to content

Commit

Permalink
fix: 🐛 add monkey patch for cheerio rc.2 encoding issue (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingallday committed Jun 9, 2022
1 parent 8491b80 commit a0e1965
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 21 additions & 0 deletions packages/components/Organisms/Markup/markup-transpiler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import cheerio from 'cheerio'

// https://github.com/cheeriojs/cheerio/issues/866#issuecomment-275699121
// This monkey patch to rc.2 stops cheerio from encoding utf8 chars (fixed in non-IE11 compliant rc.10)
const cheerioHtml = cheerio.prototype.html
cheerio.prototype.html = function wrappedHtml () {
var result = cheerioHtml.apply(this, arguments)

if (typeof result === 'string') {
result = result.replace(/&#x([0-9a-f]{1,6});/ig, function (entity, code) {
code = parseInt(code, 16)

// don't unescape ascii characters, assuming that all ascii characters
// are encoded for a good reason
if (code < 0x80) return entity

return String.fromCodePoint(code)
})
}

return result
}

// A markup transpiler for converting HTML into Vue template by giving plugins.
const markupTranspiler = (html, plugins = {}, options = {}) => {
const $ = cheerio.load(html, options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ exports[`Markup transpiler should able transpile html into vue template with plu
exports[`Markup transpiler should get same HTML with no plugins 1`] = `
"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<a href=\\"http://google.com\\">&lt;&gt;&amp;&apos;&quot;&#xA9;. &#xBD; &#x20AC; &#x4E2D;&#x6587;</a>
<h4>Sort out your&apos;&gt;&lt;&amp;&apos;&quot;s finances</h4>\\\\n\\\\n<p><a href=\\"https://www.test.dev/test\\">Here is a&#xA0;irregular white space</a>&#xA0;Another irregular white space.</p>
<p>&#xA0; &#xA0; &#xA0;</p>
<p>Single&#xA0;line&#x2000;demonstrating&#x2001;a&#x2002;mixture&#x2003;of&#x2004;<em>inline&#x2005;markup</em>&#x2006;and&#x2007;the&#x2008;<b>19</b>&#x2009;different&#x200A;unicode&#x200B;codepoints&#x202F;that&#x205F;can&#xFEFF;be used for spaces.</p>
<a href=\\"http://google.com\\">&lt;&gt;&amp;&apos;&quot;©. ½ 中文</a>
<h4>Sort out your&apos;&gt;&lt;&amp;&apos;&quot;s finances</h4>\\\\n\\\\n<p><a href=\\"https://www.test.dev/test\\">Here is a irregular white space</a> Another irregular white space.</p>
<p>     </p>
<p>Single line demonstrating a mixture of <em>inlinemarkup</em>andthe<b>19</b>differentunicodecodepointsthatcanbe used for spaces.</p>
<img src=\\"https://test.dev/test.jpeg\\" title=\\"&quot;Needles, Knots &amp; Threads&quot;- an Exhibition of Embroidery &amp; Quilting\\">
<p>&#xA0;</p>
<p> </p>
<!-- parseForLinks -->
<h2>This is a heading 2</h2>
Expand Down

0 comments on commit a0e1965

Please sign in to comment.