-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding prepend function on metal-dom #306
Changes from 6 commits
4e18cbe
0dff329
305507b
42a7da4
6a41424
aac2458
70ed4b5
b7291a0
3f57d76
bc83265
321e335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -521,6 +521,34 @@ export function parent(element, selector) { | |
return closest(element.parentNode, selector); | ||
} | ||
|
||
/** | ||
* Inserts a node before first child of the parent. If child is a HTML string | ||
* it will be converted to document fragment before prepending it to the parent. | ||
* @param {!Element} parent The node to prepend to. | ||
* @param {!(Element|NodeList|string)} child The thing who must be prepended | ||
* @return {!Element} The prepended child | ||
*/ | ||
export function prepend(parent, child) { | ||
if (isString(child)) { | ||
child = buildFragment(child); | ||
} | ||
|
||
if (!isNodeListLike(child) && !isDefAndNotNull(parent.firstChild)) { | ||
parent.appendChild(child); | ||
} | ||
|
||
if (isNodeListLike(child)) { | ||
const childArr = Array.prototype.slice.call(child); | ||
for (let i = 0; i < childArr.length; i++) { | ||
parent.appendChild(childArr[i]); | ||
} | ||
} else { | ||
parent.insertBefore(child, parent.firstChild); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When calling this on a parent that has no children, this block will run even though line 537 has already added the child. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @Robert-Frampton , what do you think that function return parent? User probably can get info about childNodes easily, instead actually returning child. I've sent a commit that fixes the problem on running though and returns parent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @diegonvs, we should probably keep it the same as the |
||
} | ||
|
||
return child; | ||
} | ||
|
||
/** | ||
* Registers a custom event. | ||
* @param {string} eventName The name of the custom event. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add a node list to a parent that already has a child this won't work, this test will currently fail as an example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've corrected this using insertBefore instead appendChild