-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathlistNumbers.js
175 lines (165 loc) · 5.8 KB
/
listNumbers.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
// Manually prefix algorithm step list items with hidden counter representations
// corresponding with their markers so they get selected and copied with content.
// We read list-style-type to avoid divergence with the style sheet, but
// for efficiency assume that all lists at the same nesting depth use the same
// style (except for those associated with replacement steps).
// We also precompute some initial items for each supported style type.
// https://w3c.github.io/csswg-drafts/css-counter-styles/
const lowerLetters = Array.from({ length: 26 }, (_, i) =>
String.fromCharCode('a'.charCodeAt(0) + i),
);
// Implement the lower-alpha 'alphabetic' algorithm,
// adjusting for indexing from 0 rather than 1.
// https://w3c.github.io/csswg-drafts/css-counter-styles/#simple-alphabetic
// https://w3c.github.io/csswg-drafts/css-counter-styles/#alphabetic-system
const lowerAlphaTextForIndex = i => {
let S = '';
for (const N = lowerLetters.length; i >= 0; i--) {
S = lowerLetters[i % N] + S;
i = Math.floor(i / N);
}
return S;
};
const weightedLowerRomanSymbols = Object.entries({
m: 1000,
cm: 900,
d: 500,
cd: 400,
c: 100,
xc: 90,
l: 50,
xl: 40,
x: 10,
ix: 9,
v: 5,
iv: 4,
i: 1,
});
// Implement the lower-roman 'additive' algorithm,
// adjusting for indexing from 0 rather than 1.
// https://w3c.github.io/csswg-drafts/css-counter-styles/#simple-numeric
// https://w3c.github.io/csswg-drafts/css-counter-styles/#additive-system
const lowerRomanTextForIndex = i => {
let value = i + 1;
let S = '';
for (const [symbol, weight] of weightedLowerRomanSymbols) {
if (!value) break;
if (weight > value) continue;
const reps = Math.floor(value / weight);
S += symbol.repeat(reps);
value -= weight * reps;
}
return S;
};
// Memoize pure index-to-text functions with an exposed cache for fast retrieval.
const makeCounter = (pureGetTextForIndex, precomputeCount = 30) => {
const cache = Array.from({ length: precomputeCount }, (_, i) => pureGetTextForIndex(i));
const getTextForIndex = i => {
if (i >= cache.length) cache[i] = pureGetTextForIndex(i);
return cache[i];
};
return { getTextForIndex, cache };
};
const counterByStyle = {
__proto__: null,
decimal: makeCounter(i => String(i + 1)),
'lower-alpha': makeCounter(lowerAlphaTextForIndex),
'upper-alpha': makeCounter(i => lowerAlphaTextForIndex(i).toUpperCase()),
'lower-roman': makeCounter(lowerRomanTextForIndex),
'upper-roman': makeCounter(i => lowerRomanTextForIndex(i).toUpperCase()),
};
const fallbackCounter = makeCounter(() => '?');
const counterByDepth = [];
function addStepNumberText(
ol,
depth = 0,
indent = '',
special = [...ol.classList].some(c => c.startsWith('nested-')),
) {
let counter = !special && counterByDepth[depth];
if (!counter) {
const counterStyle = getComputedStyle(ol)['list-style-type'];
counter = counterByStyle[counterStyle];
if (!counter) {
console.warn('unsupported list-style-type', {
ol,
counterStyle,
id: ol.closest('[id]')?.getAttribute('id'),
});
counterByStyle[counterStyle] = fallbackCounter;
counter = fallbackCounter;
}
if (!special) {
counterByDepth[depth] = counter;
}
}
const { cache, getTextForIndex } = counter;
let i = (Number(ol.getAttribute('start')) || 1) - 1;
for (const li of ol.children) {
const marker = document.createElement('span');
const markerText = i < cache.length ? cache[i] : getTextForIndex(i);
const extraIndent = ' '.repeat(markerText.length + 2);
marker.textContent = `${indent}${markerText}. `;
marker.setAttribute('aria-hidden', 'true');
marker.setAttribute('class', 'list-marker');
const attributesContainer = li.querySelector('.attributes-tag');
if (attributesContainer == null) {
li.prepend(marker);
} else {
attributesContainer.insertAdjacentElement('afterend', marker);
}
for (const sublist of li.querySelectorAll(':scope > ol')) {
addStepNumberText(sublist, depth + 1, indent + extraIndent, special);
}
i++;
}
}
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('emu-alg > ol').forEach(ol => {
addStepNumberText(ol);
});
});
// Omit indendation when copying a single algorithm step.
document.addEventListener('copy', evt => {
// Construct a DOM from the selection.
const doc = document.implementation.createHTMLDocument('');
const domRoot = doc.createElement('div');
const html = evt.clipboardData.getData('text/html');
if (html) {
domRoot.innerHTML = html;
} else {
const selection = getSelection();
const singleRange = selection?.rangeCount === 1 && selection.getRangeAt(0);
const container = singleRange?.commonAncestorContainer;
if (!container?.querySelector?.('.list-marker')) {
return;
}
domRoot.append(singleRange.cloneContents());
}
// Preserve the indentation if there is no hidden list marker, or if selection
// of more than one step is indicated by either multiple such markers or by
// visible text before the first one.
const listMarkers = domRoot.querySelectorAll('.list-marker');
if (listMarkers.length !== 1) {
return;
}
const treeWalker = document.createTreeWalker(domRoot, undefined, {
acceptNode(node) {
return node.nodeType === Node.TEXT_NODE || node === listMarkers[0]
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_SKIP;
},
});
while (treeWalker.nextNode()) {
const node = treeWalker.currentNode;
if (node.nodeType === Node.ELEMENT_NODE) break;
if (/\S/u.test(node.data)) return;
}
// Strip leading indentation from the plain text representation.
evt.clipboardData.setData('text/plain', domRoot.textContent.trimStart());
if (!html) {
evt.clipboardData.setData('text/html', domRoot.innerHTML);
}
evt.preventDefault();
});