katex 0.15.4 → 0.16.0

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.
package/README.md CHANGED
@@ -31,13 +31,13 @@ Try out KaTeX [on the demo page](https://katex.org/#demo)!
31
31
  <!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
32
32
  <html>
33
33
  <head>
34
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.css" integrity="sha384-YC8qdB2s1ab8mZ+cia7a0adIJypa9Tkwmt+FCx4o0sFZ1x353rJ1FNT2j+sJHHbw" crossorigin="anonymous">
34
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
35
35
 
36
36
  <!-- The loading of KaTeX is deferred to speed up page rendering -->
37
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.js" integrity="sha384-KZADTF3hNEblA406FgHRI7wD4vngipdDF5/v5nLzNf2v1StaZ2xBisKoJunz85In" crossorigin="anonymous"></script>
37
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
38
38
 
39
39
  <!-- To automatically render math in text elements, include the auto-render extension: -->
40
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
40
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
41
41
  onload="renderMathInElement(document.body);"></script>
42
42
  </head>
43
43
  ...
@@ -2,29 +2,27 @@
2
2
 
3
3
  This extension modifies the copy/paste behavior in any browser supporting the
4
4
  [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardEvent)
5
- so that, when selecting and copying whole KaTeX-rendered elements, the text
5
+ so that, when selecting and copying KaTeX-rendered elements, the text
6
6
  content of the resulting clipboard renders KaTeX elements as their LaTeX source
7
7
  surrounded by specified delimiters. (The HTML content of the resulting
8
8
  clipboard remains the selected HTML content, as it normally would.)
9
9
  The default delimiters are `$...$` for inline math and `$$...$$` for display
10
10
  math, but you can easy switch them to e.g. `\(...\)` and `\[...\]` by
11
11
  modifying `copyDelimiters` in [the source code](copy-tex.js).
12
+ Note that a selection containing part of a KaTeX formula gets extended to
13
+ include the entire KaTeX formula.
12
14
 
13
- ### Usage
15
+ ## Usage
14
16
 
15
17
  This extension isn't part of KaTeX proper, so the script should be separately
16
- included in the page. It also provides *optional* custom CSS that
17
- defines KaTeX equations as
18
- [`user-select: all`](https://developer.mozilla.org/en-US/docs/Web/CSS/user-select)
19
- so that they get selected all-or-nothing (and thus trigger the good behavior
20
- provided by this extension). Without this CSS, partially selected equations
21
- will just get the usual HTML copy/paste behavior.
18
+ included in the page.
22
19
 
23
20
  ```html
24
- <link href="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/copy-tex.css" rel="stylesheet" type="text/css">
25
- <script src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/copy-tex.min.js" integrity="sha384-Ep9Es0VCjVn9dFeaN2uQxgGcGmG+pfZ4eBaHxUpxXDORrrVACZVOpywyzvFRGbmv" crossorigin="anonymous"></script>
21
+ <script src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/copy-tex.min.js" integrity="sha384-ww/583aHhxWkz5DEVn6OKtNiIaLi2iBRNZXfJRiY1Ai7tnJ9UXpEsyvOITVpTl4A" crossorigin="anonymous"></script>
26
22
  ```
27
23
 
24
+ (Note that, as of KaTeX 0.16.0, there is no longer a corresponding CSS file.)
25
+
28
26
  See [index.html](index.html) for an example.
29
27
  (To run this example from a clone of the repository, run `yarn start`
30
28
  in the root KaTeX directory, and then visit
@@ -37,13 +35,5 @@ statement with `require('katex/contrib/copy-tex/katex2tex.js')`.
37
35
 
38
36
  ECMAScript module is also available:
39
37
  ```html
40
- <script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/copy-tex.mjs" integrity="sha384-+gSYJ3yzY30+a6FGYJXOx9swmWs5oPKEi1AeCsAxsLexABlUXgHXkOkEZCj0Lz8U" crossorigin="anonymous"></script>
38
+ <script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/copy-tex.mjs" integrity="sha384-bVEnwt0PtX+1EuJoOEcm4rgTUWvb2ILTdjHfI1gUe/r5fdqrTcQaUuRdHG2DciuQ" crossorigin="anonymous"></script>
41
39
  ```
42
-
43
- ### Known Issues
44
-
45
- This extension has been tested on Chrome, Firefox, Edge, and Safari.
46
-
47
- Safari copies correctly, but the selection rectangle renders strangely
48
- (too big) when interacting with display math
49
- (because of the `user-select: all` CSS).
@@ -1,23 +1,50 @@
1
+ // @flow
2
+
1
3
  import katexReplaceWithTex from './katex2tex';
2
4
 
3
- // Global copy handler to modify behavior on .katex elements.
4
- document.addEventListener('copy', function(event) {
5
+ // Return <div class="katex"> element containing node, or null if not found.
6
+ function closestKatex(node: Node): ?Element {
7
+ // If node is a Text Node, for example, go up to containing Element,
8
+ // where we can apply the `closest` method.
9
+ const element: ?Element =
10
+ (node instanceof Element ? node : node.parentElement);
11
+ return element && element.closest('.katex');
12
+ }
13
+
14
+ // Global copy handler to modify behavior on/within .katex elements.
15
+ document.addEventListener('copy', function(event: ClipboardEvent) {
5
16
  const selection = window.getSelection();
6
- if (selection.isCollapsed) {
7
- return; // default action OK if selection is empty
17
+ if (selection.isCollapsed || !event.clipboardData) {
18
+ return; // default action OK if selection is empty or unchangeable
19
+ }
20
+ const clipboardData = event.clipboardData;
21
+ const range = selection.getRangeAt(0);
22
+
23
+ // When start point is within a formula, expand to entire formula.
24
+ const startKatex = closestKatex(range.startContainer);
25
+ if (startKatex) {
26
+ range.setStartBefore(startKatex);
27
+ }
28
+
29
+ // Similarly, when end point is within a formula, expand to entire formula.
30
+ const endKatex = closestKatex(range.endContainer);
31
+ if (endKatex) {
32
+ range.setEndAfter(endKatex);
8
33
  }
9
- const fragment = selection.getRangeAt(0).cloneContents();
34
+
35
+ const fragment = range.cloneContents();
10
36
  if (!fragment.querySelector('.katex-mathml')) {
11
- return; // default action OK if no .katex-mathml elements
37
+ return; // default action OK if no .katex-mathml elements
12
38
  }
39
+
40
+ const htmlContents = Array.prototype.map.call(fragment.childNodes,
41
+ (el) => (el instanceof Text ? el.textContent : el.outerHTML)
42
+ ).join('');
43
+
13
44
  // Preserve usual HTML copy/paste behavior.
14
- const html = [];
15
- for (let i = 0; i < fragment.childNodes.length; i++) {
16
- html.push(fragment.childNodes[i].outerHTML);
17
- }
18
- event.clipboardData.setData('text/html', html.join(''));
45
+ clipboardData.setData('text/html', htmlContents);
19
46
  // Rewrite plain-text version.
20
- event.clipboardData.setData('text/plain',
47
+ clipboardData.setData('text/plain',
21
48
  katexReplaceWithTex(fragment).textContent);
22
49
  // Prevent normal copy handling.
23
50
  event.preventDefault();
@@ -1,5 +1,12 @@
1
+ // @flow
2
+
3
+ export interface CopyDelimiters {
4
+ inline: [string, string],
5
+ display: [string, string],
6
+ }
7
+
1
8
  // Set these to how you want inline and display math to be delimited.
2
- export const defaultCopyDelimiters = {
9
+ export const defaultCopyDelimiters: CopyDelimiters = {
3
10
  inline: ['$', '$'], // alternative: ['\(', '\)']
4
11
  display: ['$$', '$$'], // alternative: ['\[', '\]']
5
12
  };
@@ -7,16 +14,18 @@ export const defaultCopyDelimiters = {
7
14
  // Replace .katex elements with their TeX source (<annotation> element).
8
15
  // Modifies fragment in-place. Useful for writing your own 'copy' handler,
9
16
  // as in copy-tex.js.
10
- export const katexReplaceWithTex = function(fragment,
11
- copyDelimiters = defaultCopyDelimiters) {
17
+ export function katexReplaceWithTex(
18
+ fragment: DocumentFragment,
19
+ copyDelimiters: CopyDelimiters = defaultCopyDelimiters
20
+ ): DocumentFragment {
12
21
  // Remove .katex-html blocks that are preceded by .katex-mathml blocks
13
22
  // (which will get replaced below).
14
23
  const katexHtml = fragment.querySelectorAll('.katex-mathml + .katex-html');
15
24
  for (let i = 0; i < katexHtml.length; i++) {
16
25
  const element = katexHtml[i];
17
26
  if (element.remove) {
18
- element.remove(null);
19
- } else {
27
+ element.remove();
28
+ } else if (element.parentNode) {
20
29
  element.parentNode.removeChild(element);
21
30
  }
22
31
  }
@@ -29,7 +38,7 @@ export const katexReplaceWithTex = function(fragment,
29
38
  if (texSource) {
30
39
  if (element.replaceWith) {
31
40
  element.replaceWith(texSource);
32
- } else {
41
+ } else if (element.parentNode) {
33
42
  element.parentNode.replaceChild(texSource, element);
34
43
  }
35
44
  texSource.innerHTML = copyDelimiters.inline[0] +
@@ -47,6 +56,6 @@ export const katexReplaceWithTex = function(fragment,
47
56
  + copyDelimiters.display[1];
48
57
  }
49
58
  return fragment;
50
- };
59
+ }
51
60
 
52
61
  export default katexReplaceWithTex;
@@ -11,7 +11,7 @@ included in the page, in addition to KaTeX.
11
11
  Load the extension by adding the following line to your HTML file.
12
12
 
13
13
  ```html
14
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
14
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
15
15
  ```
16
16
  You can download the script and use it locally, or from a local KaTeX installation instead.
17
17
 
@@ -23,9 +23,9 @@ Then, in the body, we use a `math/tex` script to typeset the equation `x+\sqrt{1
23
23
  <!DOCTYPE html>
24
24
  <html>
25
25
  <head>
26
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.css" integrity="sha384-YC8qdB2s1ab8mZ+cia7a0adIJypa9Tkwmt+FCx4o0sFZ1x353rJ1FNT2j+sJHHbw" crossorigin="anonymous">
27
- <script src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.js" integrity="sha384-KZADTF3hNEblA406FgHRI7wD4vngipdDF5/v5nLzNf2v1StaZ2xBisKoJunz85In" crossorigin="anonymous"></script>
28
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
26
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
27
+ <script src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
28
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-jiBVvJ8NGGj5n7kJaiWwWp9AjC+Yh8rhZY3GtAX8yU28azcLgoRo4oukO87g7zDT" crossorigin="anonymous"></script>
29
29
  </head>
30
30
  <body>
31
31
  <script type="math/tex">x+\sqrt{1-x^2}</script>
@@ -35,4 +35,4 @@ Then, in the body, we use a `math/tex` script to typeset the equation `x+\sqrt{1
35
35
 
36
36
  ECMAScript module is also available:
37
37
  ```html
38
- <script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/mathtex-script-type.mjs" integrity="sha384-4EJvC5tvqq9XJxXvdD4JutBokuFw/dCe2AB4gZ9sRpwFFXECpL3qT43tmE0PkpVg" crossorigin="anonymous"></script>
38
+ <script type="module" src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mathtex-script-type.mjs" integrity="sha384-4EJvC5tvqq9XJxXvdD4JutBokuFw/dCe2AB4gZ9sRpwFFXECpL3qT43tmE0PkpVg" crossorigin="anonymous"></script>
@@ -7,7 +7,7 @@ This extension adds to KaTeX the `\ce` and `\pu` functions from the [mhchem](htt
7
7
  This extension isn't part of core KaTeX, so the script should be separately included. Write the following line into the HTML page's `<head>`. Place it *after* the line that calls `katex.js`, and if you make use of the [auto-render](https://katex.org/docs/autorender.html) extension, place it *before* the line that calls `auto-render.js`.
8
8
 
9
9
  ```html
10
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/mhchem.min.js" integrity="sha384-UEY9IRPkV+TTTY7nK1wSrfhWPDJy9wr4PmYg3DLPcN5F4NDlIwGZkWtWveKR/45c" crossorigin="anonymous"></script>
10
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/mhchem.min.js" integrity="sha384-RTN08a0AXIioPBcVosEqPUfKK+rPp+h1x/izR7xMkdMyuwkcZCWdxO+RSwIFtJXN" crossorigin="anonymous"></script>
11
11
  ```
12
12
 
13
13
  If you remove the `defer` attribute from this tag, then you must also remove the `defer` attribute from the `<script src="https://../katex.min.js">` tag.
@@ -77,7 +77,7 @@ import katex from "katex";
77
77
  var chemParse = function (tokens, stateMachine) {
78
78
  // Recreate the argument string from KaTeX's array of tokens.
79
79
  var str = "";
80
- var expectedLoc = tokens[tokens.length - 1].loc.start
80
+ var expectedLoc = tokens.length && tokens[tokens.length - 1].loc.start
81
81
  for (var i = tokens.length - 1; i >= 0; i--) {
82
82
  if(tokens[i].loc.start > expectedLoc) {
83
83
  // context.consumeArgs has eaten a space.
package/dist/README.md CHANGED
@@ -31,13 +31,13 @@ Try out KaTeX [on the demo page](https://katex.org/#demo)!
31
31
  <!-- KaTeX requires the use of the HTML5 doctype. Without it, KaTeX may not render properly -->
32
32
  <html>
33
33
  <head>
34
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.css" integrity="sha384-YC8qdB2s1ab8mZ+cia7a0adIJypa9Tkwmt+FCx4o0sFZ1x353rJ1FNT2j+sJHHbw" crossorigin="anonymous">
34
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" integrity="sha384-Xi8rHCmBmhbuyyhbI88391ZKP2dmfnOl4rT9ZfRI7mLTdk1wblIUnrIq35nqwEvC" crossorigin="anonymous">
35
35
 
36
36
  <!-- The loading of KaTeX is deferred to speed up page rendering -->
37
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/katex.min.js" integrity="sha384-KZADTF3hNEblA406FgHRI7wD4vngipdDF5/v5nLzNf2v1StaZ2xBisKoJunz85In" crossorigin="anonymous"></script>
37
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.js" integrity="sha384-X/XCfMm41VSsqRNQgDerQczD69XqmjOOOwYQvr/uuC+j4OPoNhVgjdGFwhvN02Ja" crossorigin="anonymous"></script>
38
38
 
39
39
  <!-- To automatically render math in text elements, include the auto-render extension: -->
40
- <script defer src="https://cdn.jsdelivr.net/npm/katex@0.15.4/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
40
+ <script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/contrib/auto-render.min.js" integrity="sha384-+XBljXPPiv+OzfbB3cVmLHf4hdUFHlWNZN5spNQ7rmHTXpd7WvJum6fIACpNNfIR" crossorigin="anonymous"
41
41
  onload="renderMathInElement(document.body);"></script>
42
42
  </head>
43
43
  ...
@@ -23,7 +23,7 @@ var defaultCopyDelimiters = {
23
23
  // Modifies fragment in-place. Useful for writing your own 'copy' handler,
24
24
  // as in copy-tex.js.
25
25
 
26
- var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters) {
26
+ function katexReplaceWithTex(fragment, copyDelimiters) {
27
27
  if (copyDelimiters === void 0) {
28
28
  copyDelimiters = defaultCopyDelimiters;
29
29
  }
@@ -36,8 +36,8 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
36
36
  var element = katexHtml[i];
37
37
 
38
38
  if (element.remove) {
39
- element.remove(null);
40
- } else {
39
+ element.remove();
40
+ } else if (element.parentNode) {
41
41
  element.parentNode.removeChild(element);
42
42
  }
43
43
  } // Replace .katex-mathml elements with their annotation (TeX source)
@@ -54,7 +54,7 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
54
54
  if (texSource) {
55
55
  if (_element.replaceWith) {
56
56
  _element.replaceWith(texSource);
57
- } else {
57
+ } else if (_element.parentNode) {
58
58
  _element.parentNode.replaceChild(texSource, _element);
59
59
  }
60
60
 
@@ -71,44 +71,58 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
71
71
  }
72
72
 
73
73
  return fragment;
74
- };
74
+ }
75
75
  /* harmony default export */ var katex2tex = (katexReplaceWithTex);
76
76
  ;// CONCATENATED MODULE: ./contrib/copy-tex/copy-tex.js
77
- // Global copy handler to modify behavior on .katex elements.
77
+ // Return <div class="katex"> element containing node, or null if not found.
78
+
79
+ function closestKatex(node) {
80
+ // If node is a Text Node, for example, go up to containing Element,
81
+ // where we can apply the `closest` method.
82
+ var element = node instanceof Element ? node : node.parentElement;
83
+ return element && element.closest('.katex');
84
+ } // Global copy handler to modify behavior on/within .katex elements.
85
+
78
86
 
79
87
  document.addEventListener('copy', function (event) {
80
88
  var selection = window.getSelection();
81
89
 
82
- if (selection.isCollapsed) {
83
- return; // default action OK if selection is empty
90
+ if (selection.isCollapsed || !event.clipboardData) {
91
+ return; // default action OK if selection is empty or unchangeable
84
92
  }
85
93
 
86
- var fragment = selection.getRangeAt(0).cloneContents();
94
+ var clipboardData = event.clipboardData;
95
+ var range = selection.getRangeAt(0); // When start point is within a formula, expand to entire formula.
87
96
 
88
- if (!fragment.querySelector('.katex-mathml')) {
89
- return; // default action OK if no .katex-mathml elements
90
- } // Preserve usual HTML copy/paste behavior.
97
+ var startKatex = closestKatex(range.startContainer);
91
98
 
99
+ if (startKatex) {
100
+ range.setStartBefore(startKatex);
101
+ } // Similarly, when end point is within a formula, expand to entire formula.
92
102
 
93
- var html = [];
94
103
 
95
- for (var i = 0; i < fragment.childNodes.length; i++) {
96
- html.push(fragment.childNodes[i].outerHTML);
104
+ var endKatex = closestKatex(range.endContainer);
105
+
106
+ if (endKatex) {
107
+ range.setEndAfter(endKatex);
97
108
  }
98
109
 
99
- event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version.
110
+ var fragment = range.cloneContents();
100
111
 
101
- event.clipboardData.setData('text/plain', katex2tex(fragment).textContent); // Prevent normal copy handling.
112
+ if (!fragment.querySelector('.katex-mathml')) {
113
+ return; // default action OK if no .katex-mathml elements
114
+ }
102
115
 
103
- event.preventDefault();
104
- });
105
- ;// CONCATENATED MODULE: ./contrib/copy-tex/copy-tex.webpack.js
106
- /**
107
- * This is the webpack entry point for KaTeX. As ECMAScript doesn't support
108
- * CSS modules natively, a separate entry point is used.
109
- */
116
+ var htmlContents = Array.prototype.map.call(fragment.childNodes, function (el) {
117
+ return el instanceof Text ? el.textContent : el.outerHTML;
118
+ }).join(''); // Preserve usual HTML copy/paste behavior.
110
119
 
120
+ clipboardData.setData('text/html', htmlContents); // Rewrite plain-text version.
111
121
 
122
+ clipboardData.setData('text/plain', katex2tex(fragment).textContent); // Prevent normal copy handling.
123
+
124
+ event.preventDefault();
125
+ });
112
126
  __webpack_exports__ = __webpack_exports__["default"];
113
127
  /******/ return __webpack_exports__;
114
128
  /******/ })()
@@ -1 +1 @@
1
- !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var l in n)("object"==typeof exports?exports:e)[l]=n[l]}}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={},t={inline:["$","$"],display:["$$","$$"]},n=function(e,n){void 0===n&&(n=t);for(var l=e.querySelectorAll(".katex-mathml + .katex-html"),r=0;r<l.length;r++){var i=l[r];i.remove?i.remove(null):i.parentNode.removeChild(i)}for(var o=e.querySelectorAll(".katex-mathml"),a=0;a<o.length;a++){var d=o[a],f=d.querySelector("annotation");f&&(d.replaceWith?d.replaceWith(f):d.parentNode.replaceChild(f,d),f.innerHTML=n.inline[0]+f.innerHTML+n.inline[1])}for(var c=e.querySelectorAll(".katex-display annotation"),s=0;s<c.length;s++){var p=c[s];p.innerHTML=n.display[0]+p.innerHTML.substr(n.inline[0].length,p.innerHTML.length-n.inline[0].length-n.inline[1].length)+n.display[1]}return e};return document.addEventListener("copy",(function(e){var t=window.getSelection();if(!t.isCollapsed){var l=t.getRangeAt(0).cloneContents();if(l.querySelector(".katex-mathml")){for(var r=[],i=0;i<l.childNodes.length;i++)r.push(l.childNodes[i].outerHTML);e.clipboardData.setData("text/html",r.join("")),e.clipboardData.setData("text/plain",n(l).textContent),e.preventDefault()}}})),e=e.default}()}));
1
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}("undefined"!=typeof self?self:this,(function(){return function(){"use strict";var e={},t={inline:["$","$"],display:["$$","$$"]};var n=function(e,n){void 0===n&&(n=t);for(var r=e.querySelectorAll(".katex-mathml + .katex-html"),a=0;a<r.length;a++){var o=r[a];o.remove?o.remove():o.parentNode&&o.parentNode.removeChild(o)}for(var i=e.querySelectorAll(".katex-mathml"),l=0;l<i.length;l++){var f=i[l],c=f.querySelector("annotation");c&&(f.replaceWith?f.replaceWith(c):f.parentNode&&f.parentNode.replaceChild(c,f),c.innerHTML=n.inline[0]+c.innerHTML+n.inline[1])}for(var d=e.querySelectorAll(".katex-display annotation"),s=0;s<d.length;s++){var p=d[s];p.innerHTML=n.display[0]+p.innerHTML.substr(n.inline[0].length,p.innerHTML.length-n.inline[0].length-n.inline[1].length)+n.display[1]}return e};function r(e){var t=e instanceof Element?e:e.parentElement;return t&&t.closest(".katex")}return document.addEventListener("copy",(function(e){var t=window.getSelection();if(!t.isCollapsed&&e.clipboardData){var a=e.clipboardData,o=t.getRangeAt(0),i=r(o.startContainer);i&&o.setStartBefore(i);var l=r(o.endContainer);l&&o.setEndAfter(l);var f=o.cloneContents();if(f.querySelector(".katex-mathml")){var c=Array.prototype.map.call(f.childNodes,(function(e){return e instanceof Text?e.textContent:e.outerHTML})).join("");a.setData("text/html",c),a.setData("text/plain",n(f).textContent),e.preventDefault()}}})),e=e.default}()}));
@@ -8,7 +8,7 @@ var defaultCopyDelimiters = {
8
8
  // Modifies fragment in-place. Useful for writing your own 'copy' handler,
9
9
  // as in copy-tex.js.
10
10
 
11
- var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters) {
11
+ function katexReplaceWithTex(fragment, copyDelimiters) {
12
12
  if (copyDelimiters === void 0) {
13
13
  copyDelimiters = defaultCopyDelimiters;
14
14
  }
@@ -21,8 +21,8 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
21
21
  var element = katexHtml[i];
22
22
 
23
23
  if (element.remove) {
24
- element.remove(null);
25
- } else {
24
+ element.remove();
25
+ } else if (element.parentNode) {
26
26
  element.parentNode.removeChild(element);
27
27
  }
28
28
  } // Replace .katex-mathml elements with their annotation (TeX source)
@@ -39,7 +39,7 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
39
39
  if (texSource) {
40
40
  if (_element.replaceWith) {
41
41
  _element.replaceWith(texSource);
42
- } else {
42
+ } else if (_element.parentNode) {
43
43
  _element.parentNode.replaceChild(texSource, _element);
44
44
  }
45
45
 
@@ -56,31 +56,50 @@ var katexReplaceWithTex = function katexReplaceWithTex(fragment, copyDelimiters)
56
56
  }
57
57
 
58
58
  return fragment;
59
- };
59
+ }
60
+
61
+ function closestKatex(node) {
62
+ // If node is a Text Node, for example, go up to containing Element,
63
+ // where we can apply the `closest` method.
64
+ var element = node instanceof Element ? node : node.parentElement;
65
+ return element && element.closest('.katex');
66
+ } // Global copy handler to modify behavior on/within .katex elements.
67
+
60
68
 
61
69
  document.addEventListener('copy', function (event) {
62
70
  var selection = window.getSelection();
63
71
 
64
- if (selection.isCollapsed) {
65
- return; // default action OK if selection is empty
72
+ if (selection.isCollapsed || !event.clipboardData) {
73
+ return; // default action OK if selection is empty or unchangeable
66
74
  }
67
75
 
68
- var fragment = selection.getRangeAt(0).cloneContents();
76
+ var clipboardData = event.clipboardData;
77
+ var range = selection.getRangeAt(0); // When start point is within a formula, expand to entire formula.
69
78
 
70
- if (!fragment.querySelector('.katex-mathml')) {
71
- return; // default action OK if no .katex-mathml elements
72
- } // Preserve usual HTML copy/paste behavior.
79
+ var startKatex = closestKatex(range.startContainer);
80
+
81
+ if (startKatex) {
82
+ range.setStartBefore(startKatex);
83
+ } // Similarly, when end point is within a formula, expand to entire formula.
73
84
 
74
85
 
75
- var html = [];
86
+ var endKatex = closestKatex(range.endContainer);
76
87
 
77
- for (var i = 0; i < fragment.childNodes.length; i++) {
78
- html.push(fragment.childNodes[i].outerHTML);
88
+ if (endKatex) {
89
+ range.setEndAfter(endKatex);
79
90
  }
80
91
 
81
- event.clipboardData.setData('text/html', html.join('')); // Rewrite plain-text version.
92
+ var fragment = range.cloneContents();
93
+
94
+ if (!fragment.querySelector('.katex-mathml')) {
95
+ return; // default action OK if no .katex-mathml elements
96
+ }
97
+
98
+ var htmlContents = Array.prototype.map.call(fragment.childNodes, el => el instanceof Text ? el.textContent : el.outerHTML).join(''); // Preserve usual HTML copy/paste behavior.
99
+
100
+ clipboardData.setData('text/html', htmlContents); // Rewrite plain-text version.
82
101
 
83
- event.clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
102
+ clipboardData.setData('text/plain', katexReplaceWithTex(fragment).textContent); // Prevent normal copy handling.
84
103
 
85
104
  event.preventDefault();
86
105
  });
@@ -154,7 +154,7 @@ katex__WEBPACK_IMPORTED_MODULE_0___default().__defineMacro("\\tripledash", "{\\v
154
154
  var chemParse = function chemParse(tokens, stateMachine) {
155
155
  // Recreate the argument string from KaTeX's array of tokens.
156
156
  var str = "";
157
- var expectedLoc = tokens[tokens.length - 1].loc.start;
157
+ var expectedLoc = tokens.length && tokens[tokens.length - 1].loc.start;
158
158
 
159
159
  for (var i = tokens.length - 1; i >= 0; i--) {
160
160
  if (tokens[i].loc.start > expectedLoc) {