diff --git a/.gitignore b/.gitignore index 4befed3..6d84185 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store .idea +node_modules \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b8e1f17 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - 0.6 \ No newline at end of file diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 09c6070..0000000 --- a/LICENSE +++ /dev/null @@ -1,7 +0,0 @@ -Copyright (C) 2012 Adrian Sieber - -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. \ No newline at end of file diff --git a/README.md b/README.md index a1c05b5..40b470b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ +[![Build Status](https://secure.travis-ci.org/microjs/DOMinate.png?branch=master)](https://travis-ci.org/microjs/DOMinate) # DOMinate + A **DOM building utility** and **Template engine** build upon **JsonML** with syntax sugar. ## Features @@ -9,55 +11,54 @@ A **DOM building utility** and **Template engine** build upon **JsonML** with sy ```javascript - DOMinate( - [document.body, - ['h1#logo', 'Static Example', {style:'color:blue'}], - ['p','some example text'], - ['ul', {id:'list', class:'bullets'}, - ['li', 'item1'], - ['li.active', 'item2'], - ['li', - ['a', 'item3', {href: '#'}] - ] - ] - ] - ); +DOMinate( + [document.body, + ['h1#logo', 'Static Example', {style:'color:blue'}], + ['p','some example text'], + ['ul', {id:'list', class:'bullets'}, + ['li', 'item1'], + ['li.active', 'item2'], + ['li', + ['a', 'item3', {href: '#'}] + ] + ] + ] +); ``` compiles to ```html - -

Static Example

-

some example text

- - + +

Static Example

+

some example text

+ + ``` +## SVG support -## Versions -DOMinate is available in three versions, which are based on each other. - -### DOMinate essential -- 272 bytes -- Contains the basic functionality -- Attempt to build the shortest JsonML parser possible -- For projects where every byte counts - -### DOMinate -- 345 bytes -- Standard version of DOMinate which keeps the balance between size and functionality -- Returns a DOM Object -- Syntax Sugar for ids - -### DOMinate extended -- 5k bytes -- Contains all the functionality -- Syntax Sugar for ids and classes -- Support for namespaces. (Lets you build SVGs) +```javascript +DOMinate( + [document.body, + ['svg', {height: 100, width: 100}, + ['circle', {cx: 10, cy: 10, r: 5, style: 'fill:green'}], + ['circle', {cx: 20, cy: 20, r: 5, style: 'fill:red'}] + ] + ], 'http://www.w3.org/2000/svg' +); +``` +compiles to: -**Check out the examples folder for more in-depth examples** \ No newline at end of file +```html + + + + + + +``` \ No newline at end of file diff --git a/bin/compress.sh b/bin/compress.sh deleted file mode 100755 index efb9eda..0000000 --- a/bin/compress.sh +++ /dev/null @@ -1,9 +0,0 @@ -#! /bin/bash - -dir='../src/' - -for a in $(find $dir -name '*.js' ! -name "*.min.js"); -do - name=$(basename "$a") - uglifyjs2 --comments -v -o $dir${name%.js}.min.js "$a" -done \ No newline at end of file diff --git a/component.json b/component.json new file mode 100644 index 0000000..b3b8418 --- /dev/null +++ b/component.json @@ -0,0 +1,14 @@ +{ + "name": "DOMinate", + "repo": "microjs/DOMinate", + "description": "DOMinate the DOM with this simple, yet powerful DOM building utility and template engine.", + "version": "1.0.0", + "keywords": [], + "dependencies": {}, + "development": {}, + "scripts": [ + "index.js" + ], + "twitter": "@ForbesLindesay", + "license": "MIT" +} \ No newline at end of file diff --git a/examples/extended.html b/examples/extended.html index e6d6347..faa8844 100644 --- a/examples/extended.html +++ b/examples/extended.html @@ -3,7 +3,7 @@ DOMinate Extended Static Example - + diff --git a/examples/static.html b/examples/static.html index 23ecb24..d3e53aa 100644 --- a/examples/static.html +++ b/examples/static.html @@ -3,7 +3,7 @@ DOMinate Static Example - + diff --git a/examples/svg.html b/examples/svg.html index 6830a5e..6a99d5f 100644 --- a/examples/svg.html +++ b/examples/svg.html @@ -3,7 +3,7 @@ DOMinate SVG example - + diff --git a/index.js b/index.js new file mode 100644 index 0000000..5e1ea7b --- /dev/null +++ b/index.js @@ -0,0 +1,46 @@ +/** + * Parse a DOMfragment in JsonML and return the resulting DOM element. + * + * @param {Array} DOMfragment Array containing the DOMfragment in JsonML + * @param {String} namespace Optional namespace (defaults to xhtml) + * @return {DOMElement} The resulting DOM element + */ +function DOMinate(DOMfragment, namespace) { + namespace = namespace || 'http://www.w3.org/1999/xhtml'; + + // create DOM element from syntax sugar string (tagName#tagID.classA.classB.classC) + function createElement(sugarString) { + var temp; + + var element = document.createElementNS(namespace, sugarString.match(/^\w+/)[0]); + + if(temp = sugarString.match(/#([\w\-]+)/)) + element.id = temp[1]; + + if(temp = sugarString.match(/\.[\w\-]+/g)) + element.setAttribute('class', temp.join(' ').replace(/\./g, '')); + + return element; + } + + if (typeof DOMfragment[0] == 'string') + DOMfragment[0] = createElement(DOMfragment[0]); + + for (var i = 1; i < DOMfragment.length; i++) { + if (typeof DOMfragment[i] == 'string') { + DOMfragment[0].appendChild(document.createTextNode(DOMfragment[i])); + } else if (typeof DOMfragment[i].pop == 'function') { //if (DOMfragment[i] is an Array) + DOMfragment[0].appendChild(DOMinate(DOMfragment[i], namespace)); + } else { + for (var property in DOMfragment[i]) { + DOMfragment[0].setAttribute(property, DOMfragment[i][property]) + } + } + } + + return DOMfragment[0] +} + +if (typeof module != 'undefined' && typeof module.exports != 'undefined') { + module.exports = DOMinate; +} \ No newline at end of file diff --git a/jstd/domiante.essential.jstd b/jstd/domiante.essential.jstd deleted file mode 100644 index 4899f01..0000000 --- a/jstd/domiante.essential.jstd +++ /dev/null @@ -1,5 +0,0 @@ -load: - - ../src/dominate.essential.js - -test: - - ../test/essential_features.js diff --git a/jstd/dominate.extended.jstd b/jstd/dominate.extended.jstd deleted file mode 100644 index 88e9694..0000000 --- a/jstd/dominate.extended.jstd +++ /dev/null @@ -1,7 +0,0 @@ -load: - - ../src/dominate.extended.js - -test: - - ../test/essential_features.js - - ../test/standard_features.js - - ../test/extended_features.js diff --git a/jstd/dominate.jstd b/jstd/dominate.jstd deleted file mode 100644 index a0fde94..0000000 --- a/jstd/dominate.jstd +++ /dev/null @@ -1,6 +0,0 @@ -load: - - ../src/dominate.js - -test: - - ../test/essential_features.js - - ../test/standard_features.js diff --git a/package.json b/package.json new file mode 100644 index 0000000..8cfa214 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "DOMinate", + "version": "1.0.0", + "description": "DOMinate the DOM with this simple, yet powerful DOM building utility and template engine.", + "main": "index.js", + "directories": { + "example": "examples", + "test": "test" + }, + "scripts": { + "test": "mocha -R spec --globals document" + }, + "repository": { + "type": "git", + "url": "https://github.com/microjs/DOMinate.git" + }, + "author": "", + "license": "MIT", + "devDependencies": { + "mocha": "~1.7.0", + "expect.js": "~0.2.0" + }, + "private": true +} \ No newline at end of file diff --git a/src/dominate.essential.js b/src/dominate.essential.js deleted file mode 100644 index 0b37fca..0000000 --- a/src/dominate.essential.js +++ /dev/null @@ -1,24 +0,0 @@ -/*@preserve DOMinate essential by Adrian Sieber*/ - -DOMinate = function(a, b, c) { - - function d(a) { - return document.createElement(a) - } - - if (a[0].big) - a[0] = d(a[0]); - - for (c = 1; c < a.length; c++) { - if (a[c].big) - a[0].innerHTML = a[c] - - else if (a[c].pop) - a[0].appendChild(a[c][0] = d(a[c][0])), - DOMinate(a[c]) - - else - for (b in a[c]) - a[0].setAttribute(b, a[c][b]) - } -}; \ No newline at end of file diff --git a/src/dominate.essential.min.js b/src/dominate.essential.min.js deleted file mode 100644 index 9bcd2d8..0000000 --- a/src/dominate.essential.min.js +++ /dev/null @@ -1,2 +0,0 @@ -/*@preserve DOMinate essential by Adrian Sieber*/ -DOMinate=function(a,b,c){function d(a){return document.createElement(a)}if(a[0].big)a[0]=d(a[0]);for(c=1;c*/ - /*:DOC ref =
*/ - - DOMinate([this.gen, ['div']]); - - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - }, - - 'test recursive building of Elements': function () { - /*:DOC gen =
*/ - /*:DOC ref =

test2

*/ +describe('essential', function () { + it('attaches to the first element', function () { + var element = document.createElementNS(ns, 'div'); + DOMinate([element, ['div']]); + expect(element.children.length).to.be(1); + expect(element.children[0].__tag).to.be('div'); + expect(element.children[0].__ns).to.be(ns); + }); + it('recursively builds a DOM', function () { + var element = document.createElementNS(ns, 'div'); DOMinate( - [this.gen, + [element, ['div', ['p', 'test', ['a', '2'] @@ -22,16 +23,31 @@ TestCase('Essential', { ] ] ); + expect(element.children.length).to.be(1); - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - }, + element = element.children[0]; + expect(element.__tag).to.be('div'); + expect(element.__ns).to.be(ns); + expect(element.children.length).to.be(1); - 'test setting of Properties': function () { - /*:DOC gen =
*/ - /*:DOC ref =
*/ + element = element.children[0]; + expect(element.__tag).to.be('p'); + expect(element.__ns).to.be(ns); + expect(element.children.length).to.be(2) + var text = element.children[0]; + expect(text.text).to.be('test'); + var a = element.children[1]; + expect(a.__tag).to.be('a'); + expect(a.__ns).to.be(ns); + expect(a.children.length).to.be(1); + expect(a.children[0].text).to.be('2'); + }); + + it('sets properties when given an object', function () { + var element = document.createElementNS(ns, 'div'); DOMinate( - [this.gen, + [element, ['div', { id: 'important', class: 'test', // class is restricted word @@ -39,7 +55,12 @@ TestCase('Essential', { }] ] ); - - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - } + expect(element.children.length).to.be(1); + element = element.children[0]; + expect(element.__tag).to.be('div'); + expect(element.__ns).to.be(ns); + expect(element.id).to.be('important'); + expect(element.className).to.be('test'); + expect(element['data-info']).to.be('none'); + }); }); \ No newline at end of file diff --git a/test/extended_features.js b/test/extended_features.js index b3e2d5b..ce7051c 100644 --- a/test/extended_features.js +++ b/test/extended_features.js @@ -1,33 +1,69 @@ -var reference, a, b, c, - bo = document.body; +var expect = require('expect.js'); +var DOMinate = require('..'); +require('./document-mock'); +var ns = 'http://www.w3.org/1999/xhtml'; -TestCase('Extended', { - 'test syntax-sugar class': function () { - /*:DOC ref =
*/ - /*:DOC gen =
*/ +describe('extended', function () { + describe('syntax-sugar class', function () { + it('works with just a class', function () { + var element = document.createElementNS(ns, 'div'); + DOMinate([element, ['a.new']]); + expect(element.children.length).to.be(1); + expect(element.children[0].__ns).to.be(ns); + expect(element.children[0].__tag).to.be('a'); + expect(element.children[0].className).to.be('new'); + }); + it('works with a class and an id', function () { + var element = document.createElementNS(ns, 'div'); + DOMinate([element, ['a#b.new']]); + expect(element.children.length).to.be(1); + expect(element.children[0].__ns).to.be(ns); + expect(element.children[0].__tag).to.be('a'); + expect(element.children[0].className).to.be('new'); + expect(element.children[0].id).to.be('b'); + }); + it('works with class and id reversed', function () { + var element = document.createElementNS(ns, 'div'); + DOMinate([element, ['a.new#b']]); + expect(element.children.length).to.be(1); + expect(element.children[0].__ns).to.be(ns); + expect(element.children[0].__tag).to.be('a'); + expect(element.children[0].className).to.be('new'); + expect(element.children[0].id).to.be('b'); + }); + }); + describe('namespace support', function () { + describe('when a namespace is passed as the second argument', function () { + it('is used to create the elements', function () { + var svgNS = 'http://www.w3.org/2000/svg'; + var element = document.createElementNS(ns, 'div'); + DOMinate( + [element, + ['svg', {height: 100, width: 100}, + ['circle', {cx: 10, cy: 10, r: 5, style: 'fill:green'}] + ] + ], svgNS + ); + expect(element.__ns).to.be(ns); + expect(element.__tag).to.be('div'); + expect(element.children.length).to.be(1); - DOMinate([this.gen, ['a.new']]); + var svg = element.children[0]; + expect(svg.__ns).to.be(svgNS); + expect(svg.__tag).to.be('svg'); + expect(svg.height).to.be(100); + expect(svg.width).to.be(100); + expect(svg.children.length).to.be(1); - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - }, - - 'test syntax-sugar class and id': function () { - /*:DOC ref =
*/ - /*:DOC gen =
*/ - - DOMinate([this.gen, ['a#b.new']]); - - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - }, - - 'test syntax-sugar class and id reversed': function () { - /*:DOC ref =
*/ - /*:DOC gen =
*/ - - DOMinate([this.gen, ['a.new#c']]); - - console.log(this.gen, this.ref); - - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - } + var circle = svg.children[0]; + expect(circle.__ns).to.be(svgNS); + expect(circle.__tag).to.be('circle'); + expect(circle.cx).to.be(10); + expect(circle.cy).to.be(10); + expect(circle.r).to.be(5); + expect(circle.style).to.be('fill:green'); + expect(circle.children.length).to.be(0); + }); + }); + }); }); \ No newline at end of file diff --git a/test/standard_features.js b/test/standard_features.js index c2d2345..b4dd66f 100644 --- a/test/standard_features.js +++ b/test/standard_features.js @@ -1,16 +1,20 @@ -TestCase('Standard', { - 'test return of DOM element': function () { - /*:DOC gen =
*/ +var expect = require('expect.js'); +var DOMinate = require('..'); +require('./document-mock'); +var ns = 'http://www.w3.org/1999/xhtml'; - assertEquals('Should return a node of type', 1, DOMinate([this.gen, ['a']]).nodeType); - }, - - 'test syntax-sugar id': function () { - /*:DOC gen =
*/ - /*:DOC ref =
*/ - - DOMinate([this.gen, ['div#test']]); - - assertTrue(this.gen.outerHTML + ' == ' + this.ref.outerHTML, this.gen.isEqualNode(this.ref)); - } +describe('standard', function () { + it('returns the node that is the first element of the root array', function () { + var element = document.createElementNS(ns, 'div'); + expect(DOMinate([element, ['a']])).to.be(element); + }); + + it('supports id syntax-sugar', function () { + var element = document.createElementNS(ns, 'div'); + DOMinate([element, ['div#test']]); + expect(element.children.length).to.be(1); + expect(element.children[0].__ns).to.be(ns); + expect(element.children[0].__tag).to.be('div'); + expect(element.children[0].id).to.be('test'); + }); }); \ No newline at end of file