mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-05 11:18:39 +00:00
refactor: update toSplitInto to accept delimiter object (#3444)
Update toSplitInto matcher to accept the same delimiter object type that's used in renderMathInElement Co-authored-by: ylemkimon <y@ylem.kim>
This commit is contained in:
@@ -6,15 +6,14 @@ import renderMathInElement from "../auto-render";
|
|||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
expect.extend({
|
expect.extend({
|
||||||
toSplitInto: function(actual, left, right, result) {
|
toSplitInto: function(actual, result, delimiters) {
|
||||||
const message = {
|
const message = {
|
||||||
pass: true,
|
pass: true,
|
||||||
message: () => "'" + actual + "' split correctly",
|
message: () => "'" + actual + "' split correctly",
|
||||||
};
|
};
|
||||||
|
|
||||||
const split =
|
const split =
|
||||||
splitAtDelimiters(actual,
|
splitAtDelimiters(actual, delimiters);
|
||||||
[{left: left, right: right, display: false}]);
|
|
||||||
|
|
||||||
if (split.length !== result.length) {
|
if (split.length !== result.length) {
|
||||||
message.pass = false;
|
message.pass = false;
|
||||||
@@ -60,60 +59,76 @@ beforeEach(function() {
|
|||||||
|
|
||||||
describe("A delimiter splitter", function() {
|
describe("A delimiter splitter", function() {
|
||||||
it("doesn't split when there are no delimiters", function() {
|
it("doesn't split when there are no delimiters", function() {
|
||||||
expect("hello").toSplitInto("(", ")", [{type: "text", data: "hello"}]);
|
expect("hello").toSplitInto(
|
||||||
|
[
|
||||||
|
{type: "text", data: "hello"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("doesn't create a math node with only one left delimiter", function() {
|
it("doesn't create a math node with only one left delimiter", function() {
|
||||||
expect("hello ( world").toSplitInto(
|
expect("hello ( world").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "text", data: "( world"},
|
{type: "text", data: "( world"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("doesn't split when there's only a right delimiter", function() {
|
it("doesn't split when there's only a right delimiter", function() {
|
||||||
expect("hello ) world").toSplitInto(
|
expect("hello ) world").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello ) world"},
|
{type: "text", data: "hello ) world"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("splits when there are both delimiters", function() {
|
it("splits when there are both delimiters", function() {
|
||||||
expect("hello ( world ) boo").toSplitInto(
|
expect("hello ( world ) boo").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world ",
|
{type: "math", data: " world ",
|
||||||
rawData: "( world )", display: false},
|
rawData: "( world )", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("splits on multi-character delimiters", function() {
|
it("splits on multi-character delimiters", function() {
|
||||||
expect("hello [[ world ]] boo").toSplitInto(
|
expect("hello [[ world ]] boo").toSplitInto(
|
||||||
"[[", "]]",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world ",
|
{type: "math", data: " world ",
|
||||||
rawData: "[[ world ]]", display: false},
|
rawData: "[[ world ]]", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "[[", right: "]]", display: false},
|
||||||
]);
|
]);
|
||||||
expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
|
expect("hello \\begin{equation} world \\end{equation} boo").toSplitInto(
|
||||||
"\\begin{equation}", "\\end{equation}",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: "\\begin{equation} world \\end{equation}",
|
{type: "math", data: "\\begin{equation} world \\end{equation}",
|
||||||
rawData: "\\begin{equation} world \\end{equation}",
|
rawData: "\\begin{equation} world \\end{equation}",
|
||||||
display: false},
|
display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "\\begin{equation}", right: "\\end{equation}",
|
||||||
|
display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("splits mutliple times", function() {
|
it("splits mutliple times", function() {
|
||||||
expect("hello ( world ) boo ( more ) stuff").toSplitInto(
|
expect("hello ( world ) boo ( more ) stuff").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world ",
|
{type: "math", data: " world ",
|
||||||
@@ -122,44 +137,52 @@ describe("A delimiter splitter", function() {
|
|||||||
{type: "math", data: " more ",
|
{type: "math", data: " more ",
|
||||||
rawData: "( more )", display: false},
|
rawData: "( more )", display: false},
|
||||||
{type: "text", data: " stuff"},
|
{type: "text", data: " stuff"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("leaves the ending when there's only a left delimiter", function() {
|
it("leaves the ending when there's only a left delimiter", function() {
|
||||||
expect("hello ( world ) boo ( left").toSplitInto(
|
expect("hello ( world ) boo ( left").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world ",
|
{type: "math", data: " world ",
|
||||||
rawData: "( world )", display: false},
|
rawData: "( world )", display: false},
|
||||||
{type: "text", data: " boo "},
|
{type: "text", data: " boo "},
|
||||||
{type: "text", data: "( left"},
|
{type: "text", data: "( left"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("doesn't split when close delimiters are in {}s", function() {
|
it("doesn't split when close delimiters are in {}s", function() {
|
||||||
expect("hello ( world { ) } ) boo").toSplitInto(
|
expect("hello ( world { ) } ) boo").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world { ) } ",
|
{type: "math", data: " world { ) } ",
|
||||||
rawData: "( world { ) } )", display: false},
|
rawData: "( world { ) } )", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect("hello ( world { { } ) } ) boo").toSplitInto(
|
expect("hello ( world { { } ) } ) boo").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world { { } ) } ",
|
{type: "math", data: " world { { } ) } ",
|
||||||
rawData: "( world { { } ) } )", display: false},
|
rawData: "( world { { } ) } )", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("correctly processes sequences of $..$", function() {
|
it("correctly processes sequences of $..$", function() {
|
||||||
expect("$hello$$world$$boo$").toSplitInto(
|
expect("$hello$$world$$boo$").toSplitInto(
|
||||||
"$", "$",
|
|
||||||
[
|
[
|
||||||
{type: "math", data: "hello",
|
{type: "math", data: "hello",
|
||||||
rawData: "$hello$", display: false},
|
rawData: "$hello$", display: false},
|
||||||
@@ -167,17 +190,22 @@ describe("A delimiter splitter", function() {
|
|||||||
rawData: "$world$", display: false},
|
rawData: "$world$", display: false},
|
||||||
{type: "math", data: "boo",
|
{type: "math", data: "boo",
|
||||||
rawData: "$boo$", display: false},
|
rawData: "$boo$", display: false},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "$", right: "$", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("doesn't split at escaped delimiters", function() {
|
it("doesn't split at escaped delimiters", function() {
|
||||||
expect("hello ( world \\) ) boo").toSplitInto(
|
expect("hello ( world \\) ) boo").toSplitInto(
|
||||||
"(", ")",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world \\) ",
|
{type: "math", data: " world \\) ",
|
||||||
rawData: "( world \\) )", display: false},
|
rawData: "( world \\) )", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "(", right: ")", display: false},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/* TODO(emily): make this work maybe?
|
/* TODO(emily): make this work maybe?
|
||||||
@@ -194,21 +222,25 @@ describe("A delimiter splitter", function() {
|
|||||||
|
|
||||||
it("splits when the right and left delimiters are the same", function() {
|
it("splits when the right and left delimiters are the same", function() {
|
||||||
expect("hello $ world $ boo").toSplitInto(
|
expect("hello $ world $ boo").toSplitInto(
|
||||||
"$", "$",
|
|
||||||
[
|
[
|
||||||
{type: "text", data: "hello "},
|
{type: "text", data: "hello "},
|
||||||
{type: "math", data: " world ",
|
{type: "math", data: " world ",
|
||||||
rawData: "$ world $", display: false},
|
rawData: "$ world $", display: false},
|
||||||
{type: "text", data: " boo"},
|
{type: "text", data: " boo"},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "$", right: "$", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("ignores \\$", function() {
|
it("ignores \\$", function() {
|
||||||
expect("$x = \\$5$").toSplitInto(
|
expect("$x = \\$5$").toSplitInto(
|
||||||
"$", "$",
|
|
||||||
[
|
[
|
||||||
{type: "math", data: "x = \\$5",
|
{type: "math", data: "x = \\$5",
|
||||||
rawData: "$x = \\$5$", display: false},
|
rawData: "$x = \\$5$", display: false},
|
||||||
|
],
|
||||||
|
[
|
||||||
|
{left: "$", right: "$", display: false},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user