Skip to content

Commit

Permalink
added closeOpenedElement callback and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
tmont committed Jun 5, 2012
1 parent 41e7c0e commit 00ad88c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ exports.create = function(raw, options) {
});

context.callbacks = {};
[ 'openElement', 'closeElement', 'attribute', 'comment', 'cdata', 'text', 'docType', 'xmlProlog', ].forEach(function(value) {
var types = [ 'openElement', 'closeElement', 'attribute', 'comment', 'cdata', 'text', 'docType', 'xmlProlog', 'closeOpenedElement' ];
types.forEach(function(value) {
context.callbacks[value] = options[value] || function() {
};
});
Expand Down
6 changes: 4 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ function readAttributes(context, isXml) {
if (context.current === '/') {
//self closing tag "/>"
context.readUntilNonWhitespace();
context.callbacks.closeElement('');
context.read();
context.callbacks.closeOpenedElement('/>');
} else if (isXml) {
//xml closing "?>"
context.read(2);
context.callbacks.closeElement('');
context.callbacks.closeOpenedElement('?>');
} else {
//normal closing ">"
context.read();
context.callbacks.closeOpenedElement('>');
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/files/good-expected.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!doctype html>

<html>
<head>
<title>tommy montgomery </title>
Expand All @@ -18,6 +19,6 @@ <h1><a href="/"><span>t</span>ommy <span>mont</span>gomery</a></h1>
</div>
</div>

<script src="/media/js/d3.min.js" type="text/javascript"></script>
<script src="/media/js/d3.min.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/files/good.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ <h1><a href="/"><span>t</span>ommy <span>mont</span>gomery</a></h1>
</div>
</div>

<script src="/media/js/d3.min.js" type="text/javascript"></script>
<script src="/media/js/d3.min.js"></script>
</body>
</html>
57 changes: 35 additions & 22 deletions tests/integration-tests.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
var should = require('should');
var helpers = require('./helpers');

//describe('Integration', function() {
// it('real life HTML document', function() {
// var content = '';
//
// helpers.htmlParser.parseFile(__dirname + '/files/good.html', null, {
// docType: function(value) {
// content += '<!doctype ' + value + '>\n';
// },
//
// openElement: function(name) {
// switch (name) {
// case 'link':
// case 'meta':
//
//
// }
// }
// }, function(err) {
// should.not.exist(err);
// });
// });
//});
describe('Integration', function() {
it('real life HTML document', function(done) {
var content = '';
helpers.parser.parseFile(__dirname + '/files/good.html', 'utf8', {
docType: function(value) {
content += '<!doctype ' + value + '>\n';
},

openElement: function(name) {
content += '<' + name;
},

closeOpenedElement: function(token) {
content += token;
},

closeElement: function(name) {
content += '</' + name + '>';
},

attribute: function(name, value) {
content += ' ' + name + '="' + value.replace(/"/g, '&quot;') + '"';
},

text: function(value) {
content += value;
}
}, function(err) {
should.not.exist(err);
var expected = require('fs').readFileSync(__dirname + '/files/good-expected.html', 'utf8');
content.should.equal(expected);
done();
});
});
});
6 changes: 3 additions & 3 deletions tests/open-and-close-tag-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ describe('opening and closing tags', function() {
textCount.should.equal(1);
});

it('self closing tag should emit closeElement', function() {
it('self closing tag should emit closeOpenedElement', function() {
var closeCount = 0, openCount = 0;
helpers.parseString('<foo />', {
openElement: function(name) {
name.should.equal('foo');
openCount++;
},
closeElement: function(name) {
name.should.equal('');
closeOpenedElement: function(name) {
name.should.equal('/>');
closeCount++;
}
});
Expand Down
4 changes: 2 additions & 2 deletions tests/xml-prolog-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('XML Prologs', function() {
openCount++;
},
closeElement: function(name) {
name.should.equal(closeCount === 0 ? '' : 'foo');
name.should.equal('foo');
closeCount++;
},
attribute: function(name, value) {
Expand All @@ -75,6 +75,6 @@ describe('XML Prologs', function() {

prologCount.should.equal(1);
openCount.should.equal(1);
closeCount.should.equal(2);
closeCount.should.equal(1);
});
});

0 comments on commit 00ad88c

Please sign in to comment.