Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Version 1.0.0.
Browse files Browse the repository at this point in the history
This first official version includes major changes such as:
	- Including the latest version of the library minified, instead
		  of the v0.9.1 version.
	- The ability to add styles to elements via the style property
		of the attribute object.
  • Loading branch information
chrisdotcode committed Mar 3, 2016
1 parent 6849c08 commit dfde2a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
21 changes: 0 additions & 21 deletions dist/lmth-v0.9.1.min.js

This file was deleted.

21 changes: 21 additions & 0 deletions dist/lmth.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 20 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

'use strict';

function Node(name, id, class_, attributes, content, children) {
function Node(name, id, class_, style, attributes, content, children) {
if (!(this instanceof Node)) {
throw new Error("Uh-oh. You're creating a new Node without using the `new` keyword. This isn't allowed because Bad Stuff happens when you do that. What you more than likely want is one of the element functions attached to the Node object like so: `lmth.div(...)`. If you actually did want to create a plain node, you should use `Node.new(name, id, class_, attributes, content, children)` and avoid the manual construction with `new` where possible. If you really, really, really want to create a node with `new`, you can do so like this: `new Node(name, class, attributes, content, children)`.");
throw new Error("Uh-oh. You're creating a new Node without using the `new` keyword. This isn't allowed because Bad Stuff happens when you do that. What you more than likely want is one of the element functions attached to the Node object like so: `lmth.div(...)`. If you actually did want to create a plain node, you should use `Node.new(name, id, class, style, attributes, content, children)` and avoid the manual construction with `new` where possible. If you really, really, really want to create a node with `new`, you can do so like this: `new Node(name, class, style, attributes, content, children)`.");
}

this.name = name;
this.id = id;
this.class = class_;
this.style = style;
this.attributes = attributes;
this.content = content;
this.children = children || [];
Expand All @@ -36,8 +37,8 @@ Node.prototype.toString = function toString() {
return JSON.stringify(this);
};

Node.new = function new_(name, id, class_, attributes, content, children) {
return new Node(name, id, class_, attributes, content, children);
Node.new = function new_(name, id, class_, style, attributes, content, children) {
return new Node(name, id, class_, style, attributes, content, children);
};

/* 'Primitive' defined here is a strict subset of all actual JavaScript
Expand Down Expand Up @@ -138,11 +139,13 @@ Node.createElement = function createElement(name) {
var nodeAttributes = parseAttributes(attributes);
var id = nodeAttributes.id;
var class_ = nodeAttributes.class;
var style = nodeAttributes.style;
delete nodeAttributes.id;
delete nodeAttributes.class;
delete nodeAttributes.style;

return Node.new(name, id, class_, nodeAttributes, content,
children);
return Node.new(name, id, class_, style, nodeAttributes,
content, children);
}
};

Expand Down Expand Up @@ -257,6 +260,12 @@ function renderAttribute(key, value) {
}
}

function renderStyle(style) {
return Object.keys(style).reduce(function styler(styleString, property) {
return styleString + property + ':' + style[property] + ';';
}, '');
}

/* Renders a node tree into an HTML string. */
Node.prototype.render = function render() {
var node = this;
Expand All @@ -277,6 +286,7 @@ Node.prototype.render = function render() {
return '<' + node.name +
(node.id ? ' id="' + node.id + '"' : '') +
(node.class.length !== 0 ? ' class="' + node.class.join(' ') + '"' : '') +
(node.style ? ' style="' + renderStyle(node.style) + '"' : '') +
attributes +
'>' +
(Node.voidElements.indexOf(node.name) !== -1 ? '' : childContentsAndClosingTags);
Expand Down Expand Up @@ -313,6 +323,10 @@ Node.prototype.toDOM = function toDOM(document_) {
root.className = node.class.join(' ');
}

if (node.style) {
root.setAttribute('style', renderStyle(node.style));
}

// Explicit coercive comparison because the only non-valid values here
// are `null` and `undefined` (other other falsy values are perfectly
// valid here).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lmth",
"version": "0.9.3",
"version": "1.0.0",
"description": "A \"type-safe\" HTML DSL for JavaScript environments.",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit dfde2a9

Please sign in to comment.