davidmerfield / Typeset

An HTML pre-proces­sor for web ty­pog­ra­phy
https://typeset.lllllllllllllllll.com/
Creative Commons Zero v1.0 Universal
2.41k stars 52 forks source link

Make the browser version work without jQuery #29

Closed lydell closed 7 years ago

lydell commented 9 years ago

This also fixes a few other browser issues:

.type is only available on elements in cheerio, which caused more than text nodes to be processed. The solution is to use .nodeType instead, which is available both in cheerio and in the DOM.

Setting .data on a text node in the DOM automatically escapes HTML, while in cheerio it does not. The solution is to change textNode.data = html to $(textNode).replaceWith(html).

Since browsers do not have the decodeEntities: false option, manually escape text that might be interpreted as HTML otherwise.

For example, if a text node contains the text a <p> tag that used to result in a new paragraph element being created, but doesn't anymore.

I also had to make a slight adjustment to get webpack to set ENV.browser = true.


I like that all files simply take a string as input, modify it and return it. Only eachTextNode.js deals with the DOM. I understand that simply delegating to jQuery is the simplest, since cheerio was already used in Node.js. But I think it’s a shame to require jQuery for such simple DOM manipulations.

This commits preserves functionality exactly like before in Node.js, and also lets you continue to use jQuery if available. But it also lets you pass a regular DOM node without jQuery.