whacko
whacko copied to clipboard
Add fragment parsing option for $.load()
Simple example:
$ = whacko.load('<h1>This should be alone</h1>');
console.log($.html());
I expect and want the output from that log statement to be:
<h1>This should be alone</h1>
Instead, the output is:
<html><head></head><body><h1>This should be alone</h1></body></html>
Is there a config option for this, or a method I should use instead of html()
? I reviewed Cheerio's API and the options for the parser, and I couldn't find anything for this.
Also tried using $.xml()
instead of $.html()
, same result.
Hi, quote from the whacko API note in readme:
Use $.load(content) to load HTML documents (e.g. missing tags will be automatically emitted in this case). Use $(content) to create HTML-fragments which can be later added to the loaded document.
So, tranforming your code this way:
$ = whacko('<h1>This should be alone</h1>');
console.log($.html());
you will get desired result.
Thanks for the quick response.
Unfortunately, in the example
$ = whacko('<h1>This should be alone</h1>');
console.log($.html());
I get This should be alone
with no wrapping <h1>
tags.
Ok, I see now that it may be confusing in some cases. I consider adding option to the load()
. Thanks for the report!
As a temporary workaround I can suggest you to wrap your content with additional <div>
element.
E.g.:
$ = whacko('<div><h1>This should be alone</h1></div>');
console.log($.html());
+1 this feature is awesome, but seems to already exist?
https://github.com/inikulin/whacko/blob/master/lib/static.js#L14