Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 0e722939f8679bea04d57ba506b3d84a41266916 0 parents
@tj tj authored
2  .gitignore
@@ -0,0 +1,2 @@
+node_modules
+test/build.*
3  .gitmodules
@@ -0,0 +1,3 @@
+[submodule "test/mocha"]
+ path = test/mocha
+ url = git://github.com/visionmedia/mocha.git
4 .npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
5 History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
5 Makefile
@@ -0,0 +1,5 @@
+
+build:
+ component build package.json test/build
+
+.PHONY: build
42 Readme.md
@@ -0,0 +1,42 @@
+
+# domify
+
+ Turn HTML into DOM elements x-browser.
+
+```js
+domify('<option>Hello</option>')
+// => <option>Hello</option>
+```
+
+## Running tests
+
+```
+$ git submodule update --init
+$ make
+$ 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.
2  index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/domify');
39 lib/domify.js
@@ -0,0 +1,39 @@
+
+/**
+ * Wrap map from jquery.
+ */
+
+var map = {
+ option: [1, '<select multiple="multiple">', '</select>'],
+ optgroup: [1, '<select multiple="multiple">', '</select>'],
+ legend: [1, '<fieldset>', '</fieldset>'],
+ thead: [1, '<table>', '</table>'],
+ tbody: [1, '<table>', '</table>'],
+ tfoot: [1, '<table>', '</table>'],
+ colgroup: [1, '<table>', '</table>'],
+ caption: [1, '<table>', '</table>'],
+ tr: [2, '<table><tbody>', '</tbody></table>'],
+ td: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
+ th: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
+ col: [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
+ _default: [0, '', '']
+};
+
+/**
+ * Convert the given `html` into DOM elements.
+ *
+ * @api public
+ */
+
+module.exports = function(html){
+ html = String(html);
+ var tag = /<([\w:]+)/.exec(html)[1];
+ var wrap = map[tag] || map._default;
+ var depth = wrap[0];
+ var prefix = wrap[1];
+ var suffix = wrap[2];
+ var el = document.createElement('div');
+ el.innerHTML = prefix + html + suffix;
+ while (depth--) el = el.lastChild;
+ return el.lastChild;
+};
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "domify"
+ , "version": "0.0.1"
+ , "description": "turn HTML into DOM elements"
+ , "keywords": ["dom", "html", "client", "browser", "component"]
+ , "author": "TJ Holowaychuk <tj@vision-media.ca>"
+ , "dependencies": {}
+ , "component": {
+ "scripts": {
+ "domify": "lib/domify.js"
+ }
+ }
+ , "main": "index"
+}
70 test/domify.js
@@ -0,0 +1,70 @@
+
+var domify = require('domify');
+
+describe('domify(html)', function(){
+ it('should convert HTML to DOM elements', function(){
+ var el = domify('<p>Hello</p>');
+ assert('P' == el.nodeName);
+ assert('Hello' == el.textContent);
+ })
+
+ it('should support legend tags', function(){
+ var el = domify('<legend>Hello</legend>');
+ assert('LEGEND' == el.nodeName);
+ })
+
+ it('should support table tags', function(){
+ var el = domify('<table></table>');
+ assert('TABLE' == el.nodeName);
+ })
+
+ it('should support thead tags', function(){
+ var el = domify('<thead></thead>');
+ assert('THEAD' == el.nodeName);
+ })
+
+ it('should support tbody tags', function(){
+ var el = domify('<tbody></tbody>');
+ assert('TBODY' == el.nodeName);
+ })
+
+ it('should support tfoot tags', function(){
+ var el = domify('<tfoot></tfoot>');
+ assert('TFOOT' == el.nodeName);
+ })
+
+ it('should support caption tags', function(){
+ var el = domify('<caption></caption>');
+ assert('CAPTION' == el.nodeName);
+ })
+
+ it('should support col tags', function(){
+ var el = domify('<col></col>');
+ assert('COL' == el.nodeName);
+ })
+
+ it('should support td tags', function(){
+ var el = domify('<td></td>');
+ assert('TD' == el.nodeName);
+ })
+
+ it('should support th tags', function(){
+ var el = domify('<th></th>');
+ assert('TH' == el.nodeName);
+ })
+
+ it('should support tr tags', function(){
+ var el = domify('<tr></tr>');
+ assert('TR' == el.nodeName);
+ })
+
+ it('should support option tags', function(){
+ var el = domify('<option></option>');
+ assert('OPTION' == el.nodeName);
+ })
+
+ it('should support optgroup tags', function(){
+ var el = domify('<optgroup></optgroup>');
+ assert('OPTGROUP' == el.nodeName);
+ })
+})
22 test/index.html
@@ -0,0 +1,22 @@
+<html>
+ <head>
+ <title>domify tests</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <link rel="stylesheet" href="mocha/mocha.css" />
+ <script src="mocha/mocha.js"></script>
+ <script>mocha.setup('bdd')</script>
+ <script>
+ function assert(expr, msg) {
+ if (!expr) throw new Error(msg || 'failed');
+ }
+ </script>
+ </head>
+ <body>
+ <div id="mocha"></div>
+ <script src="build.js"></script>
+ <script src="domify.js"></script>
+ <script>
+ mocha.run();
+ </script>
+ </body>
+</html>
1  test/mocha
@@ -0,0 +1 @@
+Subproject commit 885fb12f6e1c3e510afc9e3b8c7f232ea587492e
Please sign in to comment.
Something went wrong with that request. Please try again.