Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Completed test coverage.

  • Loading branch information...
commit 53e4dc1cd03354ac64952f6d0adb56deed513e1f 1 parent 7149bab
@vail130 authored
View
41 lib/wieldymarkup.js
@@ -139,13 +139,9 @@
this.processCurrentLevel = __bind(this.processCurrentLevel, this);
- this.compile = __bind(this.compile, this);
-
this.text = text;
this.compress = compress;
- if (this.text !== "") {
- this.compile();
- }
+ this.compile();
}
WieldyMarkup.prototype.compile = function(text, compress) {
@@ -209,8 +205,8 @@
WieldyMarkup.prototype.closeTag = function() {
var closingTagTuple;
closingTagTuple = this.openTags.pop();
- if (!this.compress) {
- output += _.str.repeat(this.indentToken, closingTagTuple[0]);
+ if (!this.compress && closingTagTuple[0] > 0) {
+ this.output += _.str.repeat(this.indentToken, closingTagTuple[0]);
}
this.output += "</" + closingTagTuple[1] + ">";
if (!this.compress) {
@@ -220,12 +216,12 @@
};
WieldyMarkup.prototype.processNextLine = function() {
- var line, lineBreakIndex, lineSplitList, lines, restOfLine, selector;
+ var line, lineBreakIndex, lineSplitList, restOfLine, selector, temp_line;
this.lineStartsWithTick = false;
this.selfClosing = false;
this.innerText = null;
line = "";
- if (this.text.indexOf("\n") > -1) {
+ if (__indexOf.call(this.text, "\n") >= 0) {
lineBreakIndex = this.text.indexOf("\n");
line = _.str.trim(this.text.substring(0, lineBreakIndex));
this.text = this.text.substring(lineBreakIndex + 1, this.text.length);
@@ -240,19 +236,12 @@
if (line[0] === this.embeddingToken) {
this.processEmbeddedLine(line);
} else {
- while (true) {
- lineSplitList = line.split('\\-\\');
- lines = [lineSplitList[0]];
- if (lineSplitList.length === 1) {
- line = _.str.trim(lineSplitList[0]);
- break;
- } else {
- lines.push(_.str.join('\\-\\', lineSplitList.substring(1, lineSplitList.length)));
- }
- lines[0] = _.str.trim(lines[0]);
- selector = this.constructor.getSelectorFromLine(lines[0]);
+ lineSplitList = line.split('\\-\\');
+ while (lineSplitList.length > 1) {
+ temp_line = _.str.trim(lineSplitList.shift());
+ selector = this.constructor.getSelectorFromLine(temp_line);
this.processSelector(selector);
- restOfLine = _.str.trim(lines[0].substring(selector.length, lines[0].length));
+ restOfLine = _.str.trim(temp_line.substring(selector.length, temp_line.length));
restOfLine = this.processAttributes(restOfLine);
this.addHtmlToOutput();
this.tag = null;
@@ -261,8 +250,8 @@
this.tagAttributes = [];
this.previousLevel = this.currentLevel;
this.currentLevel++;
- line = _.str.join('\\-\\', lines.substring(1, lines.length));
}
+ line = _.str.trim(lineSplitList[lineSplitList.length - 1]);
selector = this.constructor.getSelectorFromLine(line);
this.processSelector(selector);
restOfLine = _.str.trim(line.substring(selector.length, line.length));
@@ -439,16 +428,16 @@
if (!this.lineStartsWithTick) {
tagHtml = "<" + this.tag;
if (this.tagId !== null) {
- tagHtml += ' id="' + this.tagId + '"';
+ tagHtml += " id=\"" + this.tagId + "\"";
}
if (this.tagClasses.length > 0) {
- tagHtml += ' class="' + this.tagClasses.join(' ') + '"';
+ tagHtml += " class=\"" + this.tagClasses.join(' ') + "\"";
}
if (this.tagAttributes.length > 0) {
tagHtml += this.tagAttributes.join('');
}
if (this.selfClosing) {
- tagHtml += ' />';
+ tagHtml += " />";
if (!this.compress) {
this.output += _.str.repeat(this.indentToken, this.currentLevel);
}
@@ -457,7 +446,7 @@
this.output += "\n";
}
} else {
- tagHtml += '>';
+ tagHtml += ">";
if (this.innerText !== null) {
tagHtml += this.innerText;
}
View
3  package.json
@@ -21,8 +21,7 @@
},
"keywords": [
"wieldymarkup",
- "HTML",
- "abstraction"
+ "HTML abstraction"
],
"author": "Vail Gold",
"license": "MIT"
View
45 src/wieldymarkup.coffee
@@ -78,9 +78,9 @@ class WieldyMarkup
constructor: (text="", compress=false) ->
@text = text
@compress = compress
- @compile() if @text isnt ""
+ @compile()
- compile: (text=null, compress=null) =>
+ compile: (text=null, compress=null) ->
@text = text if text isnt null
@compress = not not compress if compress isnt null
@@ -101,6 +101,7 @@ class WieldyMarkup
processCurrentLevel: =>
@previousLevel = @currentLevel
leadingWhitespace = @constructor.getLeadingWhitespaceFromText @text
+
if leadingWhitespace is ""
@currentLevel = 0
@@ -129,9 +130,9 @@ class WieldyMarkup
closeTag: =>
closingTagTuple = @openTags.pop()
- if not @compress
- output += _.str.repeat @indentToken, closingTagTuple[0]
- @output += "</" + closingTagTuple[1] + ">"
+ if not @compress and closingTagTuple[0] > 0
+ @output += _.str.repeat @indentToken, closingTagTuple[0]
+ @output += "</#{closingTagTuple[1]}>"
@output += "\n" if not @compress
@
@@ -141,7 +142,7 @@ class WieldyMarkup
@innerText = null
line = ""
- if @text.indexOf("\n") > -1
+ if "\n" in @text
lineBreakIndex = @text.indexOf "\n"
line = _.str.trim @text.substring 0, lineBreakIndex
@text = @text.substring lineBreakIndex+1, @text.length
@@ -159,20 +160,12 @@ class WieldyMarkup
else
# Support multiple tags on one line via "\-\" delimiter
- while true
- lineSplitList = line.split '\\-\\'
- lines = [lineSplitList[0]]
-
- if lineSplitList.length is 1
- line = _.str.trim lineSplitList[0]
- break
- else
- lines.push _.str.join '\\-\\', lineSplitList.substring(1, lineSplitList.length)
-
- lines[0] = _.str.trim lines[0]
- selector = @constructor.getSelectorFromLine lines[0]
+ lineSplitList = line.split '\\-\\'
+ while lineSplitList.length > 1
+ temp_line = _.str.trim lineSplitList.shift()
+ selector = @constructor.getSelectorFromLine temp_line
@processSelector selector
- restOfLine = _.str.trim lines[0].substring selector.length, lines[0].length
+ restOfLine = _.str.trim temp_line.substring selector.length, temp_line.length
restOfLine = @processAttributes restOfLine
@addHtmlToOutput()
@@ -182,8 +175,8 @@ class WieldyMarkup
@tagAttributes = []
@previousLevel = @currentLevel
@currentLevel++
- line = _.str.join '\\-\\', lines.substring 1, lines.length
+ line = _.str.trim lineSplitList[lineSplitList.length - 1]
selector = @constructor.getSelectorFromLine line
@processSelector selector
restOfLine = _.str.trim line.substring selector.length, line.length
@@ -351,22 +344,22 @@ class WieldyMarkup
addHtmlToOutput: =>
if not @lineStartsWithTick
- tagHtml = "<" + @tag
- tagHtml += ' id="' + @tagId + '"' if @tagId isnt null
+ tagHtml = "<#{@tag}"
+ tagHtml += " id=\"" + @tagId + "\"" if @tagId isnt null
if @tagClasses.length > 0
- tagHtml += ' class="' + @tagClasses.join(' ') + '"'
+ tagHtml += " class=\"" + @tagClasses.join(' ') + "\""
tagHtml += @tagAttributes.join '' if @tagAttributes.length > 0
if @selfClosing
- tagHtml += ' />'
+ tagHtml += " />"
@output += _.str.repeat(@indentToken, @currentLevel) if not @compress
@output += tagHtml
@output += "\n" if not @compress
else
- tagHtml += '>'
+ tagHtml += ">"
tagHtml += @innerText if @innerText isnt null
@output += _.str.repeat(@indentToken, @currentLevel) if not @compress
@@ -380,7 +373,7 @@ class WieldyMarkup
)
else
- @output += "</" + @tag + ">"
+ @output += "</#{@tag}>"
@output += "\n" if not @compress
@
View
365 test/test.js
@@ -61,8 +61,7 @@
describe('#constructor', function() {
return it('should set correct initial instance variables', function() {
var c;
- c = new Compiler();
- c.compile();
+ c = new Compiler;
expect(c.output).to.equal('');
expect(c.text).to.equal('');
expect(c.compress).to.equal(false);
@@ -74,26 +73,23 @@
return expect(c.openTags).to.have.length(0);
});
});
- return describe('#processCurrentLevel()', function() {
+ describe('#processCurrentLevel()', function() {
return it('should return the level of nesting for a line of markup', function() {
var c;
- c = new Compiler();
- c.compile();
+ c = new Compiler;
c.text = " div";
c.processCurrentLevel();
expect(c.previousLevel).to.equal(0);
expect(c.currentLevel).to.equal(1);
expect(c.indentToken).to.equal(" ");
- c = new Compiler();
- c.compile();
+ c = new Compiler;
c.text = " div";
c.indentToken = " ";
c.processCurrentLevel();
expect(c.previousLevel).to.equal(0);
expect(c.currentLevel).to.equal(2);
expect(c.indentToken).to.equal(" ");
- c = new Compiler();
- c.compile();
+ c = new Compiler;
c.text = "\t\tdiv";
c.indentToken = "\t";
c.processCurrentLevel();
@@ -102,177 +98,186 @@
return expect(c.indentToken).to.equal("\t");
});
});
- /*
-
- def test_close_tag(self):
- c = Compiler()
- c.indentToken = " "
- c.open_tags = [(0, "div")]
- c.close_tag()
- expect(c.output, "</div>\n")
- expect(c.open_tags, [])
-
- c = Compiler('', compress=true)
- c.indentToken = " "
- c.open_tags = [(0, "div")]
- c.close_tag()
- expect(c.output, "</div>")
- expect(c.open_tags, [])
-
- def test_close_lower_level_tags(self):
- c = Compiler()
- c.currentLevel = 0
- c.previousLevel = 2
- c.indentToken = " "
- c.open_tags = [
- (0, "div"),
- (1, "div"),
- (2, "span"),
- ]
- c.close_lower_level_tags()
- expect(c.output, " </span>\n </div>\n</div>\n")
-
- c = Compiler('', compress=true)
- c.currentLevel = 0
- c.previousLevel = 2
- c.indentToken = " "
- c.open_tags = [
- (0, "div"),
- (1, "div"),
- (2, "span"),
- ]
- c.close_lower_level_tags()
- expect(c.output, "</span></div></div>")
-
- def test_process_embedded_line(self):
- c = Compiler()
- c.currentLevel = 2
- c.indentToken = " "
- c.process_embedded_line("`<div>")
- expect(c.output, " <div>\n")
-
- c = Compiler()
- c.currentLevel = 3
- c.indentToken = "\t"
- c.process_embedded_line("`<div>")
- expect(c.output, "\t\t\t<div>\n")
-
- c = Compiler('', compress=true)
- c.currentLevel = 3
- c.indentToken = "\t"
- c.process_embedded_line("`<div>")
- expect(c.output, "<div>")
-
- def test_process_selector(self):
- c = Compiler()
- c.process_selector("div")
- expect(c.tag, "div")
- expect(c.tag_id, null)
- expect(c.tag_classes, [])
-
- c = Compiler()
- c.process_selector("span.class1#id.class2")
- expect(c.tag, "span")
- expect(c.tag_id, "id")
- expect(c.tag_classes, ["class1", "class2"])
-
- c = Compiler()
- c.process_selector("#id.class")
- expect(c.tag, "div")
- expect(c.tag_id, "id")
- expect(c.tag_classes, ["class"])
-
- def test_process_attributes(self):
- c = Compiler()
- rest_of_line = c.process_attributes("")
- expect(c.tag_attributes, [])
- expect(rest_of_line, "")
-
- c = Compiler()
- rest_of_line = c.process_attributes("href=# target=_blank")
- expect(c.tag_attributes, [' href="#"', ' target="_blank"'])
- expect(rest_of_line, "")
-
- c = Compiler()
- rest_of_line = c.process_attributes("href=# <asdf>")
- expect(c.tag_attributes, [' href="#"'])
- expect(rest_of_line, "<asdf>")
-
- c = Compiler()
- rest_of_line = c.process_attributes("val1=val1 data-val2=<%= val2 %> <asdf>")
- expect(c.tag_attributes, [' val1="val1"', ' data-val2="<%= val2 %>"'])
- expect(rest_of_line, "<asdf>")
-
- c = Compiler()
- rest_of_line = c.process_attributes("val1=val1 data-val2=<%= val2 %> <asdf <%= val3 %>>")
- expect(c.tag_attributes, [' val1="val1"', ' data-val2="<%= val2 %>"'])
- expect(rest_of_line, "<asdf <%= val3 %>>")
-
- def test_process_next_line(self):
- c = Compiler()
- c.text = "div\ndiv"
- c.process_next_line()
- expect(c.inner_text, null)
-
- c = Compiler()
- c.text = "div <asdf>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "asdf")
-
- c = Compiler()
- c.text = "div <<%= val %> asdf>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "<%= val %> asdf")
-
- c = Compiler()
- c.text = "div href=# <asdf \n asdf ;lkj <%= val %>>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "asdf asdf ;lkj <%= val %>")
-
- c = Compiler()
- c.indentToken = " "
- c.text = "div \-\ a href=# <asdf>"
- c.process_next_line()
- expect(c.output, '<div>\n <a href="#">asdf</a>\n')
-
- c = Compiler()
- c.indentToken = " "
- c.text = "div \-\ a href=# target=_blank \-\ span <asdf>"
- c.process_next_line()
- expect(c.output, '<div>\n <a href="#" target="_blank">\n <span>asdf</span>\n')
-
- def test_add_html_to_output(self):
- c = Compiler()
- c.line_starts_with_tick = true
- c.add_html_to_output()
- expect(c.output, '')
-
- c = Compiler()
- c.line_starts_with_tick = false
- c.tag = 'input'
- c.tag_id = 'name-input'
- c.tag_classes = ['class1', 'class2']
- c.tag_attributes = [
- ' type="text"',
- ' value="Value"'
- ]
- c.self_closing = true
- c.add_html_to_output()
- expect(c.output, '<input id="name-input" class="class1 class2" type="text" value="Value" />\n')
-
- c = Compiler()
- c.line_starts_with_tick = false
- c.compress = true
- c.tag = 'span'
- c.tag_id = null
- c.tag_classes = []
- c.tag_attributes = []
- c.self_closing = false
- c.inner_text = "<%= val1 %>"
- c.add_html_to_output()
- expect(c.output, '<span><%= val1 %></span>')
- */
-
+ describe('#closeTag()', function() {
+ return it('should close a tag listed as open in the openTags instance variable', function() {
+ var c;
+ c = new Compiler;
+ c.indentToken = " ";
+ c.openTags = [[0, "div"]];
+ c.closeTag();
+ expect(c.output).to.equal("</div>\n");
+ expect(c.openTags).to.have.length(0);
+ expect(c.openTags).to.be.a('Array');
+ c = new Compiler('', true);
+ c.indentToken = " ";
+ c.openTags = [[0, "div"]];
+ c.closeTag();
+ expect(c.output).to.equal("</div>");
+ expect(c.openTags).to.have.length(0);
+ return expect(c.openTags).to.be.a('Array');
+ });
+ });
+ describe('#closeLowerLevelTags()', function() {
+ return it('should add closing tags to output for all open tags', function() {
+ var c;
+ c = new Compiler;
+ c.currentLevel = 0;
+ c.previousLevel = 2;
+ c.indentToken = " ";
+ c.openTags = [[0, "div"], [1, "div"], [2, "span"]];
+ c.closeLowerLevelTags();
+ expect(c.output).to.equal(" </span>\n </div>\n</div>\n");
+ c = new Compiler('', true);
+ c.currentLevel = 0;
+ c.previousLevel = 2;
+ c.indentToken = " ";
+ c.openTags = [[0, "div"], [1, "div"], [2, "span"]];
+ c.closeLowerLevelTags();
+ return expect(c.output).to.equal("</span></div></div>");
+ });
+ });
+ describe('#processEmbeddedLine()', function() {
+ return it('should add unchanged line to output with ` removed', function() {
+ var c;
+ c = new Compiler;
+ c.currentLevel = 2;
+ c.indentToken = " ";
+ c.processEmbeddedLine("`<div>");
+ expect(c.output).to.equal(" <div>\n");
+ c = new Compiler;
+ c.currentLevel = 3;
+ c.indentToken = "\t";
+ c.processEmbeddedLine("`<div>");
+ expect(c.output).to.equal("\t\t\t<div>\n");
+ c = new Compiler('', true);
+ c.currentLevel = 3;
+ c.indentToken = "\t";
+ c.processEmbeddedLine("`<div>");
+ return expect(c.output).to.equal("<div>");
+ });
+ });
+ describe('#processSelector()', function() {
+ return it('should parse a selector string into components', function() {
+ var c;
+ c = new Compiler;
+ c.processSelector("div");
+ expect(c.tag).to.equal("div");
+ expect(c.tagId).to.equal(null);
+ expect(c.tagClasses).to.be.a('Array');
+ expect(c.tagClasses).to.have.length(0);
+ c = new Compiler;
+ c.processSelector("span.class1#id.class2");
+ expect(c.tag).to.equal("span");
+ expect(c.tagId).to.equal("id");
+ expect(c.tagClasses).to.be.a('Array');
+ expect(c.tagClasses).to.have.length(2);
+ expect(c.tagClasses[0]).to.equal("class1");
+ expect(c.tagClasses[1]).to.equal("class2");
+ c = new Compiler;
+ c.processSelector("#id.class");
+ expect(c.tag).to.equal("div");
+ expect(c.tagId).to.equal("id");
+ expect(c.tagClasses).to.be.a('Array');
+ expect(c.tagClasses).to.have.length(1);
+ return expect(c.tagClasses[0]).to.equal("class");
+ });
+ });
+ describe('#processAttributes()', function() {
+ return it('should parse attribute string into components and return the rest of the text', function() {
+ var c, rest_of_line;
+ c = new Compiler;
+ rest_of_line = c.processAttributes("");
+ expect(c.tagAttributes).to.be.a('Array');
+ expect(c.tagAttributes).to.have.length(0);
+ expect(rest_of_line).to.equal("");
+ c = new Compiler;
+ rest_of_line = c.processAttributes("href=# target=_blank");
+ expect(c.tagAttributes).to.be.a('Array');
+ expect(c.tagAttributes).to.have.length(2);
+ expect(c.tagAttributes[0]).to.equal(' href="#"');
+ expect(c.tagAttributes[1]).to.equal(' target="_blank"');
+ expect(rest_of_line).to.equal("");
+ c = new Compiler;
+ rest_of_line = c.processAttributes("href=# <asdf>");
+ expect(c.tagAttributes).to.be.a('Array');
+ expect(c.tagAttributes).to.have.length(1);
+ expect(c.tagAttributes[0]).to.equal(' href="#"');
+ expect(rest_of_line).to.equal("<asdf>");
+ c = new Compiler;
+ rest_of_line = c.processAttributes("val1=val1 data-val2=<%= val2 %> <asdf>");
+ expect(c.tagAttributes).to.be.a('Array');
+ expect(c.tagAttributes).to.have.length(2);
+ expect(c.tagAttributes[0]).to.equal(' val1="val1"');
+ expect(c.tagAttributes[1]).to.equal(' data-val2="<%= val2 %>"');
+ expect(rest_of_line).to.equal("<asdf>");
+ c = new Compiler;
+ rest_of_line = c.processAttributes("val1=val1 data-val2=<%= val2 %> <asdf <%= val3 %>>");
+ expect(c.tagAttributes).to.be.a('Array');
+ expect(c.tagAttributes).to.have.length(2);
+ expect(c.tagAttributes[0]).to.equal(' val1="val1"');
+ expect(c.tagAttributes[1]).to.equal(' data-val2="<%= val2 %>"');
+ return expect(rest_of_line).to.equal("<asdf <%= val3 %>>");
+ });
+ });
+ describe('#processNextLine()', function() {
+ var c;
+ it('should put everything after the attribute string inside of "<" and ' + '">", across line breaks into innerText', function() {});
+ c = new Compiler;
+ c.text = "div\ndiv";
+ c.processNextLine();
+ expect(c.innerText).to.equal(null);
+ c = new Compiler;
+ c.text = "div <asdf>\ndiv";
+ c.processNextLine();
+ expect(c.innerText).to.equal("asdf");
+ c = new Compiler;
+ c.text = "div <<%= val %> asdf>\ndiv";
+ c.processNextLine();
+ expect(c.innerText).to.equal("<%= val %> asdf");
+ c = new Compiler;
+ c.text = "div href=# <asdf \n asdf ;lkj <%= val %>>\ndiv";
+ c.processNextLine();
+ expect(c.innerText).to.equal("asdf asdf ;lkj <%= val %>");
+ c = new Compiler;
+ c.indentToken = " ";
+ c.text = "div \\-\\ a href=# <asdf>";
+ c.processNextLine();
+ expect(c.output).to.equal('<div>\n <a href="#">asdf</a>\n');
+ c = new Compiler;
+ c.indentToken = " ";
+ c.text = "div \\-\\ a href=# target=_blank \\-\\ span <asdf>";
+ c.processNextLine();
+ return expect(c.output).to.equal('<div>\n <a href="#" target="_blank">\n <span>asdf</span>\n');
+ });
+ return describe('#add_html_to_output()', function() {
+ return it('should add HTML to output correctly based on parsed tag data', function() {
+ var c;
+ c = new Compiler;
+ c.lineStartsWithTick = true;
+ c.addHtmlToOutput();
+ expect(c.output).to.equal('');
+ c = new Compiler;
+ c.lineStartsWithTick = false;
+ c.tag = 'input';
+ c.tagId = 'name-input';
+ c.tagClasses = ['class1', 'class2'];
+ c.tagAttributes = [' type="text"', ' value="Value"'];
+ c.selfClosing = true;
+ c.addHtmlToOutput();
+ expect(c.output).to.equal('<input id="name-input" class="class1 class2" type="text" value="Value" />\n');
+ c = new Compiler;
+ c.lineStartsWithTick = false;
+ c.compress = true;
+ c.tag = 'span';
+ c.tagId = null;
+ c.tagClasses = [];
+ c.tagAttributes = [];
+ c.selfClosing = false;
+ c.innerText = "<%= val1 %>";
+ c.addHtmlToOutput();
+ return expect(c.output).to.equal('<span><%= val1 %></span>');
+ });
+ });
});
}).call(this);
View
374 test_src/test.coffee
@@ -58,8 +58,7 @@ describe 'Compiler.prototype', ->
describe '#constructor', ->
it 'should set correct initial instance variables', ->
- c = new Compiler()
- c.compile()
+ c = new Compiler
expect(c.output).to.equal ''
expect(c.text).to.equal ''
@@ -72,19 +71,16 @@ describe 'Compiler.prototype', ->
expect(c.openTags).to.be.a 'Array'
expect(c.openTags).to.have.length 0
-
describe '#processCurrentLevel()', ->
it 'should return the level of nesting for a line of markup', ->
- c = new Compiler()
- c.compile()
+ c = new Compiler
c.text = " div"
c.processCurrentLevel()
expect(c.previousLevel).to.equal 0
expect(c.currentLevel).to.equal 1
expect(c.indentToken).to.equal " "
- c = new Compiler()
- c.compile()
+ c = new Compiler
c.text = " div"
c.indentToken = " "
c.processCurrentLevel()
@@ -92,191 +88,207 @@ describe 'Compiler.prototype', ->
expect(c.currentLevel).to.equal 2
expect(c.indentToken).to.equal " "
- c = new Compiler()
- c.compile()
+ c = new Compiler
c.text = "\t\tdiv"
c.indentToken = "\t"
c.processCurrentLevel()
expect(c.previousLevel).to.equal 0
expect(c.currentLevel).to.equal 2
expect(c.indentToken).to.equal "\t"
-
-
-
- ###
-
- def test_close_tag(self):
- c = Compiler()
- c.indentToken = " "
- c.open_tags = [(0, "div")]
- c.close_tag()
- expect(c.output, "</div>\n")
- expect(c.open_tags, [])
-
- c = Compiler('', compress=true)
- c.indentToken = " "
- c.open_tags = [(0, "div")]
- c.close_tag()
- expect(c.output, "</div>")
- expect(c.open_tags, [])
+ describe '#closeTag()', ->
+ it 'should close a tag listed as open in the openTags instance variable', ->
+ c = new Compiler
+ c.indentToken = " "
+ c.openTags = [[0, "div"]]
+ c.closeTag()
+ expect(c.output).to.equal "</div>\n"
+ expect(c.openTags).to.have.length 0
+ expect(c.openTags).to.be.a 'Array'
+
+ c = new Compiler '', true
+ c.indentToken = " "
+ c.openTags = [[0, "div"]]
+ c.closeTag()
+ expect(c.output).to.equal "</div>"
+ expect(c.openTags).to.have.length 0
+ expect(c.openTags).to.be.a 'Array'
- def test_close_lower_level_tags(self):
- c = Compiler()
- c.currentLevel = 0
- c.previousLevel = 2
- c.indentToken = " "
- c.open_tags = [
- (0, "div"),
- (1, "div"),
- (2, "span"),
- ]
- c.close_lower_level_tags()
- expect(c.output, " </span>\n </div>\n</div>\n")
-
- c = Compiler('', compress=true)
- c.currentLevel = 0
- c.previousLevel = 2
- c.indentToken = " "
- c.open_tags = [
- (0, "div"),
- (1, "div"),
- (2, "span"),
- ]
- c.close_lower_level_tags()
- expect(c.output, "</span></div></div>")
+ describe '#closeLowerLevelTags()', ->
+ it 'should add closing tags to output for all open tags', ->
+ c = new Compiler
+ c.currentLevel = 0
+ c.previousLevel = 2
+ c.indentToken = " "
+ c.openTags = [
+ [0, "div"]
+ [1, "div"]
+ [2, "span"]
+ ]
+ c.closeLowerLevelTags()
+ expect(c.output).to.equal " </span>\n </div>\n</div>\n"
+
+ c = new Compiler '', true
+ c.currentLevel = 0
+ c.previousLevel = 2
+ c.indentToken = " "
+ c.openTags = [
+ [0, "div"]
+ [1, "div"]
+ [2, "span"]
+ ]
+ c.closeLowerLevelTags()
+ expect(c.output).to.equal "</span></div></div>"
- def test_process_embedded_line(self):
- c = Compiler()
- c.currentLevel = 2
- c.indentToken = " "
- c.process_embedded_line("`<div>")
- expect(c.output, " <div>\n")
-
- c = Compiler()
- c.currentLevel = 3
- c.indentToken = "\t"
- c.process_embedded_line("`<div>")
- expect(c.output, "\t\t\t<div>\n")
-
- c = Compiler('', compress=true)
- c.currentLevel = 3
- c.indentToken = "\t"
- c.process_embedded_line("`<div>")
- expect(c.output, "<div>")
+ describe '#processEmbeddedLine()', ->
+ it 'should add unchanged line to output with ` removed', ->
+ c = new Compiler
+ c.currentLevel = 2
+ c.indentToken = " "
+ c.processEmbeddedLine "`<div>"
+ expect(c.output).to.equal " <div>\n"
+
+ c = new Compiler
+ c.currentLevel = 3
+ c.indentToken = "\t"
+ c.processEmbeddedLine "`<div>"
+ expect(c.output).to.equal "\t\t\t<div>\n"
+
+ c = new Compiler '', true
+ c.currentLevel = 3
+ c.indentToken = "\t"
+ c.processEmbeddedLine "`<div>"
+ expect(c.output).to.equal "<div>"
- def test_process_selector(self):
- c = Compiler()
- c.process_selector("div")
- expect(c.tag, "div")
- expect(c.tag_id, null)
- expect(c.tag_classes, [])
-
- c = Compiler()
- c.process_selector("span.class1#id.class2")
- expect(c.tag, "span")
- expect(c.tag_id, "id")
- expect(c.tag_classes, ["class1", "class2"])
-
- c = Compiler()
- c.process_selector("#id.class")
- expect(c.tag, "div")
- expect(c.tag_id, "id")
- expect(c.tag_classes, ["class"])
+ describe '#processSelector()', ->
+ it 'should parse a selector string into components', ->
+ c = new Compiler
+ c.processSelector "div"
+ expect(c.tag).to.equal "div"
+ expect(c.tagId).to.equal null
+ expect(c.tagClasses).to.be.a 'Array'
+ expect(c.tagClasses).to.have.length 0
+
+ c = new Compiler
+ c.processSelector "span.class1#id.class2"
+ expect(c.tag).to.equal "span"
+ expect(c.tagId).to.equal "id"
+ expect(c.tagClasses).to.be.a 'Array'
+ expect(c.tagClasses).to.have.length 2
+ expect(c.tagClasses[0]).to.equal "class1"
+ expect(c.tagClasses[1]).to.equal "class2"
+
+ c = new Compiler
+ c.processSelector "#id.class"
+ expect(c.tag).to.equal "div"
+ expect(c.tagId).to.equal "id"
+ expect(c.tagClasses).to.be.a 'Array'
+ expect(c.tagClasses).to.have.length 1
+ expect(c.tagClasses[0]).to.equal "class"
- def test_process_attributes(self):
- c = Compiler()
- rest_of_line = c.process_attributes("")
- expect(c.tag_attributes, [])
- expect(rest_of_line, "")
-
- c = Compiler()
- rest_of_line = c.process_attributes("href=# target=_blank")
- expect(c.tag_attributes, [' href="#"', ' target="_blank"'])
- expect(rest_of_line, "")
-
- c = Compiler()
- rest_of_line = c.process_attributes("href=# <asdf>")
- expect(c.tag_attributes, [' href="#"'])
- expect(rest_of_line, "<asdf>")
-
- c = Compiler()
- rest_of_line = c.process_attributes("val1=val1 data-val2=<%= val2 %> <asdf>")
- expect(c.tag_attributes, [' val1="val1"', ' data-val2="<%= val2 %>"'])
- expect(rest_of_line, "<asdf>")
-
- c = Compiler()
- rest_of_line = c.process_attributes("val1=val1 data-val2=<%= val2 %> <asdf <%= val3 %>>")
- expect(c.tag_attributes, [' val1="val1"', ' data-val2="<%= val2 %>"'])
- expect(rest_of_line, "<asdf <%= val3 %>>")
+ describe '#processAttributes()', ->
+ it 'should parse attribute string into components and return the rest of the text', ->
+ c = new Compiler
+ rest_of_line = c.processAttributes ""
+ expect(c.tagAttributes).to.be.a 'Array'
+ expect(c.tagAttributes).to.have.length 0
+ expect(rest_of_line).to.equal ""
+
+ c = new Compiler
+ rest_of_line = c.processAttributes "href=# target=_blank"
+ expect(c.tagAttributes).to.be.a 'Array'
+ expect(c.tagAttributes).to.have.length 2
+ expect(c.tagAttributes[0]).to.equal ' href="#"'
+ expect(c.tagAttributes[1]).to.equal ' target="_blank"'
+ expect(rest_of_line).to.equal ""
+
+ c = new Compiler
+ rest_of_line = c.processAttributes "href=# <asdf>"
+ expect(c.tagAttributes).to.be.a 'Array'
+ expect(c.tagAttributes).to.have.length 1
+ expect(c.tagAttributes[0]).to.equal ' href="#"'
+ expect(rest_of_line).to.equal "<asdf>"
+
+ c = new Compiler
+ rest_of_line = c.processAttributes "val1=val1 data-val2=<%= val2 %> <asdf>"
+ expect(c.tagAttributes).to.be.a 'Array'
+ expect(c.tagAttributes).to.have.length 2
+ expect(c.tagAttributes[0]).to.equal ' val1="val1"'
+ expect(c.tagAttributes[1]).to.equal ' data-val2="<%= val2 %>"'
+ expect(rest_of_line).to.equal "<asdf>"
+
+ c = new Compiler
+ rest_of_line = c.processAttributes "val1=val1 data-val2=<%= val2 %> <asdf <%= val3 %>>"
+ expect(c.tagAttributes).to.be.a 'Array'
+ expect(c.tagAttributes).to.have.length 2
+ expect(c.tagAttributes[0]).to.equal ' val1="val1"'
+ expect(c.tagAttributes[1]).to.equal ' data-val2="<%= val2 %>"'
+ expect(rest_of_line).to.equal "<asdf <%= val3 %>>"
- def test_process_next_line(self):
- c = Compiler()
- c.text = "div\ndiv"
- c.process_next_line()
- expect(c.inner_text, null)
-
- c = Compiler()
- c.text = "div <asdf>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "asdf")
-
- c = Compiler()
- c.text = "div <<%= val %> asdf>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "<%= val %> asdf")
-
- c = Compiler()
- c.text = "div href=# <asdf \n asdf ;lkj <%= val %>>\ndiv"
- c.process_next_line()
- expect(c.inner_text, "asdf asdf ;lkj <%= val %>")
+ describe '#processNextLine()', ->
+ it 'should put everything after the attribute string inside of "<" and ' +
+ '">", across line breaks into innerText', ->
+
+ c = new Compiler
+ c.text = "div\ndiv"
+ c.processNextLine()
+ expect(c.innerText).to.equal null
+
+ c = new Compiler
+ c.text = "div <asdf>\ndiv"
+ c.processNextLine()
+ expect(c.innerText).to.equal "asdf"
+
+ c = new Compiler
+ c.text = "div <<%= val %> asdf>\ndiv"
+ c.processNextLine()
+ expect(c.innerText).to.equal "<%= val %> asdf"
+
+ c = new Compiler
+ c.text = "div href=# <asdf \n asdf ;lkj <%= val %>>\ndiv"
+ c.processNextLine()
+ expect(c.innerText).to.equal "asdf asdf ;lkj <%= val %>"
+
+ c = new Compiler
+ c.indentToken = " "
+ c.text = "div \\-\\ a href=# <asdf>"
+ c.processNextLine()
+ expect(c.output).to.equal '<div>\n <a href="#">asdf</a>\n'
- c = Compiler()
- c.indentToken = " "
- c.text = "div \-\ a href=# <asdf>"
- c.process_next_line()
- expect(c.output, '<div>\n <a href="#">asdf</a>\n')
-
- c = Compiler()
- c.indentToken = " "
- c.text = "div \-\ a href=# target=_blank \-\ span <asdf>"
- c.process_next_line()
- expect(c.output, '<div>\n <a href="#" target="_blank">\n <span>asdf</span>\n')
+ c = new Compiler
+ c.indentToken = " "
+ c.text = "div \\-\\ a href=# target=_blank \\-\\ span <asdf>"
+ c.processNextLine()
+ expect(c.output).to.equal '<div>\n <a href="#" target="_blank">\n <span>asdf</span>\n'
- def test_add_html_to_output(self):
- c = Compiler()
- c.line_starts_with_tick = true
- c.add_html_to_output()
- expect(c.output, '')
-
- c = Compiler()
- c.line_starts_with_tick = false
- c.tag = 'input'
- c.tag_id = 'name-input'
- c.tag_classes = ['class1', 'class2']
- c.tag_attributes = [
- ' type="text"',
- ' value="Value"'
- ]
- c.self_closing = true
- c.add_html_to_output()
- expect(c.output, '<input id="name-input" class="class1 class2" type="text" value="Value" />\n')
-
- c = Compiler()
- c.line_starts_with_tick = false
- c.compress = true
- c.tag = 'span'
- c.tag_id = null
- c.tag_classes = []
- c.tag_attributes = []
- c.self_closing = false
- c.inner_text = "<%= val1 %>"
- c.add_html_to_output()
- expect(c.output, '<span><%= val1 %></span>')
- ###
-
-
-
-
-
-
+ describe '#add_html_to_output()', ->
+ it 'should add HTML to output correctly based on parsed tag data', ->
+ c = new Compiler
+ c.lineStartsWithTick = true
+ c.addHtmlToOutput()
+ expect(c.output).to.equal ''
+
+ c = new Compiler
+ c.lineStartsWithTick = false
+ c.tag = 'input'
+ c.tagId = 'name-input'
+ c.tagClasses = ['class1', 'class2']
+ c.tagAttributes = [
+ ' type="text"',
+ ' value="Value"'
+ ]
+ c.selfClosing = true
+ c.addHtmlToOutput()
+ expect(c.output).to.equal '<input id="name-input" class="class1 class2" type="text" value="Value" />\n'
+
+ c = new Compiler
+ c.lineStartsWithTick = false
+ c.compress = true
+ c.tag = 'span'
+ c.tagId = null
+ c.tagClasses = []
+ c.tagAttributes = []
+ c.selfClosing = false
+ c.innerText = "<%= val1 %>"
+ c.addHtmlToOutput()
+ expect(c.output).to.equal '<span><%= val1 %></span>'
Please sign in to comment.
Something went wrong with that request. Please try again.