Skip to content

Commit

Permalink
fix .html(str)
Browse files Browse the repository at this point in the history
- add tests for setting html string
  • Loading branch information
defunctzombie committed Jan 6, 2013
1 parent 36e62a6 commit 1d09d43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ proto.html = function(val){
var el = this[0];

if (val) {
el.innerHTML = str;
if (typeof(val) !== 'string') {
throw new Error('.html() requires a string');
}

el.innerHTML = val;
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion test/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ test('trim selector', function() {
assert(' foo ' == li.text());
});

// create from html
test('html', function() {
var list = dom('<em>Hello</em>');
assert('Hello' == list[0].textContent);
});

// create multiple html elements
test('multiple html', function() {
var list = dom('<em>Hello</em><em>World</em>');
assert.equal(2, list.length);
Expand Down Expand Up @@ -159,4 +161,3 @@ test('.attr(key)', function() {
});


// setting html by: string, array of string? array of List
25 changes: 25 additions & 0 deletions test/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require('assert');
var dom = require('../');

suite('.html');

test('.html(str)', function() {
var div = dom('<div>');

// setting html should work
div.html('<p>foo</p>');
assert.equal(div.html(), '<p>foo</p>');

// make sure that we can find in new html
assert.equal(div.find('p').text(), 'foo');
});

test('.html(str) - override', function() {
var div = dom('<div><p>foo</div>');

// override old html
div.html('bar');
assert.equal(div.html(), 'bar');
assert.equal(div.text(), 'bar');
});

0 comments on commit 1d09d43

Please sign in to comment.