summaryrefslogtreecommitdiff
path: root/source/features/show-whitespace.tsx
blob: b06f0b4038c4a89ab92dc94a0a11263899c661b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import './show-whitespace.css';
import React from 'dom-chef';
import select from 'select-dom';
import features from '../libs/features';
import onPrFileLoad from '../libs/on-pr-file-load';
import onNewComments from '../libs/on-new-comments';
import getTextNodes from '../libs/get-text-nodes';

function showWhiteSpacesOn(line: Element): void {
	const textNodes = getTextNodes(line);

	for (const textNode of textNodes) {
		const textContent = textNode.textContent!;
		if (textContent.length === 0 || !(textContent.includes(' ') || textContent.includes('\t'))) {
			continue;
		}

		const fragment = document.createDocumentFragment();

		let lastEncounteredCharType;
		let charType: 'space' | 'tab' | 'other';
		let node;

		for (const char of textContent) {
			if (char === ' ') {
				charType = 'space';
			} else if (char === '\t') {
				charType = 'tab';
			} else {
				charType = 'other';
			}

			if (node && lastEncounteredCharType === charType) {
				node.textContent += char;

				if (charType === 'space') {
					node.dataset.rghSpaces += '·';
				} else if (charType === 'tab') {
					node.dataset.rghTabs += '→';
				}
			} else {
				if (node) {
					fragment.append(node);
				}

				if (charType === 'space') {
					node = <span className="rgh-ws-char rgh-space-char" data-rgh-spaces="·">{char}</span>;
				} else if (charType === 'tab') {
					node = <span className="rgh-ws-char rgh-tab-char" data-rgh-tabs="→">{char}</span>;
				} else {
					node = <>{char}</>;
				}
			}

			lastEncounteredCharType = charType;
		}

		if (node) {
			fragment.append(node);
		}

		textNode.replaceWith(fragment);
	}
}

async function run(): Promise<void> {
	const tables = select.all([
		'table.js-file-line-container:not(.rgh-showing-whitespace)', // Single blob file, and gist
		'.file table.diff-table:not(.rgh-showing-whitespace)', // Split and unified diffs
		'.file table.d-table:not(.rgh-showing-whitespace)' // "Suggested changes" in PRs
	].join());

	for (const table of tables) {
		table.classList.add('rgh-showing-whitespace');

		for (const [i, line] of select.all('.blob-code-inner', table).entries()) {
			showWhiteSpacesOn(line);

			if (i % 100 === 0) {
				await new Promise(resolve => setTimeout(resolve)); // eslint-disable-line no-await-in-loop
			}
		}
	}
}

function init(): void {
	run();
	onNewComments(run);
	onPrFileLoad(run);
}

features.add({
	id: __featureName__,
	description: 'Shows whitespace characters.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/61187598-f9118380-a6a5-11e9-985a-990a7f798805.png',
	include: [
		features.hasCode
	],
	load: features.onAjaxedPages,
	init
});