Comparing version
@@ -5,2 +5,3 @@ (function(window) { | ||
var arrayProto = Array.prototype; | ||
var slice = Array.prototype.slice; | ||
@@ -44,3 +45,3 @@ // Used to store callbacks for event delegation (see on() and off()): | ||
container.innerHTML = '' + html; | ||
var els = arrayProto.slice.call(container.childNodes); | ||
var els = slice.call(container.childNodes); | ||
els.forEach(function(el){ container.removeChild(el); }); | ||
@@ -51,13 +52,16 @@ return els; | ||
// 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; | ||
@@ -69,3 +73,3 @@ } | ||
return new $dom.fn.init(query, context); | ||
} | ||
}; | ||
@@ -97,3 +101,3 @@ $dom.fn = $dom.prototype = { | ||
var results = this.qsa(query); | ||
this.results = arrayProto.slice.call(results); | ||
this.results = slice.call(results); | ||
} | ||
@@ -111,3 +115,3 @@ return makeArrayLike(this, this.results); | ||
splice: function() { | ||
var args = arrayProto.slice.call(arguments); | ||
var args = slice.call(arguments); | ||
return arrayProto.splice.apply(this.results, args); | ||
@@ -120,3 +124,3 @@ }, | ||
this.each(function(el) { | ||
var els = arrayProto.slice.call(el.querySelectorAll(subQuery)); | ||
var els = slice.call(el.querySelectorAll(subQuery)); | ||
results = results.concat(els); | ||
@@ -158,2 +162,14 @@ }); | ||
// Return the closest ancestor node that matches the passed in selector | ||
// Note! this currently only works when you pass in a simple tagname selector | ||
closest: function(selector) { | ||
if (!this.length) return newWrapperFromArray([]); | ||
var testEl = this.results[0].parentElement; | ||
while (testEl !== null) { | ||
if (testEl.nodeName === selector.toUpperCase()) return newWrapperFromArray([testEl]); | ||
testEl = testEl.parentElement; | ||
} | ||
return newWrapperFromArray([]); | ||
}, | ||
// Remove each element from the DOM. | ||
@@ -267,3 +283,2 @@ // Note that this doesn't support a query argument like jQuery's remove() does | ||
var useSecondArg = (forceExistence !== undefined); | ||
var toggle = !useSecondArg; | ||
var forceOn = useSecondArg && forceExistence; | ||
@@ -336,3 +351,3 @@ var forceOff = useSecondArg && !forceExistence; | ||
var query = el.querySelectorAll(delegateTo); | ||
candidateEls = candidateEls.concat(arrayProto.slice.call(query)); | ||
candidateEls = candidateEls.concat(slice.call(query)); | ||
}); | ||
@@ -416,3 +431,3 @@ | ||
return this.each(function(el) { | ||
var value = el.dataset[attrName] = attrValue; | ||
el.dataset[attrName] = attrValue; | ||
}); | ||
@@ -432,4 +447,4 @@ } | ||
// 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) { | ||
@@ -444,4 +459,4 @@ newEls.forEach(function(newEl) { | ||
// 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) { | ||
@@ -467,3 +482,3 @@ // Need to reverse the array so that the new elements will be in | ||
toArray: function() { return this.results; } | ||
} | ||
}; | ||
@@ -475,3 +490,3 @@ // Register a callback function for when the document is ready to go. | ||
else window.addEventListener('DOMContentLoaded', fn, false); | ||
} | ||
}; | ||
@@ -478,0 +493,0 @@ $dom.create = function(htmlString) { |
{ | ||
"name": "littledom", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"description": "Small jQuery-like DOM library", | ||
@@ -5,0 +5,0 @@ "main": "littledom.js", |
@@ -11,3 +11,5 @@ [](http://travis-ci.org/af/littledom) | ||
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: | ||
@@ -62,4 +64,4 @@ * ajax() and friends | ||
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 | ||
@@ -71,2 +73,4 @@ // Traversal: | ||
last() -> Returns the last selected element (wrapped) | ||
closest(selector) -> Returns the closest ancestor that matches the given selector | ||
(NOTE! only supports simple tag name selectors for now) | ||
@@ -113,2 +117,3 @@ // Classes and attributes: | ||
.one() | ||
.closest(), .children() | ||
.children() | ||
* form serialization: to string, and to Object |
@@ -14,2 +14,3 @@ var $dom = window.$dom; // For jshint. | ||
var testHtml = document.querySelector('#test_elements').innerHTML; | ||
var $root = $dom('#test_elements'); | ||
@@ -469,4 +470,2 @@ beforeEach(function() { | ||
describe('append()', function() { | ||
var $root = $dom('#test_elements'); | ||
afterEach(function() { | ||
@@ -492,2 +491,10 @@ // Clean up inserted elements | ||
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() { | ||
@@ -521,7 +528,19 @@ var $newEls = $dom('<div class="append_el">hi</div>'); | ||
}); | ||
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() { | ||
var $root = $dom('#test_elements'); | ||
afterEach(function() { | ||
@@ -557,2 +576,17 @@ // Clean up inserted elements | ||
}); | ||
describe('closest()', function() { | ||
it('works when you pass in a tag name', function() { | ||
var $el = $root.find('li').first(); | ||
var $parent = $root.find('ol'); | ||
assertEqual($el.closest('ol')[0], $parent[0]); | ||
assertEqual($el.closest('ol').length, 1); | ||
assertEqual($el.closest('asdf')[0], undefined); | ||
}); | ||
it('returns nothing when called on an empty selection', function() { | ||
assertEqual($dom('asdfasdf').closest('div').length, 0); | ||
}); | ||
}); | ||
}); |
142872
1.98%4757
1.34%116
4.5%