<commit_before>/**
 * Analyzes DOM complexity
 */
exports.version = '0.2';

exports.module = function(phantomas) {

	// HTML size
	phantomas.on('report', function() {
		phantomas.setMetricEvaluate('bodyHTMLSize', function() {
			return document.body.innerHTML.length;
		});

		phantomas.evaluate(function() {
			(function(phantomas) {
				var runner = new phantomas.nodeRunner(),
					whitespacesRegExp = /^\s+$/;

				var metrics = {
					comments: 0,
					hiddenContent: 0,
					whitespaces: 0
				};

				// include all nodes
				runner.isSkipped = function(node) {
					return false;
				};

				runner.walk(document.body, function(node) {
					switch (node.nodeType) {
						case Node.COMMENT_NODE:
							metrics.comments += node.textContent.length + 7; // '<!--' + '-->'.length
							break;

						case Node.ELEMENT_NODE:
							// ignore inline <script> tags
							if (node.nodeName === 'SCRIPT') {
								return false;
							}

							// @see https://developer.mozilla.org/en/DOM%3awindow.getComputedStyle
							var styles = window.getComputedStyle(node);

							if (styles && styles.getPropertyValue('display') === 'none') {
								//console.log(node.innerHTML);
								metrics.hiddenContent += node.innerHTML.length;

								// don't run for child nodes as they're hidden as well
								return false;
							}
							break;

						case Node.TEXT_NODE:
							if (whitespacesRegExp.test(node.textContent)) {
								metrics.whitespaces += node.textContent.length;
							}
							break;
					}
				});

				// store metrics
				phantomas.DOMmetrics = metrics;

			}(window.phantomas));
		});

		// total length of HTML comments (including <!-- --> brackets)
		phantomas.setMetricEvaluate('commentsSize', function() {
			return window.phantomas.DOMmetrics.comments;
		});

		// total length of HTML of hidden elements (i.e. display: none)
		phantomas.setMetricEvaluate('hiddenContentSize', function() {
			return window.phantomas.DOMmetrics.hiddenContent;
		});

		// total length of text nodes with whitespaces only (i.e. pretty formatting of HTML)
		phantomas.setMetricEvaluate('whiteSpacesSize', function() {
			return window.phantomas.DOMmetrics.whitespaces;
		});

		// count <iframe> tags
		phantomas.setMetricEvaluate('iframesCount', function() {
			return document.querySelectorAll('iframe').length;
		});
	});
};<commit_msg>domComplexity: add 'DOMelementsCount' and 'DOMelementsMaxDepth' metrics<commit_after>/**
 * Analyzes DOM complexity
 */
exports.version = '0.2';

exports.module = function(phantomas) {

	// HTML size
	phantomas.on('report', function() {
		phantomas.setMetricEvaluate('bodyHTMLSize', function() {
			return document.body.innerHTML.length;
		});

		phantomas.evaluate(function() {
			(function(phantomas) {
				var runner = new phantomas.nodeRunner(),
					whitespacesRegExp = /^\s+$/;

				var metrics = {
					nodes: 0,
					comments: 0,
					hiddenContent: 0,
					whitespaces: 0,
					maxDepth: 0
				};

				// include all nodes
				runner.isSkipped = function(node) {
					return false;
				};

				runner.walk(document.body, function(node, depth) {
					switch (node.nodeType) {
						case Node.COMMENT_NODE:
							metrics.comments += node.textContent.length + 7; // '<!--' + '-->'.length
							break;

						case Node.ELEMENT_NODE:
							metrics.nodes++;
							metrics.maxDepth = Math.max(metrics.maxDepth, depth);

							// ignore inline <script> tags
							if (node.nodeName === 'SCRIPT') {
								return false;
							}

							// @see https://developer.mozilla.org/en/DOM%3awindow.getComputedStyle
							var styles = window.getComputedStyle(node);

							if (styles && styles.getPropertyValue('display') === 'none') {
								//console.log(node.innerHTML);
								metrics.hiddenContent += node.innerHTML.length;

								// don't run for child nodes as they're hidden as well
								return false;
							}
							break;

						case Node.TEXT_NODE:
							if (whitespacesRegExp.test(node.textContent)) {
								metrics.whitespaces += node.textContent.length;
							}
							break;
					}
				});

				// store metrics
				phantomas.DOMmetrics = metrics;

			}(window.phantomas));
		});

		// total length of HTML comments (including <!-- --> brackets)
		phantomas.setMetricEvaluate('commentsSize', function() {
			return window.phantomas.DOMmetrics.comments;
		});

		// total length of HTML of hidden elements (i.e. display: none)
		phantomas.setMetricEvaluate('hiddenContentSize', function() {
			return window.phantomas.DOMmetrics.hiddenContent;
		});

		// total length of text nodes with whitespaces only (i.e. pretty formatting of HTML)
		phantomas.setMetricEvaluate('whiteSpacesSize', function() {
			return window.phantomas.DOMmetrics.whitespaces;
		});

		// count all tags
		phantomas.setMetricEvaluate('DOMelementsCount', function() {
			return window.phantomas.DOMmetrics.nodes;
		});

		phantomas.setMetricEvaluate('DOMelementMaxDepth', function() {
			return window.phantomas.DOMmetrics.maxDepth;
		});

		// count <iframe> tags
		phantomas.setMetricEvaluate('iframesCount', function() {
			return document.querySelectorAll('iframe').length;
		});
	});
};
{'fraction_non_alphanumeric': 0.13571845226521487, 'fraction_numerical': 0.003111024693758507, 'mean_word_length': 2.899924184988628, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3732482', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1505, 'n_words': 319}