aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-03-29 22:57:54 +0000
committerMichael Williamson <mike@zwobble.org>2013-03-29 22:57:54 +0000
commite51abdc358cef3655f074f4ab0e6000623ce4749 (patch)
tree1982623108807c54df0788c6738cc8e8aa48fed3
parent370a18d55e1cf97284088c5a1815f9f59ea0bcd7 (diff)
Allow styles to have CSS class
-rw-r--r--lib/html-generator.js13
-rw-r--r--lib/styles.js3
-rw-r--r--tests/html-generator.tests.js9
3 files changed, 20 insertions, 5 deletions
diff --git a/lib/html-generator.js b/lib/html-generator.js
index 8a67e53..7a03570 100644
--- a/lib/html-generator.js
+++ b/lib/html-generator.js
@@ -31,22 +31,27 @@ function HtmlGenerator() {
pop();
}
for (; partIndex < style.length; partIndex++) {
- push(style[partIndex].tagName);
+ push(style[partIndex]);
}
}
function stylePartMatchesHtmlElement(stylePart, htmlElement) {
- return stylePart.tagName === htmlElement;
+ return htmlElement && stylePart.tagName === htmlElement.tagName;
}
function push(element) {
stack.push(element);
- fragments.push(util.format("<%s>", element));
+ if (element["class"]) {
+ var attributeString = util.format(' %s="%s"', "class", element["class"]);
+ } else {
+ var attributeString = "";
+ }
+ fragments.push(util.format("<%s%s>", element.tagName, attributeString));
}
function pop() {
var element = stack.pop();
- fragments.push(util.format("</%s>", element));
+ fragments.push(util.format("</%s>", element.tagName));
}
function popAll() {
diff --git a/lib/styles.js b/lib/styles.js
index 06d63a8..8120a76 100644
--- a/lib/styles.js
+++ b/lib/styles.js
@@ -22,6 +22,7 @@ function element(tagName, options) {
options = options || {};
return {
tagName: tagName,
- fresh: options.fresh
+ fresh: options.fresh,
+ "class": options["class"]
};
}
diff --git a/tests/html-generator.tests.js b/tests/html-generator.tests.js
index a98ee70..f71ea59 100644
--- a/tests/html-generator.tests.js
+++ b/tests/html-generator.tests.js
@@ -36,4 +36,13 @@ describe('HtmlGenerator', function() {
generator.text("Banana");
return assert.equal(generator.asString(), "<ul><li>Apple</li><li>Banana</li></ul>");
});
+
+ test('renders class attribute of elements', function() {
+ var generator = new HtmlGenerator();
+ generator.style(styles.elements([
+ styles.element("p", {"class": "tip"})
+ ]));
+ generator.text("Hello!");
+ return assert.equal(generator.asString(), '<p class="tip">Hello!</p>');
+ });
})