Skip to content

Commit

Permalink
[fix] Fixed problem tag implementation, there was an API for it, but …
Browse files Browse the repository at this point in the history
…it wasn't used
  • Loading branch information
3rd-Eden committed Aug 8, 2012
1 parent c8d669e commit 550b4a4
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 87 deletions.
8 changes: 8 additions & 0 deletions lib/plates.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ var Plates = (typeof module !== 'undefined' && typeof module.exports !== 'undefi
, mapping = mappings[ii]
, shouldSetAttribute = mapping.re && attributes.match(mapping.re);

//
// check if we are targetting a element only or attributes
//
if ('tag' in mapping && !this.attr.test(tagbody) && mapping.tag === tagname) {
tagbody = tagbody + data[mapping.dataKey];
continue;
}

tagbody = tagbody.replace(this.attr, function(str, key, value, a) {
var newdata = mapping.dataKey
? data[mapping.dataKey]
Expand Down
10 changes: 10 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ vows.describe('merge data into markup').addBatch({

return common.createTest('test-28', map);
}()
),

'(29) a tag match without attributes should replace the contents': (

function() {
var map = Plates.Map();
map.tag('div').use('foo');

return common.createTest('test-29', map);
}()
)
}

Expand Down
190 changes: 103 additions & 87 deletions test/browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ <h3 class="name">FooBar</h3>

</div>

</script>
<script type="test/fixture" id="test-29.html">
<div></div>

</script>
<script type="test/fixture" id="test-29.json">
{ "foo": "bar" }

</script>
<script type="test/fixture" id="test-29.out">
<div>bar</div>

</script>
<script type="test/fixture" id="test-3.html">
<div id="key1" style="visible: foobar > 1"><!-- foo --><div id="key2"></div><img src="/"/></div>
Expand Down Expand Up @@ -603,191 +615,195 @@ <h3 class="name">FooBar</h3>
});

test("(4) a tag with an arbitrary attribute that corresponds to a `data-key`.", function () {
var map = Plates.Map();
map.where('src').is('google.com').use('key1');
map.where('src').is('yahoo.com').use('key2');

var map = Plates.Map();
map.where('src').is('google.com').use('key1');
map.where('src').is('yahoo.com').use('key2');

return common.createTest('test-4', map);
return common.createTest('test-4', map);
});

test("(5) a tag with a class attribute whith a value that corresponds to a `data-key`.", function () {
var map = Plates.Map();
map.where('class').is('hidden').use('key1');
var map = Plates.Map();
map.where('class').is('hidden').use('key1');

return common.createTest('test-5', map);
return common.createTest('test-5', map);
});

test("(6) a tag with a class attribute whith a value that corresponds to a `data-key`, a second map item.", function () {
var map = Plates.Map();
map.where('class').is('hidden').use('key1');
map.where('href').is('/foo').use('key2').as('href');
var map = Plates.Map();
map.where('class').is('hidden').use('key1');
map.where('href').is('/foo').use('key2').as('href');

return common.createTest('test-6', map);
return common.createTest('test-6', map);
});

test("(7) a map that redefines the default matching attribute of `id`.", function () {
var map = Plates.Map({
where: 'class'
});
var map = Plates.Map({
where: 'class'
});

return common.createTest('test-7', map);
return common.createTest('test-7', map);
});

test("(8) using the insert shortcut.", function () {
var map = Plates.Map();
map.where('href').is('/').insert('newurl');

var map = Plates.Map();
map.where('href').is('/').insert('newurl');

return common.createTest('test-8', map);
return common.createTest('test-8', map);
});

test("(9) iterate a collection.", function () {
var map = Plates.Map();
map.where('href').is('/').insert('url');
var map = Plates.Map();
map.where('href').is('/').insert('url');

return common.createTest('test-9', map);
return common.createTest('test-9', map);
});

test("(10) a map that defines creating missing attributes.", function () {
var map = Plates.Map({
create: true
});
var map = Plates.Map({
create: true
});

map.className('logo').use('url').as('src');
map.where('name').is('first_name').use('fname').as('value');
map.where('name').is('last_name').use('lname').as('value');
map.className('logo').use('url').as('src');
map.where('name').is('first_name').use('fname').as('value');
map.where('name').is('last_name').use('lname').as('value');

return common.createTest('test-10', map);
return common.createTest('test-10', map);
});

test("(11) differing on \"is\" parameter only.", function () {
var map = Plates.Map();
map.where('name').is('method').use('method').as('value');
map.where('name').is('id').use('id').as('value');
var map = Plates.Map();
map.where('name').is('method').use('method').as('value');
map.where('name').is('id').use('id').as('value');

return common.createTest('test-11', map);
return common.createTest('test-11', map);
});

test("(12) iterate a collection of over an element with children.", function () {
return common.createTest('test-12');
return common.createTest('test-12');
});

test("(13) attributes can contain valid characters", function () {
var map = Plates.Map();
map.where('href').is('aA1-_:/&#1235; ').insert('test');
var map = Plates.Map();
map.where('href').is('aA1-_:/&#1235; ').insert('test');

return common.createTest('test-13', map);
return common.createTest('test-13', map);
});

test("(14) It should be able to iterate over collections with maps", function () {
var map = Plates.Map();
map.className("names").use("names");
var map = Plates.Map();
map.className("names").use("names");

return common.createTest('test-14', map);
return common.createTest('test-14', map);
});

test("(15) It should be able to iterate over collections with multiple maps", function () {
var map = Plates.Map();
map.where("href").is("placeholder").insert("link");
map.className("names").use("names");
var map = Plates.Map();
map.where("href").is("placeholder").insert("link");
map.className("names").use("names");

return common.createTest('test-15', map);
return common.createTest('test-15', map);
});

test("(16) It should be able to iterate over simple arrays", function () {
return common.createTest('test-16');
return common.createTest('test-16');
});

test("(17) It should be able to iterate over deeply nested objects", function () {
return common.createTest('test-17');
return common.createTest('test-17');
});

test("(18) It should be able to see the classnames properly", function () {
var map = Plates.Map();
map.className("username").use("username");
map.className("name").use("name");
var map = Plates.Map();
map.className("username").use("username");
map.className("name").use("name");

return common.createTest('test-18', map);
return common.createTest('test-18', map);
});

test("(19) Replace partial value with a new value", function () {
var map = Plates.Map();
map.where("href").is(/^bar/).replace(/bar/, 'bazz');
var map = Plates.Map();
map.where("href").is(/^bar/).replace(/bar/, 'bazz');

return common.createTest('test-19', map);
return common.createTest('test-19', map);
});

test("(20) Replace partial value with a new value from the data object", function () {
var map = Plates.Map();
map.where("href").has(/^bar/).insert('test');
var map = Plates.Map();
map.where("href").has(/^bar/).insert('test');

return common.createTest('test-20', map);
return common.createTest('test-20', map);
});

test("(21) Remove should remove the whole element", function () {
var map = Plates.Map();
map.className('remove').remove();
var map = Plates.Map();
map.className('remove').remove();

return common.createTest('test-21', map);
return common.createTest('test-21', map);
});

test("(22) Remove should remove self closing elements", function () {
var map = Plates.Map();
map.where('type').is('email').remove();
var map = Plates.Map();
map.where('type').is('email').remove();

return common.createTest('test-22', map);
return common.createTest('test-22', map);
});

test("(23) Remove should remove self closing elements without optional ending slash", function () {
var map = Plates.Map();
map.where('type').is('email').remove();
var map = Plates.Map();
map.where('type').is('email').remove();

return common.createTest('test-23', map);
return common.createTest('test-23', map);
});

test("(24) Should append new templates", function () {
var map = Plates.Map();
map.className('insert').append('<div class="trolling"></div>');
var map = Plates.Map();
map.className('insert').append('<div class="trolling"></div>');

return common.createTest('test-24', map);
return common.createTest('test-24', map);
});

test("(25) New templates should also process map and custom data", function () {
var map = Plates.Map();
var partial = Plates.Map();
var map = Plates.Map();
var partial = Plates.Map();

partial.className('trolling').to('boink');
map.className('insert').append('<div class="trolling"></div>', { boink: 'moo' }, partial);
partial.className('trolling').to('boink');
map.className('insert').append('<div class="trolling"></div>', { boink: 'moo' }, partial);

return common.createTest('test-25', map);
return common.createTest('test-25', map);
});

test("(26) When the data for the partial was provided as a string, get it from the parent data structure", function () {
var map = Plates.Map();
var partial = Plates.Map();
var map = Plates.Map();
var partial = Plates.Map();

partial.className('trolling').to('boink');
map.className('insert').append('<div class="trolling"></div>', 'partial', partial);
partial.className('trolling').to('boink');
map.className('insert').append('<div class="trolling"></div>', 'partial', partial);

return common.createTest('test-26', map);
return common.createTest('test-26', map);
});

test("(27) append should read from file system if no template has been provided", function () {
var map = Plates.Map();
var partial = Plates.Map();
var map = Plates.Map();
var partial = Plates.Map();

map.className('insert').append('partial-1.html');
map.className('insert').append('partial-1.html');

return common.createTest('test-27', map);
return common.createTest('test-27', map);
});

test("(28) it should repeat the partial for each item in the array", function () {
var map = Plates.Map();
var partial = Plates.Map();
var map = Plates.Map();
var partial = Plates.Map();

map.className('insert').append('partial-1.html', [{}, {}, {}]);
map.className('insert').append('partial-1.html', [{}, {}, {}]);

return common.createTest('test-28', map);
});
test("(29) a tag match without attributes should replace the contents", function () {
var map = Plates.Map();
map.tag('div').use('foo');

return common.createTest('test-28', map);
return common.createTest('test-29', map);
});
</script>
1 change: 1 addition & 0 deletions test/fixtures/test-29.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div></div>
1 change: 1 addition & 0 deletions test/fixtures/test-29.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo": "bar" }
1 change: 1 addition & 0 deletions test/fixtures/test-29.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>bar</div>

0 comments on commit 550b4a4

Please sign in to comment.