Skip to content

Commit

Permalink
implemented doctype parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tmont committed Jun 5, 2012
1 parent eb596b3 commit 15476bd
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports.create = function(raw, options) {
});

context.callbacks = {};
[ 'openElement', 'closeElement', 'attribute', 'comment', 'cdata', 'text' ].forEach(function(value) {
[ 'openElement', 'closeElement', 'attribute', 'comment', 'cdata', 'text', 'docType', 'xmlProlog', ].forEach(function(value) {
context.callbacks[value] = options[value] || function() {
};
});
Expand Down
17 changes: 15 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function parseCData(context) {

var match = /^([\s\S]*?)(?:$|]]>)/.exec(context.substring);
var value = match[1];
context.read(value.length + match[0].length);
context.read(match[0].length);
context.callbacks.cdata(value);
}

Expand All @@ -61,10 +61,20 @@ function parseComment(context) {

var match = /^([\s\S]*?)(?:$|-->)/.exec(context.substring);
var value = match[1];
context.read(value.length + match[0].length);
context.read(match[0].length);
context.callbacks.comment(value);
}

function parseDocType(context) {
//read "!doctype"
context.read(8);

var match = /^\s*([\s\S]*?)(?:$|>)/.exec(context.substring);
var value = match[1];
context.read(match[0].length);
context.callbacks.docType(value);
}

function appendText(value, context) {
context.text += value;
}
Expand Down Expand Up @@ -97,6 +107,9 @@ function parseNext(context) {
} else if (/^!--/.test(context.substring)) {
callbackText(context);
parseComment(context);
} else if (/^!doctype/i.test(context.substring)) {
callbackText(context);
parseDocType(context);
} else {
//malformed html
context.read();
Expand Down
65 changes: 65 additions & 0 deletions tests/doctype-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var should = require('should');
var helpers = require('./helpers');

describe('DocTypes', function() {
it('at beginning of document', function() {
var docTypeCount = 0;
helpers.parseString('<!doctype html>', {
docType: function(value) {
value.should.equal('html');
docTypeCount++;
}
});

docTypeCount.should.equal(1);
});

it('in middle of document', function() {
var docTypeCount = 0, openCount = 0, closeCount = 0;
helpers.parseString('<foo><!doctype html></foo>', {
openElement: function(name) {
name.should.equal('foo');
openCount++;
},
closeElement: function(name) {
name.should.equal('foo');
closeCount++;
},
docType: function(value) {
openCount.should.equal(1);
closeCount.should.equal(0);
value.should.equal('html');
docTypeCount++;
}
});

docTypeCount.should.equal(1);
openCount.should.equal(1);
closeCount.should.equal(1);
});

it('with line breaks', function() {
var docTypeCount = 0;
helpers.parseString('<!doctype foo\nbar>', {
docType: function(value) {
value.should.equal('foo\nbar');
docTypeCount++;
}
});

docTypeCount.should.equal(1);
});

it('are case insensitive', function() {
var docTypeCount = 0;
helpers.parseString('<!DoCtyPE html>', {
docType: function(value) {
value.should.equal('html');
docTypeCount++;
}
});

docTypeCount.should.equal(1);
});

});
41 changes: 41 additions & 0 deletions tests/files/good.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<html>

<head>
<title>tommy montgomery </title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<link rel="stylesheet" href="/media/css/global.css"/>
<link rel="shortcut icon" type="image/png" href="/favicon.ico"/>
</head>

<body>
<div id="wrapper">
<div id="header">
<h1><a href="/"><span>t</span>ommy <span>mont</span>gomery</a></h1>
</div>

<div id="main-content">
<div id="menu"></div>

</div>
</div>

<script src="/media/js/d3.min.js" type="text/javascript"></script>
<script src="/media/js/mainmenu.js" type="text/javascript"></script>


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6733824-1");
pageTracker._trackPageview();
}
catch (err) {
}
</script>
</body>

</html>
8 changes: 8 additions & 0 deletions tests/integration-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('should');
var helpers = require('./helpers');

describe('Integration', function() {
it('real life HTML document', function() {

});
});

0 comments on commit 15476bd

Please sign in to comment.