Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ logs
*.log
npm-debug.log*

package-lock.json
# Runtime data
pids
*.pid
Expand Down
50 changes: 43 additions & 7 deletions lib/h.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* @function
* @access public
*/

var xmlns = "http://www.w3.org/2000/svg";

var H = function (argv) {
if (!(this instanceof H)) {
if (!(argv instanceof H)) {
Expand Down Expand Up @@ -89,23 +92,35 @@ H.prototype.render = function (node, parent) {
return node.el;
};

H.prototype.setAttribute = function(name, value) {
try {
if (this.el instanceof window.SVGElement) {
this.el.setAttributeNS(null, name, value);
} else {
this.el.setAttribute(name, value);
}
} catch(e) {
this.el.setAttribute(name, value);
}
};

H.prototype.setProp = function (name, value) {
if (typeof this.el !== 'undefined') {
if (name === 'className') {
this.el.setAttribute('class', value);
this.setAttribute('class', value);
} else if (name === 'style' && typeof value !== 'string') {
this.el.setAttribute('style', _stylePropToString(value));
this.setAttribute('style', _stylePropToString(value));
} else if (name.match(/^on/)) {
this.addEvent(name, value);
} else if (name === 'ref') {
if (typeof value === 'function') {
value(this.el);
}
} else if (typeof value === 'boolean' || value === 'true') {
this.el.setAttribute(name, value);
this.setAttribute(name, value);
this.el[name] = Boolean(value);
} else {
this.el.setAttribute(name, value);
this.setAttribute(name, value);
}
}

Expand Down Expand Up @@ -227,9 +242,30 @@ var _createTextNode = function (text) {
};

var createElement = function (node, parent) {
node.el = node.tag === 'text'
? document.createTextNode(node.children)
: document.createElement(node.tag);
// node.el = node.tag === 'text'
// ? document.createTextNode(node.children)
// : document.createElement(node.tag);


switch (node.tag) {
case 'text':
node.el = document.createTextNode(node.children);
break;
case 'svg':
case 'g':
case 'circle':
case 'ellipse':
case 'line':
case 'path':
case 'polygon':
case 'polyline':
case 'rect':
node.el = document.createElementNS(xmlns, node.tag);
break;
default:
node.el = document.createElement(node.tag);
break;
}

if (typeof node.props !== 'undefined') {
node.setProps(node.props);
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "basic-virtual-dom",
"version": "0.3.3",
"version": "0.4.0",
"description": "Basic virtual dom implementation",
"main": "index.js",
"files": [
Expand All @@ -10,14 +10,13 @@
"LICENSE"
],
"directories": {},
"dependencies": {},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.15",
"eslint": "^3.11.1",
"jsdom": "^9.8.3",
"eslint": "^3.19.0",
"jsdom": "^9.12.0",
"mocha": "^3.2.0",
"mocha-jsdom": "^1.1.0",
"mocha-jsdom": "~1.1.0",
"nyc": "^10.0.0",
"sinon": "^2.1.0"
},
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/svg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var h = require('../../').h;

exports.a =
h('svg', { id: 'app'},
h(
'g',
null,
h('rect', { x: '40', y: '80', fill: 'blue' }),
h('rect', { x: '40', y: '80', fill: 'red' }),
h('rect', { x: '40', y: '80', fill: 'green' })
)
);

exports.a =
h('svg', { id: 'app'},
h(
'g',
null,
h('rect', { x: '40', y: '80', fill: 'blue' }),
h('text', { x: '40', y: '80' }, 'text node')
)
);


24 changes: 24 additions & 0 deletions tests/mocha-tests/svg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var expect = require('chai').expect;
var sinon = require('sinon');
var jsdom = require('mocha-jsdom');

var a = require('../fixtures/svg').a;
var d = require('../fixtures/svg').d;
var h = require('../../').h;

describe('Test <svg>', function() {
jsdom();

it('should create svg tree', function() {
expect(a).to.be.exists;
expect(a).to.be.an('object');
expect(a.tag).to.be.equal('svg');
expect(a.children).to.be.an('array');
});

it('should create empty svg node', function() {
var empty = h('svg', null, '');
expect(empty.tag).to.be.equal('svg');
});

});