Skip to content

Commit

Permalink
Get $dom.fn.append() working when $dom instances are passed in.
Browse files Browse the repository at this point in the history
  • Loading branch information
af committed Sep 13, 2013
1 parent dd2e001 commit 5ab6866
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
10 changes: 9 additions & 1 deletion littledom.js
Expand Up @@ -402,8 +402,16 @@
},

// Append html to the end of all of the matched elements.
// Accepts a string of html, or a $dom instance.
append: function(contentToInsert) {
var newEls = createElementsFromHtmlString(contentToInsert);
var newEls = [];
if (!contentToInsert) return;

if (contentToInsert instanceof $dom) {
newEls = contentToInsert.toArray();
} else {
newEls = createElementsFromHtmlString(contentToInsert);
}
return this.each(function(selectedEl) {
newEls.forEach(function(newEl) {
selectedEl.appendChild(newEl);
Expand Down
34 changes: 28 additions & 6 deletions test/tests.js
Expand Up @@ -462,22 +462,44 @@ describe('$dom', function() {
});

describe('append()', function() {
var $root = $dom('#test_elements');

afterEach(function() {
// Clean up inserted elements
$dom('#test_elements div.append_el').remove();
$root.find('div.append_el').remove();
});

it('accepts an html string', function() {
$dom('#test_elements').append('<div class="append_el">hi</div>');
var $inserted = $dom('#test_elements div.append_el');
$root.append('<div class="append_el">hi</div>');
var $inserted = $root.find('div.append_el');
assertEqual($inserted.length, 1);
assertEqual($inserted.html(), 'hi');
});

it('accepts an html string with multiple elements', function() {
$dom('#test_elements').append('<div class="append_el">one</div> <div class="append_el">two</div>');
assertEqual($dom('#test_elements div.append_el').length, 2);
assertEqual($dom('#test_elements div.append_el').html(), 'one'); // Returns the first result
$root.append('<div class="append_el">one</div> <div class="append_el">two</div>');

var $inserted = $root.find('div.append_el');
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);

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

it('accepts a $dom object with multiple elements and appends them', function() {
var $newEls = $dom('<div class="append_el">one</div> <div class="append_el">two</div>');
$root.append($newEls);

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

0 comments on commit 5ab6866

Please sign in to comment.