Skip to content

Commit

Permalink
prepend()/append(): add support for multiple arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
af committed Jan 31, 2014
1 parent 1e5bf89 commit a8e1d75
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -8,7 +8,9 @@ as a curated jQuery subset, optimized for readability and file size.

littledom is *not* a drop-in replacement for jQuery, but it does implement many of its
most commonly used methods. In many cases, not all method signatures are supported either.
This keeps the code lean and pretty simple to read. The following parts of jQuery are intentionally omitted- if you need them you might consider a best-of-breed standalone library instead:
This keeps the code lean and pretty simple to read. The following parts of jQuery
are intentionally omitted- if you need them you might consider a best-of-breed
standalone library instead:

* ajax() and friends
* promises
Expand Down Expand Up @@ -58,8 +60,8 @@ API
remove() -> remove all matched elements from the document (doesn't take any arguments)
data(attrName) -> return the value of the given data-* attribute (for the first matched element where it's set)
data(attrName, val) -> set data-attrName to the given value for all matched elements
append(content) -> Append content ($dom instance, DOM node, or html string) to each element
prepend(content) -> Same as append(), but inserts at the front of the selection
append(content, ...) -> Append content ($dom instances, DOM nodes, or html strings) to each element
prepend(content, ...) -> Same as append(), but inserts at the front of the selection
// Traversal:
parent() -> Returns all unique parents of the selected elements
Expand Down Expand Up @@ -110,3 +112,4 @@ API
* Add support for:
.one()
.children()
* form serialization: to string, and to Object
31 changes: 17 additions & 14 deletions littledom.js
Expand Up @@ -47,17 +47,20 @@
};

// Accepts an element, string, or $dom object, and returns an array of dom elements
function contentToArray(contentToInsert) {
function contentToArray(contentArray) {
if (!contentArray || !contentArray.length) return [];

var newEls = [];
if (!contentToInsert) {
newEls = [];
} else if (contentToInsert instanceof HTMLElement) {
newEls = [contentToInsert];
} else if (contentToInsert instanceof $dom) {
newEls = contentToInsert.toArray();
} else {
newEls = createElementsFromHtmlString(contentToInsert);
}
contentArray.forEach(function(contentToInsert) {
if (!contentToInsert) return;
if (contentToInsert instanceof HTMLElement) {
newEls.push(contentToInsert);
} else if (contentToInsert instanceof $dom) {
newEls = newEls.concat(contentToInsert.toArray());
} else {
newEls = newEls.concat(createElementsFromHtmlString(contentToInsert));
}
});
return newEls;
}

Expand Down Expand Up @@ -430,8 +433,8 @@

// Append html to the end of all of the matched elements.
// Accepts a string of html, a DOM node, or a $dom instance.
append: function(contentToInsert) {
var newEls = contentToArray(contentToInsert);
append: function() {
var newEls = contentToArray(slice.call(arguments));
return this.each(function(selectedEl) {
newEls.forEach(function(newEl) {
selectedEl.appendChild(newEl);
Expand All @@ -441,8 +444,8 @@

// Prepend html at the beginning of all of the matched elements.
// Accepts a string of html, a DOM node, or a $dom instance.
prepend: function(contentToInsert) {
var newEls = contentToArray(contentToInsert);
prepend: function() {
var newEls = contentToArray(slice.call(arguments));
return this.each(function(selectedEl) {
// Need to reverse the array so that the new elements will be in
// the order that they were passed in (since they're prepended in order):
Expand Down
22 changes: 22 additions & 0 deletions test/tests.js
Expand Up @@ -486,6 +486,14 @@ describe('$dom', function() {
assertEqual($inserted.html(), 'one'); // Returns the first element's content
});

it('accepts multiple strings with elements', function() {
$root.append('<div class="xyz">one</div>', '<div class="xyz">two</div>');

var $inserted = $root.find('div.xyz');
assertEqual($inserted.length, 2);
assertEqual($inserted.html(), 'one'); // Returns the first element's content
});

it('accepts a $dom object with one element and appends its element', function() {
var $newEls = $dom('<div class="append_el">hi</div>');
$root.append($newEls);
Expand Down Expand Up @@ -514,6 +522,20 @@ describe('$dom', function() {
assertEqual($inserted.length, 1);
assertEqual($inserted.html(), 'hi');
});

it('accepts multiple DOM nodes and appends them', function() {
var div1 = document.createElement('div');
div1.className = 'append_el';
div1.textContent = 'hi';
var div2 = document.createElement('div');
div2.className = 'append_el';
div2.textContent = 'hi';
$root.append(div1, div2);

var $inserted = $root.find('.append_el');
assertEqual($inserted.length, 2);
assertEqual($inserted.html(), 'hi');
});
});

describe('prepend()', function() {
Expand Down

0 comments on commit a8e1d75

Please sign in to comment.