ded / bonzo

library agnostic, extensible DOM utility
Other
1.32k stars 137 forks source link

.html() can insert HTMLElements #69

Closed zenlor closed 12 years ago

zenlor commented 12 years ago

don't know if it was intended, but, calling $('#id').html($('<div>')) used el.innerHTML instead of appendChild() as http://jsfiddle.net/UwzVF/1/

var bDiv = bonzo.create('<div>');

bonzo(bDiv).text('bonzo')

bonzo(document.getElementById('bz'))
    .html(bDiv)

resulted in:

<div id="bz">[object HTMLDivElement]</div>

there is a small caveat, in the code I could see a reference to (m = el.tagName.match(specialTags)) calling append(el, m[0]), but append used only the first argument el, in this patch I removed it. Was it some planned unfinished feature?

rvagg commented 12 years ago

That hanging m[0] looks like a remnant of something older, it's certainly not necessary now. I've put this in to #50 too.

But regarding the rest, I'm not convinced that there is a clear enough use-case that isn't solved by other methods. .html() is for putting unescaped text into elements, which in some special cases requires some special appendChild() work, but only because of browser oddities.

What I think you're suggesting can already be achieved with any of these variations (and more!):

$('<div id="div1">').appendTo($('body'))
$.create('<div id="div2">').appendTo($('body'))
$('body').append('<div id="div3">')
$('body').append($('<div id="div4">'))
$('body').append($.create('<div id="div5">'))

(This is from Ender with Bonzo + Qwery but they all use Bonzo to do the creating and inserting, the first one is special but only because Qwery knows to pass it on to Bonzo).

zenlor commented 12 years ago

.html() method, before appending clears the element so it's more like:

$('<div if="foo">').appendTo($('body').clear());
$('foo').clear().append($('<div>'))

by the way it's just a small bit of jquery-like compatibility, is not meant to be the right way to achieve it.

benvinegar commented 12 years ago

I'm +1 on this, as I've run into the same situation.

I don't think you should have some functions accept DOM elements (or Bonzo objects), and some not.

nikuda commented 12 years ago

@benvinegar, agreed, not to mention it's conceptually confusing for functions that perform similar tasks. (append(), before(), after() etc...)