2011-11-29 15:11:51 +00:00
|
|
|
/* Perform post-processing steps on an already-built HTML DOM. */
|
|
|
|
|
|
|
|
var isBlock = function isBlock (name) {
|
|
|
|
switch (name.toLowerCase()) {
|
|
|
|
case 'div':
|
|
|
|
case 'table':
|
|
|
|
case 'td':
|
|
|
|
case 'tr':
|
|
|
|
case 'tbody':
|
|
|
|
case 'p':
|
|
|
|
case 'ul':
|
|
|
|
case 'ol':
|
|
|
|
case 'li':
|
|
|
|
case 'dl':
|
|
|
|
case 'dt':
|
|
|
|
case 'dd':
|
|
|
|
case 'img': // hmm!
|
|
|
|
case 'pre':
|
|
|
|
case 'center':
|
|
|
|
case 'blockquote':
|
2011-12-04 19:23:24 +00:00
|
|
|
case 'h1':
|
|
|
|
case 'h2':
|
|
|
|
case 'h3':
|
|
|
|
case 'h4':
|
|
|
|
case 'h5':
|
|
|
|
case 'h6':
|
2011-11-29 15:11:51 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-30 12:28:45 +00:00
|
|
|
// Wrap all top-level inline elements in paragraphs. This should also be
|
|
|
|
// applied inside block-level elements, but in that case the first paragraph
|
|
|
|
// usually remains plain inline.
|
2011-11-29 15:11:51 +00:00
|
|
|
var process_inlines_in_p = function ( document ) {
|
2011-11-30 12:28:45 +00:00
|
|
|
// document.body does not always work in jsdom, so work around it.
|
2011-11-29 15:11:51 +00:00
|
|
|
var body = document.getElementsByTagName('body')[0],
|
2011-11-30 12:28:45 +00:00
|
|
|
newP = document.createElement('p'),
|
2011-11-29 15:11:51 +00:00
|
|
|
cnodes = body.childNodes,
|
2011-11-30 12:28:45 +00:00
|
|
|
haveInlines = false,
|
|
|
|
deleted = 0;
|
2011-11-29 15:11:51 +00:00
|
|
|
|
|
|
|
function isElementContentWhitespace ( e ) {
|
|
|
|
return (e.data.match(/^[ \r\n\t]*$/) !== null);
|
|
|
|
}
|
|
|
|
|
2011-11-30 12:28:45 +00:00
|
|
|
for(var i = 0, length = cnodes.length; i < length; i++) {
|
|
|
|
var child = cnodes[i - deleted],
|
2011-11-29 15:11:51 +00:00
|
|
|
ctype = child.nodeType;
|
|
|
|
//console.log(child + ctype);
|
2011-11-30 13:40:17 +00:00
|
|
|
if ((ctype === 3 && (haveInlines || !isElementContentWhitespace(child))) ||
|
2011-11-30 12:28:45 +00:00
|
|
|
(ctype !== 3 && // text
|
|
|
|
ctype !== 8 && // comment
|
|
|
|
!isBlock(child.nodeName))) {
|
2011-11-29 15:11:51 +00:00
|
|
|
// text node
|
2011-11-30 12:28:45 +00:00
|
|
|
newP.appendChild(child);
|
|
|
|
haveInlines = true;
|
|
|
|
deleted++;
|
|
|
|
} else if (haveInlines) {
|
|
|
|
body.insertBefore(newP, child);
|
|
|
|
newP = document.createElement('p');
|
|
|
|
haveInlines = false;
|
|
|
|
}
|
2011-11-29 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 12:28:45 +00:00
|
|
|
if (haveInlines) {
|
|
|
|
body.appendChild(newP);
|
2011-11-29 15:11:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function DOMPostProcessor () {
|
|
|
|
this.processors = [process_inlines_in_p];
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMPostProcessor.prototype.doPostProcess = function ( document ) {
|
|
|
|
for(var i = 0; i < this.processors.length; i++) {
|
|
|
|
this.processors[i](document);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.DOMPostProcessor = DOMPostProcessor;
|
|
|
|
}
|