whacko icon indicating copy to clipboard operation
whacko copied to clipboard

Add fragment parsing option for $.load()

Open kaldrenon opened this issue 10 years ago • 5 comments

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.

kaldrenon avatar Dec 15 '14 19:12 kaldrenon

Also tried using $.xml() instead of $.html(), same result.

kaldrenon avatar Dec 15 '14 20:12 kaldrenon

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.

inikulin avatar Dec 15 '14 20:12 inikulin

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.

kaldrenon avatar Dec 15 '14 20:12 kaldrenon

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

inikulin avatar Dec 15 '14 21:12 inikulin

+1 this feature is awesome, but seems to already exist?

https://github.com/inikulin/whacko/blob/master/lib/static.js#L14

alanclarke avatar Mar 02 '15 09:03 alanclarke