html-compare
Version:
html-compare package
31 lines (25 loc) • 713 B
JavaScript
/*jshint node:true*/
"use strict";
module.exports = stringifyNode;
var nodeType = require('../util/cheerio-utils').nodeType;
/**
* Takes a cheerio node and returns a concise string representation of it,
* regardless of type. Also handles corner-cases like printing 'undefined'
* or 'null' as '[nothing]'.
*
* @param $node the cheerio object for the node
* @returns {string} a printable string
*/
function stringifyNode($node) {
if (!$node || !$node.length)
return "[nothing]";
switch(nodeType($node)) {
case 'text':
var text = $node.text();
return '"' + text + '"';
default:
var $ = $node.cheerio;
var $clone = $node.clone();
return $.html($clone);
}
}