Fix \nobreakspace, refactor "regular space" checking (#1200)

* Fix \nobreakspace, refactor "regular space" checking

Fix #1199 by adding \nobreakspace to the list of commands/symbols that
behave like a regular space character.  Refactor to put that list in one
place, and use an object so it checks instantly instead of sequentially.

* Copy test from #1201
This commit is contained in:
Erik Demaine
2018-03-09 19:14:42 -04:00
committed by Kevin Barabash
parent 017eb7b91d
commit abfb641d7a
4 changed files with 20 additions and 6 deletions

View File

@@ -682,6 +682,16 @@ const spacingFunctions: {[string]: {| size: string, className: string |}} = {
},
};
// A lookup table to determine whether a spacing function/symbol should be
// treated like a regular space character.
const regularSpace: {[string]: boolean} = {
" ": true,
"\\ ": true,
"~": true,
"\\space": true,
"\\nobreakspace": true,
};
/**
* Maps TeX font commands to objects containing:
* - variant: string used for "mathvariant" attribute in buildMathML.js
@@ -777,4 +787,5 @@ export default {
svgData,
tryCombineChars,
spacingFunctions,
regularSpace,
};

View File

@@ -419,8 +419,7 @@ groupTypes.supsub = function(group, options) {
};
groupTypes.spacing = function(group, options) {
if (group.value === "\\ " || group.value === "\\space" ||
group.value === " " || group.value === "~") {
if (buildCommon.regularSpace.hasOwnProperty(group.value)) {
// Spaces are generated by adding an actual space. Each of these
// things has an entry in the symbols table, so these will be turned
// into appropriate outputs.

View File

@@ -228,8 +228,7 @@ groupTypes.supsub = function(group, options) {
groupTypes.spacing = function(group) {
let node;
if (group.value === "\\ " || group.value === "\\space" ||
group.value === " " || group.value === "~") {
if (buildCommon.regularSpace.hasOwnProperty(group.value)) {
node = new mathMLTree.MathNode(
"mtext", [new mathMLTree.TextNode("\u00a0")]);
} else {

View File

@@ -875,6 +875,11 @@ describe("A text parser", function() {
expect(textWithEmbeddedMath).toParse();
});
it("should parse spacing functions", function() {
expect("a b\\, \\; \\! \\: ~ \\thinspace \\medspace \\quad \\ ").toBuild();
expect("\\enspace \\thickspace \\qquad \\space \\nobreakspace").toBuild();
});
it("should omit spaces after commands", function() {
expect("\\text{\\textellipsis !}")
.toParseLike("\\text{\\textellipsis!}");
@@ -2999,11 +3004,11 @@ describe("The \\mathchoice function", function() {
describe("Symbols", function() {
it("should parse \\text{\\i\\j}", () => {
expect("\\text{\\i\\j}").toParse();
expect("\\text{\\i\\j}").toBuild();
});
it("should parse spacing functions in math or text mode", () => {
expect("A\\;B\\,C\\nobreakspace \\text{A\\;B\\,C\\nobreakspace}").toParse();
expect("A\\;B\\,C\\nobreakspace \\text{A\\;B\\,C\\nobreakspace}").toBuild();
});
it("should render ligature commands like their unicode characters", () => {