Cheerio: .html() returns outerHTML, while .html(html) sets innerHTML

Created on 19 Dec 2011  路  8Comments  路  Source: cheeriojs/cheerio

I can't believe this one has gone unnoticed. I'm not really sure what to do about it actually. I think it's more useful how it is right now, but I don't like the asymmetry. I also don't like straying away from the jQuery API.

What do you guy's think?

馃挜 jQuery Differences

Most helpful comment

Yah, I tend to agree with you. I want it to be incredibly easy to get the outer HTML though, because most of my use cases involve manipulating the html string, then rendering the _whole_ string.

I'm realizing though, that in jQuery i never needed to get the outer HTML, because I'm never rendering the whole document. So this is what I'm thinking:

$('.orange').html() // => returned innerHTML

$.html() // => Returned entire HTML string

This way we are adhering to the jQuery spec while extending the spec gracefully to cover a major use case of server-side rendering.

All 8 comments

I think that we should stick to the jQuery spec. So .html() should both return and set innerHTML.

Yah.. but jQuery doesn't have a good way of getting "outerHTML".

Then it would be better to provide a separate function to get and set outerHTML, rather than mix and match with .html().

Plus, it's better to break functionality early on, rather than later.

I'd like to be able to use this library to execute the same manipulation on the server as I do in the browser. jQuery compatibility seems like the highest priority. I think .html() should read and write innerHTML.

I'd vote for adding a special method like .toHtml() which would return an array of strings if you need to dump outerHTML of the collection for debugging purposes.

Yah, I tend to agree with you. I want it to be incredibly easy to get the outer HTML though, because most of my use cases involve manipulating the html string, then rendering the _whole_ string.

I'm realizing though, that in jQuery i never needed to get the outer HTML, because I'm never rendering the whole document. So this is what I'm thinking:

$('.orange').html() // => returned innerHTML

$.html() // => Returned entire HTML string

This way we are adhering to the jQuery spec while extending the spec gracefully to cover a major use case of server-side rendering.

I'm fine with that, also, I'd allow $.html to take an optional cheerio parameter, so in code like the following:

var span = $("<div><span>foo</span><span>bar</span></div>").children().get(1);
span.html() // => returns "bar"
$.html(span) // => returns "<span>bar</span>"

The major advantage to that, is if I wanted jQuery to behave the same way, I could write a jQuery plugin for that in 30 seconds.

Jeez @ironchefpython you're on fire! I like it. Added to master.

The following assertion fails:

      $.html($('<span>baz</span>')).should.equal('<span>baz</span>');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gajus picture gajus  路  4Comments

rajkumarpb picture rajkumarpb  路  3Comments

collegepinger picture collegepinger  路  3Comments

trevorfrese picture trevorfrese  路  4Comments

misner picture misner  路  3Comments