-
Notifications
You must be signed in to change notification settings - Fork 1
/
mathml-to-clipboard.js
213 lines (201 loc) · 7.61 KB
/
mathml-to-clipboard.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*global window, document, Node, XMLSerializer */
"use strict";
var transformMathMLToAsciiMath = require("mathml-to-asciimath");
var isBlock = function (display) {
switch (display) {
case "inline":
case "inline-block":
case "inline-flex":
case "inline-grid":
case "inline-table":
case "none":
case "table-column":
case "table-column-group":
case "table-cell":
return false;
}
return true;
};
var getNodeLength = function (container) {
if (container.nodeType === Node.TEXT_NODE) {
return container.data.length;
}
if (container.nodeType === Node.ELEMENT_NODE) {
var count = 0;
var child = container.firstChild;
while (child != null) {
child = child.nextSibling;
count += 1;
}
return count;
}
return undefined;
};
var isBoundaryPoint = function (container, offset, which, node) {
if (which === "end" && offset !== getNodeLength(container) || which === "start" && offset !== 0) {
return false;
}
for (var x = container; x !== node; x = x.parentNode) {
var y = which === "end" ? x.nextSibling : (which === "start" ? x.previousSibling : null);
// https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace#Whitespace_helper_functions
while (y != null && y.nodeType !== Node.ELEMENT_NODE && (y.nodeType !== Node.TEXT_NODE || /^[\t\n\f\r\u0020]*$/.test(y.data))) {
y = which === "end" ? y.nextSibling : (which === "start" ? y.previousSibling : null);
}
if (y != null) {
return false;
}
}
return true;
};
var getChildNode = function (container, offset, which, node) {
var child = null;
var x = container;
while (x !== node) {
child = x;
x = x.parentNode;
}
if (child != null) {
child = which === "end" ? child.nextSibling : (which === "start" ? child : null);
} else {
var i = -1;
child = container.firstChild; // node === container
while (++i < offset) {
child = child.nextSibling;
}
}
return child;
};
var serialize = function (range, isLineStart) {
// big thanks to everyone
// see https://github.com/timdown/rangy/blob/master/src/modules/rangy-textrange.js
// see https://github.com/WebKit/webkit/blob/ec2f4d46b97bb20fd0877b1f4b5ec50f7b9ec521/Source/WebCore/editing/TextIterator.cpp#L1188
// see https://github.com/jackcviers/Rangy/blob/master/spec/innerText.htm
var node = range.commonAncestorContainer;
var startContainer = range.startContainer;
var startOffset = range.startOffset;
var endContainer = range.endContainer;
var endOffset = range.endOffset;
if (node.nodeType === Node.TEXT_NODE) {
if (node !== startContainer || node !== endContainer) {
throw new TypeError();
}
var data = node.data.slice(startOffset, endOffset);
data = data.replace(/[\t\n\f\r\u0020]+/g, " ");
if (isLineStart) {
data = data.replace(/^[\t\n\f\r\u0020]/g, "");
}
return data;
}
if (node.nodeType === Node.ELEMENT_NODE) {
var display = window.getComputedStyle(node, null).display;
if (display === "none") {
return "";
}
var result = "";
if (isBlock(display) && !isLineStart) {
result += "\n";
isLineStart = true;
}
var x = undefined;
if (isBoundaryPoint(startContainer, startOffset, "start", node) &&
isBoundaryPoint(endContainer, endOffset, "end", node)) {
var tagName = node.tagName.toLowerCase();
if (tagName === "math" || (tagName !== "mtext" && node.namespaceURI === "http://www.w3.org/1998/Math/MathML")) {
x = transformMathMLToAsciiMath(node);
}
if (tagName === "br") {
x = "\n";
}
}
if (x != undefined) {
result += x;
} else {
var startChildNode = getChildNode(startContainer, startOffset, "start", node);
var endChildNode = getChildNode(endContainer, endOffset, "end", node);
var childNode = startChildNode;
while (childNode !== endChildNode) {
var childNodeRange = {
startContainer: childNode === startChildNode && startContainer !== node ? startContainer : childNode,
startOffset: childNode === startChildNode && startContainer !== node ? startOffset : 0,
endContainer: childNode.nextSibling === endChildNode && endContainer !== node ? endContainer : childNode,
endOffset: childNode.nextSibling === endChildNode && endContainer !== node ? endOffset : getNodeLength(childNode),
commonAncestorContainer: childNode
};
var y = serialize(childNodeRange, isLineStart);
isLineStart = y === "" && isLineStart || y.slice(-1) === "\n";
result += y;
childNode = childNode.nextSibling;
}
}
if (display === "table-cell") {
result += "\t";
}
if (isBlock(display) && !isLineStart) {
result = result.replace(/[\t\n\f\r\u0020]$/g, "");
result += "\n";
isLineStart = true;
}
return result;
}
return "";
};
var serializeAsPlainText = function (range) {
var isLineStart = range.startContainer.nodeType !== Node.TEXT_NODE || /^[\t\n\f\r\u0020]*$/.test(range.startContainer.data.slice(0, range.startOffset));
var isLineEnd = range.endContainer.nodeType !== Node.TEXT_NODE || /^[\t\n\f\r\u0020]*$/.test(range.endContainer.data.slice(range.endOffset));
var staticRange = {
startContainer: range.startContainer,
startOffset: range.startOffset,
endContainer: range.endContainer,
endOffset: range.endOffset,
commonAncestorContainer: range.commonAncestorContainer
};
var value = serialize(staticRange, false);
if (isLineStart) {
value = value.replace(/^[\t\n\f\r\u0020]/g, "");
}
if (isLineEnd) {
value = value.replace(/[\t\n\f\r\u0020]$/g, "");
}
return value;
};
var serializeAsHTML = function (range) {
var fragment = range.cloneContents();
if (range.commonAncestorContainer.nodeType === Node.ELEMENT_NODE && range.commonAncestorContainer.namespaceURI === "http://www.w3.org/1998/Math/MathML") {//?
var math = document.createElementNS("http://www.w3.org/1998/Math/MathML", "math");
math.appendChild(fragment);
fragment = math;
}
return new XMLSerializer().serializeToString(fragment); // to have the xmlns for <math> elements
};
var onCopyOrDragStart = function (event) {
var dataTransfer = event.type === "copy" ? event.clipboardData : event.dataTransfer;
var tagName = event.target.nodeType === Node.ELEMENT_NODE ? event.target.tagName.toLowerCase() : "";
if (tagName !== "input" && tagName !== "textarea" && (tagName !== "a" || event.type === "copy") && tagName !== "img") {
//! dataTransfer.effectAllowed throws an exception in FireFox if tagName is INPUT or TEXTAREA
if ((event.type === "copy" || dataTransfer.effectAllowed === "uninitialized") && !event.defaultPrevented) {
var selection = window.getSelection();
var rangeCount = selection.rangeCount;
if (rangeCount !== 0 && !selection.isCollapsed) {
var i = -1;
var plainText = "";
var htmlText = "";
while (++i < rangeCount) {
//TODO: Firefox makes multiple selection when some <button> elements are selected ...
var range = selection.getRangeAt(i);
htmlText += serializeAsHTML(range);
plainText += serializeAsPlainText(range);
}
// see also https://github.com/w3c/clipboard-apis/issues/48
dataTransfer.setData("text/html", htmlText);
dataTransfer.setData("text/plain", plainText);
if (event.type === "copy") {
event.preventDefault();
} else {
dataTransfer.effectAllowed = "copy";
}
}
}
}
};
document.addEventListener("copy", onCopyOrDragStart, false);
document.addEventListener("dragstart", onCopyOrDragStart, false);