Permalink
Browse files

change to return an array at all times. Closes #7

fuck it, no point getting all complex in here, if users dont want multiple, let
them throw. I havent tested with text nodes at the root level, that likely fails right now
since im using .children but I dont need it for any of my cases yet
  • Loading branch information...
1 parent 8432fa0 commit 84e4992c1f847de7c18328d71f76ac8867d6a807 @tj tj committed Nov 24, 2012
Showing with 58 additions and 72 deletions.
  1. +3 −24 Readme.md
  2. +33 −11 index.js
  3. +22 −37 test/domify.js
View
@@ -4,8 +4,8 @@
Turn HTML into DOM elements x-browser.
```js
-domify('<option>Hello</option>')
-// => <option>Hello</option>
+domify('<p>Hello</p><p>World</p>')
+// => [<p>Hello</p><p>World</p>]
```
## Running tests
@@ -18,25 +18,4 @@ $ open test/index.html
## License
-(The MIT License)
-
-Copyright (c) 2012 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-'Software'), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ MIT
View
@@ -1,4 +1,10 @@
+/**
+ * Expose `parse`.
+ */
+
+module.exports = parse;
+
/**
* Wrap map from jquery.
*/
@@ -20,26 +26,28 @@ var map = {
};
/**
- * Convert the given `html` into DOM elements.
+ * Parse `html` and return the children.
*
- * @api public
+ * @param {String} html
+ * @return {Array}
+ * @api private
*/
-module.exports = function(html){
+function parse(html) {
if ('string' != typeof html) throw new TypeError('String expected');
-
+
// tag name
var m = /<([\w:]+)/.exec(html);
if (!m) throw new Error('No elements were generated.');
var tag = m[1];
-
+
// body support
if (tag == 'body') {
var el = document.createElement('html');
el.innerHTML = html;
- return el.removeChild(el.lastChild);
+ return [el.removeChild(el.lastChild)];
}
-
+
// wrap map
var wrap = map[tag] || map._default;
var depth = wrap[0];
@@ -49,9 +57,23 @@ module.exports = function(html){
el.innerHTML = prefix + html + suffix;
while (depth--) el = el.lastChild;
- if (el.lastChild.nextElementSibling || el.lastChild.previousElementSibling) {
- throw new Error('More than one element was generated.');
+ return orphan(el.children);
+}
+
+/**
+ * Orphan `els` and return an array.
+ *
+ * @param {NodeList} els
+ * @return {Array}
+ * @api private
+ */
+
+function orphan(els) {
+ var ret = [];
+
+ while (els.length) {
+ ret.push(els[0].parentNode.removeChild(els[0]));
}
- return el.removeChild(el.lastChild);
-};
+ return ret;
+}
View
@@ -3,105 +3,90 @@ var domify = require('domify');
describe('domify(html)', function(){
it('should convert HTML to DOM elements', function(){
- var el = domify('<p>Hello</p>');
+ var el = domify('<p>Hello</p>')[0];
assert('P' == el.nodeName);
assert('Hello' == el.textContent);
+
+ var els = domify('<p>one</p><p>two</p><p>three</p>');
+ console.log(els);
+ assert('one' == els[0].textContent);
+ assert('two' == els[1].textContent);
+ assert('three' == els[2].textContent);
})
it('should support body tags', function(){
- var el = domify('<body></body>');
+ var el = domify('<body></body>')[0];
assert('BODY' == el.nodeName);
})
it('should support body tags with classes', function(){
- var el = domify('<body class="page"></body>');
+ var el = domify('<body class="page"></body>')[0];
assert('BODY' == el.nodeName);
assert('page' == el.className);
})
it('should support legend tags', function(){
- var el = domify('<legend>Hello</legend>');
+ var el = domify('<legend>Hello</legend>')[0];
assert('LEGEND' == el.nodeName);
})
it('should support table tags', function(){
- var el = domify('<table></table>');
+ var el = domify('<table></table>')[0];
assert('TABLE' == el.nodeName);
})
it('should support thead tags', function(){
- var el = domify('<thead></thead>');
+ var el = domify('<thead></thead>')[0];
assert('THEAD' == el.nodeName);
})
it('should support tbody tags', function(){
- var el = domify('<tbody></tbody>');
+ var el = domify('<tbody></tbody>')[0];
assert('TBODY' == el.nodeName);
})
it('should support tfoot tags', function(){
- var el = domify('<tfoot></tfoot>');
+ var el = domify('<tfoot></tfoot>')[0];
assert('TFOOT' == el.nodeName);
})
it('should support caption tags', function(){
- var el = domify('<caption></caption>');
+ var el = domify('<caption></caption>')[0];
assert('CAPTION' == el.nodeName);
})
it('should support col tags', function(){
- var el = domify('<col></col>');
+ var el = domify('<col></col>')[0];
assert('COL' == el.nodeName);
})
it('should support td tags', function(){
- var el = domify('<td></td>');
+ var el = domify('<td></td>')[0];
assert('TD' == el.nodeName);
})
it('should support th tags', function(){
- var el = domify('<th></th>');
+ var el = domify('<th></th>')[0];
assert('TH' == el.nodeName);
})
it('should support tr tags', function(){
- var el = domify('<tr></tr>');
+ var el = domify('<tr></tr>')[0];
assert('TR' == el.nodeName);
})
it('should support option tags', function(){
- var el = domify('<option></option>');
+ var el = domify('<option></option>')[0];
assert('OPTION' == el.nodeName);
})
it('should support optgroup tags', function(){
- var el = domify('<optgroup></optgroup>');
+ var el = domify('<optgroup></optgroup>')[0];
assert('OPTGROUP' == el.nodeName);
})
- it('should throw if more than one tag is given', function(){
- var thrown = null;
- try {
- domify('<div></div><span></span>');
- } catch (err) {
- thrown = err;
- }
-
- assert(thrown && /more than one element/i.test(thrown.message));
- })
-
- it('should throw if no tag is given', function(){
- var thrown = null;
- try {
- domify(' ');
- } catch (err) {
- thrown = err;
- }
-
- assert(thrown && /no elements/i.test(thrown.message));
- })
it('should not set parentElement', function() {
- var el = domify('<p>Hello</p>');
+ var el = domify('<p>Hello</p>')[0];
assert(!el.parentElement);
assert(!el.parentNode);
})

4 comments on commit 84e4992

Owner

timoxley replied Nov 24, 2012

hahah +1 this is going to break a lot of components

Owner

tj replied Nov 24, 2012

haha yup, might as well get it over with. .one() and .many() just seems awkward

What about returning a DocumentFragment instead?

Owner

tj replied Nov 28, 2012

yeah might as well

Please sign in to comment.