katex 0.16.34 → 0.16.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,87 +1,85 @@
1
1
  // Set these to how you want inline and display math to be delimited.
2
- const defaultCopyDelimiters = {
3
- inline: ['$', '$'], // alternative: ['\(', '\)']
4
- display: ['$$', '$$'], // alternative: ['\[', '\]']
2
+ var defaultCopyDelimiters = {
3
+ inline: ['$', '$'],
4
+ // alternative: ['\(', '\)']
5
+ display: ['$$', '$$'] // alternative: ['\[', '\]']
5
6
  };
6
7
  // Replace .katex elements with their TeX source (<annotation> element).
7
8
  // Modifies fragment in-place. Useful for writing your own 'copy' handler,
8
9
  // as in copy-tex.js.
9
- function katexReplaceWithTex(fragment, copyDelimiters = defaultCopyDelimiters) {
10
- // Remove .katex-html blocks that are preceded by .katex-mathml blocks
11
- // (which will get replaced below).
12
- const katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
13
- for (let i = 0; i < katexHtml.length; i++) {
14
- const element = katexHtml[i];
15
- if (element.remove) {
16
- element.remove();
17
- }
18
- else if (element.parentNode) {
19
- element.parentNode.removeChild(element);
20
- }
10
+ function katexReplaceWithTex(fragment, copyDelimiters) {
11
+ if (copyDelimiters === void 0) {
12
+ copyDelimiters = defaultCopyDelimiters;
13
+ }
14
+ // Remove .katex-html blocks that are preceded by .katex-mathml blocks
15
+ // (which will get replaced below).
16
+ var katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
17
+ for (var i = 0; i < katexHtml.length; i++) {
18
+ var element = katexHtml[i];
19
+ if (element.remove) {
20
+ element.remove();
21
+ } else if (element.parentNode) {
22
+ element.parentNode.removeChild(element);
21
23
  }
22
- // Replace .katex-mathml elements with their annotation (TeX source)
23
- // descendant, with inline delimiters.
24
- const katexMathml = fragment.querySelectorAll('.katex-mathml');
25
- for (let i = 0; i < katexMathml.length; i++) {
26
- const element = katexMathml[i];
27
- const texSource = element.querySelector('annotation');
28
- if (texSource) {
29
- if (element.replaceWith) {
30
- element.replaceWith(texSource);
31
- }
32
- else if (element.parentNode) {
33
- element.parentNode.replaceChild(texSource, element);
34
- }
35
- texSource.innerHTML = copyDelimiters.inline[0] +
36
- texSource.innerHTML + copyDelimiters.inline[1];
37
- }
24
+ }
25
+ // Replace .katex-mathml elements with their annotation (TeX source)
26
+ // descendant, with inline delimiters.
27
+ var katexMathml = fragment.querySelectorAll('.katex-mathml');
28
+ for (var _i = 0; _i < katexMathml.length; _i++) {
29
+ var _element = katexMathml[_i];
30
+ var texSource = _element.querySelector('annotation');
31
+ if (texSource) {
32
+ if (_element.replaceWith) {
33
+ _element.replaceWith(texSource);
34
+ } else if (_element.parentNode) {
35
+ _element.parentNode.replaceChild(texSource, _element);
36
+ }
37
+ texSource.innerHTML = copyDelimiters.inline[0] + texSource.innerHTML + copyDelimiters.inline[1];
38
38
  }
39
- // Switch display math to display delimiters.
40
- const displays = fragment.querySelectorAll('.katex-display annotation');
41
- for (let i = 0; i < displays.length; i++) {
42
- const element = displays[i];
43
- element.innerHTML = copyDelimiters.display[0] +
44
- element.innerHTML.substr(copyDelimiters.inline[0].length, element.innerHTML.length - copyDelimiters.inline[0].length
45
- - copyDelimiters.inline[1].length)
46
- + copyDelimiters.display[1];
47
- }
48
- return fragment;
39
+ }
40
+ // Switch display math to display delimiters.
41
+ var displays = fragment.querySelectorAll('.katex-display annotation');
42
+ for (var _i2 = 0; _i2 < displays.length; _i2++) {
43
+ var _element2 = displays[_i2];
44
+ _element2.innerHTML = copyDelimiters.display[0] + _element2.innerHTML.substr(copyDelimiters.inline[0].length, _element2.innerHTML.length - copyDelimiters.inline[0].length - copyDelimiters.inline[1].length) + copyDelimiters.display[1];
45
+ }
46
+ return fragment;
49
47
  }
50
48
 
51
49
  // Return <div class="katex"> element containing node, or null if not found.
52
50
  function closestKatex(node) {
53
- // If node is a Text Node, for example, go up to containing Element,
54
- // where we can apply the `closest` method.
55
- const element = (node instanceof Element ? node : node.parentElement);
56
- return element && element.closest('.katex');
51
+ // If node is a Text Node, for example, go up to containing Element,
52
+ // where we can apply the `closest` method.
53
+ var element = node instanceof Element ? node : node.parentElement;
54
+ return element && element.closest('.katex');
57
55
  }
58
56
  // Global copy handler to modify behavior on/within .katex elements.
59
57
  document.addEventListener('copy', function (event) {
60
- const selection = window.getSelection();
61
- if (!selection || selection.isCollapsed || !event.clipboardData) {
62
- return; // default action OK if selection is empty or unchangeable
63
- }
64
- const clipboardData = event.clipboardData;
65
- const range = selection.getRangeAt(0);
66
- // When start point is within a formula, expand to entire formula.
67
- const startKatex = closestKatex(range.startContainer);
68
- if (startKatex) {
69
- range.setStartBefore(startKatex);
70
- }
71
- // Similarly, when end point is within a formula, expand to entire formula.
72
- const endKatex = closestKatex(range.endContainer);
73
- if (endKatex) {
74
- range.setEndAfter(endKatex);
75
- }
76
- const fragment = range.cloneContents();
77
- if (!fragment.querySelector('.katex-mathml')) {
78
- return; // default action OK if no .katex-mathml elements
79
- }
80
- const htmlContents = Array.prototype.map.call(fragment.childNodes, (el) => (el instanceof Text ? el.textContent : el.outerHTML)).join('');
81
- // Preserve usual HTML copy/paste behavior.
82
- clipboardData.setData('text/html', htmlContents);
83
- // Rewrite plain-text version.
84
- clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent);
85
- // Prevent normal copy handling.
86
- event.preventDefault();
58
+ var selection = window.getSelection();
59
+ if (!selection || selection.isCollapsed || !event.clipboardData) {
60
+ return; // default action OK if selection is empty or unchangeable
61
+ }
62
+ var clipboardData = event.clipboardData;
63
+ var range = selection.getRangeAt(0);
64
+ // When start point is within a formula, expand to entire formula.
65
+ var startKatex = closestKatex(range.startContainer);
66
+ if (startKatex) {
67
+ range.setStartBefore(startKatex);
68
+ }
69
+ // Similarly, when end point is within a formula, expand to entire formula.
70
+ var endKatex = closestKatex(range.endContainer);
71
+ if (endKatex) {
72
+ range.setEndAfter(endKatex);
73
+ }
74
+ var fragment = range.cloneContents();
75
+ if (!fragment.querySelector('.katex-mathml')) {
76
+ return; // default action OK if no .katex-mathml elements
77
+ }
78
+ var htmlContents = Array.prototype.map.call(fragment.childNodes, el => el instanceof Text ? el.textContent : el.outerHTML).join('');
79
+ // Preserve usual HTML copy/paste behavior.
80
+ clipboardData.setData('text/html', htmlContents);
81
+ // Rewrite plain-text version.
82
+ clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent);
83
+ // Prevent normal copy handling.
84
+ event.preventDefault();
87
85
  });