Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update handling of braket bars so that the enclosing braket can be more accurately found. (mathjax/MathJax#3164) #1041

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ts/input/tex/Stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class Stack {


/**
* Lookup the nth elements on the stack without removing them.
* Look up the nth elements on the stack without removing them.
* @param {number=} n Position of element that should be returned. Default 1.
* @return {StackItem} Nth item on the stack.
*/
Expand All @@ -137,7 +137,7 @@ export default class Stack {


/**
* Lookup the topmost element on the stack, returning the Mml node in that
* Look up the topmost element on the stack, returning the Mml node in that
* item. Optionally pops the Mml node from that stack item.
* @param {boolean=} noPop Pop top item if true.
* @return {MmlNode} The Mml node in the topmost stack item.
Expand All @@ -147,6 +147,14 @@ export default class Stack {
return noPop ? top.First : top.Pop();
}

/**
* Look up the current number of stack items.
* @return {number} The number of items on the stack.
*/
public get height(): number {
return this.stack.length;
}


/**
* @override
Expand Down
3 changes: 2 additions & 1 deletion ts/input/tex/braket/BraketConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const BraketConfiguration = Configuration.create(
},
items: {
[BraketItem.prototype.kind]: BraketItem,
}
},
priority: 3 // must come before base configuration
}
);

Expand Down
21 changes: 9 additions & 12 deletions ts/input/tex/braket/BraketMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ BraketMethods.Braket = function(parser: TexParser, _name: string,
parser.i++;
single = false;
}
parser.Push(
parser.itemFactory.create('braket')
.setProperties({barcount: 0, barmax, open, close, stretchy, single, space}));
const node = parser.itemFactory.create('braket');
node.setProperties({barcount: 0, barmax, open, close, stretchy, single, space});
parser.Push(node);
node.env.braketItem = parser.stack.height - 1;
};


Expand All @@ -71,20 +72,16 @@ BraketMethods.Braket = function(parser: TexParser, _name: string,
*/
BraketMethods.Bar = function(parser: TexParser, name: string): ParseResult {
let c = name === '|' ? '|' : '\u2016';
let top = parser.stack.Top() as BraketItem;
if (top.isKind('over')) {
// If the top item is from \over, use the previous one
top = parser.stack.Top(2) as BraketItem;
}
if (!top.isKind('braket') || top.getProperty('barcount') >= top.getProperty('barmax')) {
let n = parser.stack.height - (parser.stack.env.braketItem as number);
let top = parser.stack.Top(n) as BraketItem;
if (!top || !top.isKind('braket') || top.getProperty('barcount') >= top.getProperty('barmax')) {
return false;
}
if (c === '|' && parser.GetNext() === '|') {
parser.i++;
c = '\u2016';
}
let stretchy = top.getProperty('stretchy');
if (!stretchy) {
if (!top.getProperty('stretchy')) {
let node = parser.create('token', 'mo', {stretchy: false, 'data-braketbar': true, texClass: TEXCLASS.ORD}, c);
parser.Push(node);
return;
Expand All @@ -97,7 +94,7 @@ BraketMethods.Bar = function(parser: TexParser, name: string): ParseResult {
//
// Push a CLOSE atom, the bar as a BIN, and an OPEN atom onto the barNodes,
// which will be added into the toMml() output. This allows \over to be used
// after any bars
// before, between, or after any bars
//
top.barNodes.push(
parser.create('node', 'TeXAtom', [], {texClass: TEXCLASS.CLOSE}),
Expand Down