UNPKG

rule

Version:

Coffeescript friendly css selector based templating

1,020 lines (922 loc) 37.1 kB
// Generated by CoffeeScript 1.4.0 (function() { var Rule, document, expect, window, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Rule = this.Rule; expect = this.expect; window = this.window; document = this.document; describe('Rule', function() { var asString, makeNode; makeNode = function(string) { var container; container = document.createElement('div'); container.innerHTML = string; return container.childNodes; }; asString = function(object) { var container, node, _i, _len; container = document.createElement('div'); if (object instanceof Array) { for (_i = 0, _len = object.length; _i < _len; _i++) { node = object[_i]; container.appendChild(node.cloneNode(true)); } return container.innerHTML; } else { return object.outerHTML; } }; before(function(done) { if ((typeof module !== "undefined" && module !== null) && this.module !== module) { Rule = require('../rule'); expect = require('expect.js'); window = require('sub').window; document = window.document; Rule.env = window; } return done(); }); describe('::split', function() { var attributes, positions, selectors; selectors = [void 0, 'div', '.a', '.a-b', '["a=b"]', 'div:nth-child(n)', 'div > span', 'div + span']; attributes = [void 0, 'a', 'a-b']; positions = [void 0, '-', '+', '=', '<', '>']; return it("should return [selector, attribute, position]", function() { var attribute, position, selector, _i, _len, _results; _results = []; for (_i = 0, _len = selectors.length; _i < _len; _i++) { selector = selectors[_i]; _results.push((function() { var _j, _len1, _results1; _results1 = []; for (_j = 0, _len1 = attributes.length; _j < _len1; _j++) { attribute = attributes[_j]; _results1.push((function() { var _k, _len2, _results2; _results2 = []; for (_k = 0, _len2 = positions.length; _k < _len2; _k++) { position = positions[_k]; _results2.push(expect(Rule.split((selector != null ? selector : '') + (attribute ? '@' + attribute : '') + (position != null ? position : ''))).to.be.eql([selector, attribute, position])); } return _results2; })()); } return _results1; })()); } return _results; }); }); describe('::parse', function() { it("should return the parsed result of the function bound to data", function() { var a; expect(Rule.parse((function() { return this; }), 'a')).to.be.eql('a'); expect(Rule.parse((function() { return this.a; }), { a: 'b' })).to.be.eql('b'); expect(Rule.parse((function() { return function() { return this.a; }; }), { a: 'b' })).to.be.eql('b'); a = function() { return this.a; }; return expect(Rule.parse((function() { return a; }), { a: 'b' })).to.be.eql('b'); }); it("should return the array with each array item parsed", function() { var a, b, c; a = document.createTextNode('a'); b = document.createTextNode('b'); c = document.createTextNode('c'); expect(Rule.parse([a, b, c])).to.be.eql([a, b, c]); return expect(Rule.parse([ (function() { return this.a; }), (function() { return this.b; }), (function() { return this.c; }) ], { a: a, b: b, c: c })).to.be.eql([a, b, c]); }); it("should return the result of the rule's render function", function() { var rule, selection; rule = new Rule({ '.a': function() { return this; } }); selection = makeNode('<div><span class="a"></div>'); Rule.parse(rule, 'b', { selection: selection }); expect(asString(selection)).to.be.equal(asString(makeNode('<div><span class="a">b</div>'))); selection = makeNode('<div><span class="a"></div>'); rule.template = selection; return expect(asString(Rule.parse(rule, 'b'))).to.be.eql(asString(makeNode('<div><span class="a">b</span></div>'))); }); it("should return the passed in HTMLElement", function() { var el; el = makeNode('<div>')[0]; return expect(Rule.parse(el)).to.be(el); }); it("should return the passed in node array as an array", function() { var el; el = makeNode('<div>'); expect(Rule.parse(el)).to.eql(el); el = makeNode('<div></div><span></span>'); return expect(Rule.parse(el)).to.eql(el); }); it("should return undefined", function() { return expect(Rule.parse(void 0)).to.be(void 0); }); it("should return null", function() { return expect(Rule.parse(null)).to.be(null); }); it("should return the object's toString results", function() { var O, o; expect(Rule.parse(true)).to.be('true'); expect(Rule.parse(false)).to.be('false'); expect(Rule.parse('abc')).to.be('abc'); expect(Rule.parse(123)).to.be('123'); O = function() {}; O.prototype.toString = function() { return 'test'; }; o = new O; expect(Rule.parse(o)).to.be('test'); o = { toString: function() { return 'test'; } }; return expect(Rule.parse(o)).to.be('test'); }); return it("should return the results of the object compiled as a new rule with selection as the template", function() { var rule, selection; rule = { '.a': function() { return this; } }; selection = makeNode('<div><span class="a"></div>'); Rule.parse(rule, 'b', { selection: selection }); return expect(asString(selection)).to.be.eql(asString(makeNode('<div><span class="a">b</div>'))); }); }); describe('::add', function() { it("should prepend the attribute with content", function() { var e; e = makeNode('<div class="b">'); return expect(asString(Rule.add('a', e, 'class', '-'))).to.be.eql(asString(makeNode('<div class="a b">'))); }); it("should append the attribute with content", function() { var e; e = makeNode('<div class="a">'); return expect(asString(Rule.add('b', e, 'class', '+'))).to.be.eql(asString(makeNode('<div class="a b">'))); }); it("should add before the attribute with content", function() { var e; e = makeNode('<div class="b">'); return expect(asString(Rule.add('a', e, 'class', '<'))).to.be.eql(asString(makeNode('<div class="ab">'))); }); it("should add after the attribute with content", function() { var e; e = makeNode('<div class="a">'); return expect(asString(Rule.add('b', e, 'class', '>'))).to.be.eql(asString(makeNode('<div class="ab">'))); }); it("should set the attribute to content", function() { var e; e = makeNode('<div class="b">'); return expect(asString(Rule.add('a', e, 'class'))).to.be.eql(asString(makeNode('<div class="a">'))); }); it("should add content before selection", function() { var c, r; c = makeNode('<div><span></span></div>'); r = Rule.add('a', c, null, '<'); expect(asString(c)).to.be.eql(asString(makeNode('<div>a<span></span></div>'))); return expect(asString(r)).to.be.eql(asString(makeNode('<div>a<span></span></div>'))); }); it("should add content after selection", function() { var c, r; c = makeNode('<div><span></span></div>'); r = Rule.add('a', c, null, '>'); expect(asString(c)).to.be.eql(asString(makeNode('<div><span></span>a</div>'))); return expect(asString(r)).to.be.eql(asString(makeNode('<div><span></span>a</div>'))); }); it("should add content as the first child of selection", function() { var c, e, f, r; c = makeNode('<div>')[0]; e = c.appendChild(makeNode('<span>')[0]); f = e.appendChild(makeNode('<span>')[0]); r = Rule.add('a', [f], null, '-'); expect(asString(r)).to.be.eql(asString(makeNode('a<span></span>'))); return expect(asString(c)).to.be.eql(asString(makeNode('<div><span>a<span></span></span></div>'))); }); it("should add content as the last child of selection", function() { var c, e, f, r; c = makeNode('<div>')[0]; e = c.appendChild(makeNode('<span>')[0]); f = e.appendChild(makeNode('<span>')[0]); r = Rule.add('a', [f], null, '+'); expect(asString(c)).to.be.eql(asString(makeNode('<div><span><span></span>a</span></div>'))); return expect(asString(r)).to.be.eql(asString(makeNode('<span></span>a'))); }); it("should set content to replace selection", function() { var c, e, r; c = makeNode('<div>')[0]; e = c.appendChild(makeNode('<span>')[0]); r = Rule.add('a', [e], null, '='); expect(asString(c)).to.be.eql(asString(makeNode('<div>a</div>'))); return expect(asString(r)).to.be.eql('a'); }); it("should set content as the only child of selection", function() { var c, e, f, r; c = makeNode('<div>')[0]; e = c.appendChild(makeNode('<span>')[0]); f = e.appendChild(makeNode('<span>')[0]); r = Rule.add('a', [e]); expect(asString(c)).to.be.eql(asString(makeNode('<div><span>a</span></div>'))); return expect(asString(r)).to.be.eql(asString(makeNode('<span>a</span>'))); }); it("should set array of content as children of selection", function() { var c, r; c = makeNode('<div>'); r = Rule.add(['a', 'b', 'c', 'd'], c); expect(asString(c)).to.be.eql(asString(makeNode('<div>abcd</div>'))); return expect(asString(r)).to.be.eql(asString(makeNode('<div>abcd</div>'))); }); return it("should set joined array of content as attribute", function() { var c, r; c = makeNode('<div>'); r = Rule.add(['a', 'b', 'c', 'd'], c, 'class'); return expect(asString(r)).to.be.eql(asString(makeNode('<div class="abcd"></div>'))); }); }); return describe('.render', function() { it("should clone a template and return that object", function() { var rule, template; template = makeNode('<div>'); rule = new Rule({ '': 'test' }, template); expect(asString(rule.render())).to.be.eql(asString(makeNode('<div>test</div>'))); return expect(asString(template)).to.be.eql(asString(makeNode('<div>'))); }); it("should alter a template and return that object", function() { var rule, template; template = makeNode('<div>'); rule = new Rule({ '': 'test' }); expect(asString(rule.render({}, template))).to.be.eql(asString(makeNode('<div>test</div>'))); return expect(template).to.be.equal(template); }); it("should allow text nodes in the template", function() { var rule, template; template = makeNode('<div>test<span class="a"></span>test</div>'); rule = new Rule({ '.a': 'test' }); expect(asString(rule.render({}, template))).to.be.eql(asString(makeNode('<div>test<span class="a">test</span>test</div>'))); return expect(template).to.be.equal(template); }); it("should execute a template function and render on that generated template", function() { var rule, template; template = function() { return makeNode('<div>'); }; rule = new Rule({ '': 'test' }); expect(asString(rule.render({}, template))).to.be.eql(asString(makeNode('<div>test</div>'))); return expect(template).to.be.equal(template); }); it("should set the attributes of the parent", function() { var rule; rule = new Rule({ '@class': 'test' }, makeNode('<div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div class="test"></div>'))); }); it("should set the attributes of a selection", function() { var rule; rule = new Rule({ 'span@class': 'test' }, makeNode('<div><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test"></span></div>'))); }); it("should set the attributes of a subparent", function() { var rule; rule = new Rule({ 'span': { '@class': 'test' } }, makeNode('<div><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test"></span></div>'))); }); it("should not set the attribute if the content is undefined", function() { var rule; rule = new Rule({ '@class': 'test' }, makeNode('<div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div class="test"></div>'))); }); it("should set the contents based on a data object", function() { var rule; rule = new Rule({ '': function() { return this.a; } }, makeNode('<div>')); return expect(asString(rule.render({ a: 'test' }))).to.be.eql(asString(makeNode('<div>test</div>'))); }); it("should set the contents of a simple tag selection", function() { var rule; rule = new Rule({ 'span': 'test' }, makeNode('<div><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span>test</span></div>'))); }); it("should replace the contents of a simple tag selection", function() { var rule; rule = new Rule({ 'span': 'test' }, makeNode('<div><span><a>a</a><a>b</a><a>c</a></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span>test</span></div>'))); }); it("should not find the selection and do nothing", function() { var rule; rule = new Rule({ 'span': 'x' }, makeNode('<div><a></a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a></a></div>'))); }); it("should set the contents of a simple class selection", function() { var rule; rule = new Rule({ '.simple': 'test' }, makeNode('<div><a><span>a</span><h1>x</h1><span class="simple">b</span><span>c</span></a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a><span>a</span><h1>x</h1><span class="simple">test</span><span>c</span></a></div>'))); }); it("should set the contents of multiple selections", function() { var rule; rule = new Rule({ 'span': 'test' }, makeNode('<div><span></span><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span>test</span><span>test</span></div>'))); }); it("should set the contents of multiple selections on different levels", function() { var rule; rule = new Rule({ 'span': 'test' }, makeNode('<div><span></span><a><span></span></a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span>test</span><a><span>test</span></a></div>'))); }); it("should add content in the right order and return the added siblings", function() { var rule; rule = new Rule({ '': 'c', '>': 'd', '<': 'b' }, makeNode('<span>x</span>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<span>bcd</span>'))); }); it("should add content before and after a selection", function() { var rule; rule = new Rule({ 'span-': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span></span></div>')); expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a></a><span></span></div>'))); rule = new Rule({ 'span+': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span></span><a></a></div>'))); }); it("should add content to the start and end of a selection", function() { var rule; rule = new Rule({ 'span<': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span></div>')); expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span><a></a><p></p></span></div>'))); rule = new Rule({ 'span>': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span><p></p><a></a></span></div>'))); }); it("should replace a selection", function() { var rule; rule = new Rule({ 'span=': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a></a></div>'))); }); it("should remove multiple selections", function() { var rule; rule = new Rule({ 'span=': function() { return ''; }, 'span': function() { return 'hi'; } }, makeNode('<div><span><p>test</p></span><span><h1>title</h1></span><span><a>link</a></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should replace a selection and not match again", function() { var pRan, rule; pRan = false; rule = new Rule({ 'span=': function() { return ''; }, 'span': function() { return pRan = true; } }, makeNode('<div><span><p></p></span></div>')); rule.render(); return expect(pRan).to.be.eql(false); }); it("should replace a selection and not execute replaced match", function() { var pRan, rule; pRan = false; rule = new Rule({ 'span=': function() { return makeNode('<a>')[0]; }, 'p': function() { return pRan = true; } }, makeNode('<div><span><p></p></span></div>')); rule.render(); return expect(pRan).to.be.eql(false); }); it("should add content before and after multiple selections", function() { var rule; rule = new Rule({ 'span-': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span></span><span></span></div>')); expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a></a><span></span><a></a><span></span></div>'))); rule = new Rule({ 'span+': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span></span><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span></span><a></a><span></span><a></a></div>'))); }); it("should add content to the start and end of multiple selections", function() { var rule; rule = new Rule({ 'span<': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span><span><h1></h1></span></div>')); expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span><a></a><p></p></span><span><a></a><h1></h1></span></div>'))); rule = new Rule({ 'span>': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span><span><h1></h1></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span><p></p><a></a></span><span><h1></h1><a></a></span></div>'))); }); it("should replace multiple selections", function() { var rule; rule = new Rule({ 'span=': function() { return makeNode('<a>')[0]; } }, makeNode('<div><span><p></p></span><span><h1></h1></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a></a><a></a></div>'))); }); it("should alter attributes of a selection", function() { var rule; rule = new Rule({ 'span@class': 'test' }, makeNode('<div><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test"></span></div>'))); }); it("should alter attributes of multiple selections", function() { var rule; rule = new Rule({ 'span@class': 'test' }, makeNode('<div><span></span><span></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test"></span><span class="test"></span></div>'))); }); it("should select into a new scope and apply a new rule object to it", function() { var rule; rule = new Rule({ 'a': { 'span': 'c' } }, makeNode('<div><a><span>b</span></a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a><span>c</span></a></div>'))); }); it("should select into a new scope and not find the selection in the new context", function() { var rule; rule = new Rule({ 'a': { 'div': 'c' } }, makeNode('<div><a><span>b</span></a><div></div></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a><span>b</span></a><div></div></div>'))); }); it("should select into a new scope and do nothing", function() { var rule; rule = new Rule({ '': {} }, makeNode('<div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should remove multiple selections", function() { var rule; rule = new Rule({ '.a=': '' }, makeNode('<div><span class="a">b</span><span class="a">c</span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should remove a selection then attempt to add to it", function() { var rule; rule = new Rule({ 'a': { '=': '', '': 'c' } }, makeNode('<div><a>b</a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should not remove a selection then add to it", function() { var rule; rule = new Rule({ 'a': { '=': null, '': 'c' } }, makeNode('<div><a>b</a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a>c</a></div>'))); }); it("should remove a selection then attempt to add an attribute to it", function() { var rule; rule = new Rule({ 'a': { '=': '', '@href': 'c' } }, makeNode('<div><a>b</a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should remove a selection then attempt to add to a child of it", function() { var rule; rule = new Rule({ 'a': { '=': '', 'span': 'c' } }, makeNode('<div><a><span></span></a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div></div>'))); }); it("should add a sibling to a selection then add to the root", function() { var rule; rule = new Rule({ 'a': { '+': function() { return makeNode('<a>')[0]; }, '': 'c' } }, makeNode('<div><a>b</a></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><a>c</a><a></a></div>'))); }); it("should set the contents to the result of an array of functions", function() { var rule; rule = new Rule({ 'span': [ (function() { return this.a; }), (function() { return this.b; }), (function() { return this.c; }) ] }, makeNode('<div><span></span></div>')); return expect(asString(rule.render({ a: 'x', b: 'y', c: 'z' }))).to.be.eql(asString(makeNode('<div><span>xyz</span></div>'))); }); it("should set the contents to the result of a function that returns an array of functions", function() { var rule; rule = new Rule({ 'span': function() { var i, _i, _results; _results = []; for (i = _i = 1; _i < 5; i = ++_i) { _results.push((function(i) { return i * this.x; }).bind(this, i)); } return _results; } }, makeNode('<div><span></span></div>')); return expect(asString(rule.render({ x: 2 }))).to.be.eql(asString(makeNode('<div><span>2468</span></div>'))); }); it("should extend the Rule class then render off it", function() { var Test; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '': 'test' }; Test.prototype.template = makeNode('<div></div>'); return Test; })(Rule); return expect(asString((new Test).render())).to.be.eql(asString(makeNode('<div>test</div>'))); }); it("should extend the Rule class while providing a rule property", function() { var Test, test; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '': 'test' }; Test.prototype.template = makeNode('<div></div>'); return Test; })(Rule); test = new Test({ '>': 'test2' }); return expect(asString(test.render())).to.be.eql(asString(makeNode('<div>test2</div>'))); }); it("should extend the Rule class then set the contents of a nested element", function() { var Test; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test': { '': 'test' } }; Test.prototype.template = makeNode('<div><p><span class="test"></span></p></div>'); return Test; })(Rule); return expect(asString((new Test).render())).to.be.eql(asString(makeNode('<div><p><span class="test">test</span></p></div>'))); }); it("should extend the Rule class then replace the contents of a nested element", function() { var Test; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test': { '=': 'test' } }; Test.prototype.template = makeNode('<div><p><span class="test"></span></p></div>'); return Test; })(Rule); return expect(asString((new Test).render())).to.be.eql(asString(makeNode('<div><p>test</p></div>'))); }); it("should extend an extended Rule, and then apply both rules in order of oldest first", function() { var Test, Test2; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test': 'X', '.test2': 'X' }; Test.prototype.template = makeNode('<div><p><span class="test"></span><span class="test2"></span></p></div>'); return Test; })(Rule); Test2 = (function(_super) { __extends(Test2, _super); function Test2() { return Test2.__super__.constructor.apply(this, arguments); } Test2.prototype.rule = { '.test2': 'Y' }; return Test2; })(Test); return expect(asString((new Test2).render())).to.be.eql(asString(makeNode('<div><p><span class="test">X</span><span class="test2">Y</span></p></div>'))); }); it("should extend an extended Rule, and and overwrite the order of the overwritten rule", function() { var Test, Test2; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test': 'X', '.test2': 'Y' }; Test.prototype.template = makeNode('<div><p><span class="test test2"></span></p></div>'); return Test; })(Rule); Test2 = (function(_super) { __extends(Test2, _super); function Test2() { return Test2.__super__.constructor.apply(this, arguments); } Test2.prototype.rule = { '.test': 'X' }; return Test2; })(Test); return expect(asString((new Test2).render())).to.be.eql(asString(makeNode('<div><p><span class="test test2">X</span></p></div>'))); }); it("should extend an extended Rule twice, and then apply both rules in order of oldest first", function() { var Test, Test2, Test3; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test': 'X', '.test2': 'X', '.test3': 'X' }; Test.prototype.template = makeNode('<div><p><span class="test"></span><span class="test2"></span><span class="test3"></span></p></div>'); return Test; })(Rule); Test2 = (function(_super) { __extends(Test2, _super); function Test2() { return Test2.__super__.constructor.apply(this, arguments); } Test2.prototype.rule = { '.test2': 'Y', '.test3': 'Y' }; return Test2; })(Test); Test3 = (function(_super) { __extends(Test3, _super); function Test3() { return Test3.__super__.constructor.apply(this, arguments); } Test3.prototype.rule = { '.test3': 'Z' }; return Test3; })(Test2); return expect(asString((new Test3).render())).to.be.eql(asString(makeNode('<div><p><span class="test">X</span><span class="test2">Y</span><span class="test3">Z</span></p></div>'))); }); it("should extend an extended Rule, and then apply just the childmost overwritten rule", function() { var Test, Test2; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.prototype.rule = { '.test<': 'X' }; Test.prototype.template = makeNode('<div><p><span class="test"></span></p></div>'); return Test; })(Rule); Test2 = (function(_super) { __extends(Test2, _super); function Test2() { return Test2.__super__.constructor.apply(this, arguments); } Test2.prototype.rule = { '.test<': 'Y' }; return Test2; })(Test); return expect(asString((new Test2).render())).to.be.eql(asString(makeNode('<div><p><span class="test">Y</span></p></div>'))); }); it("should extend the Rule class and overwrite the parse function", function() { var Test, rule; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.parse = function(rule, data, selection, context) { if (typeof rule === 'string' || rule instanceof String) { return rule + 'suffix'; } return Test.__super__.constructor.parse.apply(this, arguments); }; return Test; })(Rule); rule = new Test({ '.test': 'X' }, makeNode('<div><span class="test"></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test">Xsuffix</span></div>'))); }); it("should extend the Rule class and overwrite the parse function and parse other rules that haven't been blocked", function() { var Class, Test, rule; Class = (function() { function Class() {} return Class; })(); Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.parse = function(rule, data, selection, context) { if (rule instanceof Class) { return 'class'; } return Test.__super__.constructor.parse.apply(this, arguments); }; return Test; })(Rule); rule = new Test({ '.test': new Class, '.test2': 'test' }, makeNode('<div><span class="test"></span><span class="test2"></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test">class</span><span class="test2">test</span></div>'))); }); it("should extend the Rule class with a custom parser and use that parser when a new rule is created from a rule object", function() { var Test, rule; Test = (function(_super) { __extends(Test, _super); function Test() { return Test.__super__.constructor.apply(this, arguments); } Test.parse = function(rule, data, selection, context) { if (typeof rule === 'string' || rule instanceof String) { return rule + 'suffix'; } return Test.__super__.constructor.parse.apply(this, arguments); }; return Test; })(Rule); rule = new Test({ '.test': { '': 'X' } }, makeNode('<div><span class="test"></span></div>')); return expect(asString(rule.render())).to.be.eql(asString(makeNode('<div><span class="test">Xsuffix</span></div>'))); }); return it("should fail a rule without crashing", function() { var rule, temp; rule = new Rule({ '@class+': function() { return a.b; } }, makeNode('<div>')); temp = console.error; console.error = function() {}; expect(asString(rule.render())).to.not.throwError; return console.error = temp; }); }); }); }).call(this);