Skip to content

Commit

Permalink
Removed forceDoctype, changed default doctype
Browse files Browse the repository at this point in the history
With the forceDoctype option removed, the compiler now gives presedence
to the doctype set in the view. The default doctype is used if no
doctype is set in the view, or the empty doctype declaration is used. In
the case that no doctype is specified, the default will be added before
the html tag if it is the first tag in the view.

Signed-off-by: Tj Holowaychuk <tj@vision-media.ca>
  • Loading branch information
Jeremy Larkin authored and tj committed May 9, 2011
1 parent 6c115e7 commit e7f2efb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
47 changes: 17 additions & 30 deletions lib/compiler.js
Expand Up @@ -50,12 +50,11 @@ var Compiler = module.exports = function Compiler(node, options) {
this.options = options = options || {};
this.node = node;

this.hasRenderedDoctype = false;
this.isTopLevel = (options.isTopLevel || options.isLayout);
this.hasCompiledDoctype = false;
this.hasCompiledTag = false;

if (options.doctype) {
this.setDoctype(options.doctype);
this.forceDoctype = options.forceDoctype;
}
};

Expand Down Expand Up @@ -93,20 +92,6 @@ Compiler.prototype = {
this.terse = ("5" === name || "html" === name);
this.xml = 0 == this.doctype.indexOf('<?xml');
},

/**
* Checks if the doctype needs to be rendered. This will only return
* `true` if the view being compiled is a root-level view. This checks
* for the `isTopLevel` option. Express sets the `isLayout` option to
* `true`, and this option is treated equivalently.
*
* @return {boolean}
* @api public
*/

needsDoctype: function(){
return this.isTopLevel && !this.hasRenderedDoctype;
},

/**
* Buffer the given `str` optionally escaped.
Expand Down Expand Up @@ -155,11 +140,6 @@ Compiler.prototype = {
visitNode: function(node){
var name = node.constructor.name
|| node.constructor.toString().match(/function ([^(\s]+)()/)[1];

if (name !== 'Doctype' && this.needsDoctype()) {
this.visitDoctype();
}

return this['visit' + name](node);
},

Expand Down Expand Up @@ -187,15 +167,15 @@ Compiler.prototype = {
*/

visitDoctype: function(doctype){
if (!this.forceDoctype) {
if (doctype && doctype.val) {
this.setDoctype(doctype.val);
} else if (!this.doctype) {
this.setDoctype('default');
}
if (doctype && (doctype.val || !this.doctype)) {
this.setDoctype(doctype.val || 'default');
}

if (this.doctype) {
this.buffer(this.doctype);
}
this.buffer(this.doctype);
this.hasRenderedDoctype = true;

this.hasCompiledDoctype = true;
},

/**
Expand All @@ -209,6 +189,13 @@ Compiler.prototype = {
visitTag: function(tag){
var name = tag.name;

if (!this.hasCompiledTag) {
if (!this.hasCompiledDoctype && name === 'html') {
this.visitDoctype();
}
this.hasCompiledTag = true;
}

if (~selfClosing.indexOf(name) && !this.xml) {
this.buffer('<' + name);
this.visitAttributes(tag.attrs);
Expand Down
6 changes: 3 additions & 3 deletions test/jade.test.js
Expand Up @@ -36,9 +36,9 @@ module.exports = {
assert.equal('<?xml version="1.0" encoding="utf-8" ?>', render('!!! xml'));
assert.equal('<!DOCTYPE html>', render('!!! 5'));
assert.equal('<!DOCTYPE html>', render('!!!', { doctype:'html' }));
assert.equal('<!DOCTYPE html>', render('!!! strict', { doctype:'html', forceDoctype:true }));
assert.equal('<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html></html>', render('html', { isTopLevel:true }));
assert.equal('<!DOCTYPE html><html></html>', render('html', { doctype:'html', isTopLevel:true }));
assert.equal('<!DOCTYPE html>', render('!!! html', { doctype:'xml' }));
assert.equal('<html></html>', render('html'));
assert.equal('<!DOCTYPE html><html></html>', render('html', { doctype:'html' }));
},

'test Buffers': function(assert){
Expand Down

0 comments on commit e7f2efb

Please sign in to comment.