Skip to content

Commit

Permalink
ignore comment and white-space only nodes when check boundary points
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaffle committed May 19, 2020
1 parent 1f4566c commit 930459b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 66 deletions.
48 changes: 16 additions & 32 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function toAsciiMath(mathmlNode) {
module.exports = toAsciiMath;

},{"mathml-to-asciimath/lib/handlers/math":3,"mathml-to-asciimath/lib/handlers/mfrac":4,"mathml-to-asciimath/lib/handlers/mi":5,"mathml-to-asciimath/lib/handlers/mn":6,"mathml-to-asciimath/lib/handlers/mo":7,"mathml-to-asciimath/lib/handlers/mover":8,"mathml-to-asciimath/lib/handlers/mrow":9,"mathml-to-asciimath/lib/handlers/msqrt":10,"mathml-to-asciimath/lib/handlers/mstyle":11,"mathml-to-asciimath/lib/handlers/msub":12,"mathml-to-asciimath/lib/handlers/msup":13,"mathml-to-asciimath/lib/handlers/mtext":14}],2:[function(require,module,exports){
/*global window, document, console, Node, XMLSerializer */
/*global window, document, Node, XMLSerializer */
"use strict";

var transformMathMLToAsciiMath = require("mathml-to-asciimath");
Expand Down Expand Up @@ -93,15 +93,18 @@ var getNodeLength = function (container) {
};

var isBoundaryPoint = function (container, offset, which, node) {
var x = container;
if (which === "end" && offset !== getNodeLength(container) || which === "start" && offset !== 0) {
return false;
}
while (x !== node) {
if (which === "end" && x.nextSibling != null || which === "start" && x.previousSibling != null) {
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;
}
x = x.parentNode;
}
return true;
};
Expand Down Expand Up @@ -142,10 +145,9 @@ var serialize = function (range, isLineStart) {
throw new TypeError();
}
var data = node.data.slice(startOffset, endOffset);
//var data = node.data.slice(node === startContainer ? startOffset : 0, node === endContainer ? endOffset : node.data.length);
data = data.replace(/[\u0020\n\r\t\v]+/g, " ");
data = data.replace(/[\t\n\f\r\u0020]+/g, " ");
if (isLineStart) {
data = data.replace(/^[\u0020\n\r\t\v]/g, "");
data = data.replace(/^[\t\n\f\r\u0020]/g, "");
}
return data;
}
Expand All @@ -163,7 +165,7 @@ var serialize = function (range, isLineStart) {
if (isBoundaryPoint(startContainer, startOffset, "start", node) &&
isBoundaryPoint(endContainer, endOffset, "end", node)) {
var tagName = node.tagName.toLowerCase();
if (tagName === "math" || (tagName !== "mtext" && isMathMLTagName(tagName))) {
if (tagName === "math" || (tagName !== "mtext" && node.namespaceURI === "http://www.w3.org/1998/Math/MathML")) {
x = transformMathMLToAsciiMath(node);
}
if (tagName === "br") {
Expand Down Expand Up @@ -194,7 +196,7 @@ var serialize = function (range, isLineStart) {
result += "\t";
}
if (isBlock(display) && !isLineStart) {
result = result.replace(/[\u0020\n\r\t\v]$/g, "");
result = result.replace(/[\t\n\f\r\u0020]$/g, "");
result += "\n";
isLineStart = true;
}
Expand All @@ -204,8 +206,8 @@ var serialize = function (range, isLineStart) {
};

var serializeAsPlainText = function (range) {
var isLineStart = !(range.startContainer.nodeType === Node.TEXT_NODE && range.startContainer.data.slice(0, range.startOffset).replace(/\s+/g, "") !== "");
var isLineEnd = !(range.endContainer.nodeType === Node.TEXT_NODE && range.endContainer.data.slice(range.endOffset, range.endContainer.data.length).replace(/\s+/g, "") !== "");
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,
Expand All @@ -215,32 +217,14 @@ var serializeAsPlainText = function (range) {
};
var value = serialize(staticRange, false);
if (isLineStart) {
value = value.replace(/^\s/g, "");
value = value.replace(/^[\t\n\f\r\u0020]/g, "");
}
if (isLineEnd) {
value = value.replace(/\s$/g, "");
value = value.replace(/[\t\n\f\r\u0020]$/g, "");
}
return value;
};

var isMathMLTagName = function (tagName) {
if (tagName.charCodeAt(0) === "m".charCodeAt(0)) {
switch (tagName) {
case "main":
case "map":
case "mark":
case "math":
case "menu":
case "menuitem":
case "meta":
case "meter":
return false;
}
return true;
}
return false;
};

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") {//?
Expand Down
35 changes: 34 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,40 @@
Try to select the next MathML block or it's part with <mark>text selection</mark>, then copy (<kbd>Ctrl+C</kbd>) and paste into a text editor:
</p>

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mi>x</mi><mo>=</mo><mrow><mfrac><mrow><mo>&#x2212;</mo><mi>b</mi><mo>&#x00B1;</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>&#x2212;</mo><mn>4</mn><mo>&#x2062;</mo><mi>a</mi><mo>&#x2062;</mo><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mo>&#x2062;</mo><mi>a</mi></mrow></mfrac></mrow></math>
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
<mi>x</mi>
<mo>=</mo>
<mfrac>
<mrow>
<mrow>
<mo>&minus;</mo>
<mi>b</mi>
</mrow>
<mo>&plusmn;</mo>
<msqrt>
<mrow>
<msup>
<mi>b</mi>
<mn>2</mn>
</msup>
<mo>&minus;</mo>
<mrow>
<mn>4</mn>
<mo>&it;</mo>
<mi>a</mi>
<mo>&it;</mo>
<mi>c</mi>
</mrow>
</mrow>
</msqrt>
</mrow>
<mrow>
<mn>2</mn>
<mo>&it;</mo>
<mi>a</mi>
</mrow>
</mfrac>
</math>

<p>
You should see something like <code>x=(-b±sqrt(b^2-4ac))/(2a)</code>, not <code>x = − b ± b 2 − 4 a c 2 a</code>.
Expand Down
48 changes: 16 additions & 32 deletions mathml-to-clipboard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global window, document, console, Node, XMLSerializer */
/*global window, document, Node, XMLSerializer */
"use strict";

var transformMathMLToAsciiMath = require("mathml-to-asciimath");
Expand Down Expand Up @@ -36,15 +36,18 @@ var getNodeLength = function (container) {
};

var isBoundaryPoint = function (container, offset, which, node) {
var x = container;
if (which === "end" && offset !== getNodeLength(container) || which === "start" && offset !== 0) {
return false;
}
while (x !== node) {
if (which === "end" && x.nextSibling != null || which === "start" && x.previousSibling != null) {
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;
}
x = x.parentNode;
}
return true;
};
Expand Down Expand Up @@ -85,10 +88,9 @@ var serialize = function (range, isLineStart) {
throw new TypeError();
}
var data = node.data.slice(startOffset, endOffset);
//var data = node.data.slice(node === startContainer ? startOffset : 0, node === endContainer ? endOffset : node.data.length);
data = data.replace(/[\u0020\n\r\t\v]+/g, " ");
data = data.replace(/[\t\n\f\r\u0020]+/g, " ");
if (isLineStart) {
data = data.replace(/^[\u0020\n\r\t\v]/g, "");
data = data.replace(/^[\t\n\f\r\u0020]/g, "");
}
return data;
}
Expand All @@ -106,7 +108,7 @@ var serialize = function (range, isLineStart) {
if (isBoundaryPoint(startContainer, startOffset, "start", node) &&
isBoundaryPoint(endContainer, endOffset, "end", node)) {
var tagName = node.tagName.toLowerCase();
if (tagName === "math" || (tagName !== "mtext" && isMathMLTagName(tagName))) {
if (tagName === "math" || (tagName !== "mtext" && node.namespaceURI === "http://www.w3.org/1998/Math/MathML")) {
x = transformMathMLToAsciiMath(node);
}
if (tagName === "br") {
Expand Down Expand Up @@ -137,7 +139,7 @@ var serialize = function (range, isLineStart) {
result += "\t";
}
if (isBlock(display) && !isLineStart) {
result = result.replace(/[\u0020\n\r\t\v]$/g, "");
result = result.replace(/[\t\n\f\r\u0020]$/g, "");
result += "\n";
isLineStart = true;
}
Expand All @@ -147,8 +149,8 @@ var serialize = function (range, isLineStart) {
};

var serializeAsPlainText = function (range) {
var isLineStart = !(range.startContainer.nodeType === Node.TEXT_NODE && range.startContainer.data.slice(0, range.startOffset).replace(/\s+/g, "") !== "");
var isLineEnd = !(range.endContainer.nodeType === Node.TEXT_NODE && range.endContainer.data.slice(range.endOffset, range.endContainer.data.length).replace(/\s+/g, "") !== "");
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,
Expand All @@ -158,32 +160,14 @@ var serializeAsPlainText = function (range) {
};
var value = serialize(staticRange, false);
if (isLineStart) {
value = value.replace(/^\s/g, "");
value = value.replace(/^[\t\n\f\r\u0020]/g, "");
}
if (isLineEnd) {
value = value.replace(/\s$/g, "");
value = value.replace(/[\t\n\f\r\u0020]$/g, "");
}
return value;
};

var isMathMLTagName = function (tagName) {
if (tagName.charCodeAt(0) === "m".charCodeAt(0)) {
switch (tagName) {
case "main":
case "map":
case "mark":
case "math":
case "menu":
case "menuitem":
case "meta":
case "meter":
return false;
}
return true;
}
return false;
};

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") {//?
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"name": "mathml-to-clipboard",
"description": "A copy event handler for a web application to transform MathML into AsciiMath and put into the clipboard.",
"homepage": "https://github.com/Yaffle/mathml-to-clipboard#readme",
"version": "1.0.2",
"version": "1.0.3",
"license": "ISC",
"main": "dist/main.js",
"devDependencies": {
Expand Down

0 comments on commit 930459b

Please sign in to comment.