use document fragment for multiple top level elements #18

Merged
merged 1 commit into from Jun 13, 2013

5 participants

@defunctzombie
Components member

When a string of multiple top level nodes is present, create a document
fragment to contain them.

<p>foo</p><p>bar</p> will result in a document fragment

@defunctzombie defunctzombie return document fragments for multiple top level nodes
When a string of multiple top level nodes is present, create a document
fragment to contain them.

<p>foo</p><p>bar</p> will result in a document fragment
e098182
@defunctzombie
Components member

Why not? this way you always get back one thing which you can add to the dom using appendChild. In the simple case where you only created one thing it will be exactly what you expect without some additional array stuff.

@jonathanong

you already always get back one thing that you expect: an array

it's also not consistent. i don't want to check what is returned each time to see whether it's a document fragment or not. easier just to loop through an array.

if you want to be consistent, always return elements in a document fragment

but I don't like this API because 1) it breaks backwards compatibility and 2) it's not what jQuery does, which a lot of us are used to (though this doesn't necessarily make it the best API)

@jonathanong

actually, an option would be nice.

domify(html) -> [] for backwards compatibilty
domify(html, true) -> documentFragment if you just want to append it.

but i don't think it should ever return just a single element.

@jb55

I pretty much always end up looping over the array to insert each element into a document fragment before I insert it into the DOM. When would you ever need an array? For an api that turns a string into dom elements a document fragment is the logical choice. The other way encourages poor use of DOM apis (multiple appendChild calls).

The documentFragment return option would be nice.

@jonathanong

I process all the elements in a loop before putting them in a document fragment then appending it.

I'm okay with returning document fragments, but this pr does it incorrectly. It should always return document fragments, not only if there are more than one elements.

@defunctzombie
Components member

Why not return single element in all cases? If we can return that element, great, if not, then return dom fragment?

@jonathanong

because i don't want to check the type of the result.

@jb55
@jkroso

+1 for returning a single element. I know my input string

@defunctzombie
Components member

You will know the type of the result. If you just did one element, it will be that. If you did multiple, it will be a fragment. You can get elements out of a fragment easily (no worse than some array) and for simple case of single element you will have that element.

I think you guys are focusing too much on some sort of "purity" versus the actual use cases.

@jonathanong

You will know the type of the result. If you just did one element, it will be that. If you did multiple, it will be a fragment.

You assume I know the input HTML string before passing it into domify. I don't. In my case, I render my HTML server-side and push it to the client.

@jkroso

sounds like we need two functions

@tj
Components member
tj commented Jun 13, 2013

LGTM

@tj tj merged commit 1c18f76 into component:master Jun 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment