Skip to content

KaTeX(6/n): content: Handle 'nulldelimiter' KaTeX CSS #1633

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion lib/model/katex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class _KatexParser {
// A copy of class definition (where possible) is accompanied in a comment
// with each case statement to keep track of updates.
final spanClasses = List<String>.unmodifiable(element.className.split(' '));
double? widthEm;
String? fontFamily;
double? fontSizeEm;
KatexSpanFontWeight? fontWeight;
Expand Down Expand Up @@ -387,7 +388,11 @@ class _KatexParser {
_ => throw _KatexHtmlParseError(),
};

// TODO handle .nulldelimiter and .delimcenter .
case 'nulldelimiter':
// .nulldelimiter { display: inline-block; width: 0.12em; }
widthEm = 0.12;

// TODO .delimcenter .

case 'op-symbol':
// .op-symbol { ... }
Expand Down Expand Up @@ -430,6 +435,7 @@ class _KatexParser {
}
}
final styles = KatexSpanStyles(
widthEm: widthEm,
fontFamily: fontFamily,
fontSizeEm: fontSizeEm,
fontWeight: fontWeight,
Expand Down Expand Up @@ -523,6 +529,7 @@ enum KatexSpanTextAlign {

@immutable
class KatexSpanStyles {
final double? widthEm;
final double? heightEm;

final String? fontFamily;
Expand All @@ -532,6 +539,7 @@ class KatexSpanStyles {
final KatexSpanTextAlign? textAlign;

const KatexSpanStyles({
this.widthEm,
this.heightEm,
this.fontFamily,
this.fontSizeEm,
Expand All @@ -543,6 +551,7 @@ class KatexSpanStyles {
@override
int get hashCode => Object.hash(
'KatexSpanStyles',
widthEm,
heightEm,
fontFamily,
fontSizeEm,
Expand All @@ -554,6 +563,7 @@ class KatexSpanStyles {
@override
bool operator ==(Object other) {
return other is KatexSpanStyles &&
other.widthEm == widthEm &&
other.heightEm == heightEm &&
other.fontFamily == fontFamily &&
other.fontSizeEm == fontSizeEm &&
Expand All @@ -565,6 +575,7 @@ class KatexSpanStyles {
@override
String toString() {
final args = <String>[];
if (widthEm != null) args.add('widthEm: $widthEm');
if (heightEm != null) args.add('heightEm: $heightEm');
if (fontFamily != null) args.add('fontFamily: $fontFamily');
if (fontSizeEm != null) args.add('fontSizeEm: $fontSizeEm');
Expand All @@ -583,6 +594,7 @@ class KatexSpanStyles {
/// had `inherit` set to true.
KatexSpanStyles merge(KatexSpanStyles other) {
return KatexSpanStyles(
widthEm: other.widthEm ?? widthEm,
heightEm: other.heightEm ?? heightEm,
fontFamily: other.fontFamily ?? fontFamily,
fontSizeEm: other.fontSizeEm ?? fontSizeEm,
Expand Down
3 changes: 3 additions & 0 deletions lib/widgets/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ class _KatexSpan extends StatelessWidget {
}

return SizedBox(
width: styles.widthEm != null
? styles.widthEm! * (fontSize ?? em)
: null,
height: styles.heightEm != null
? styles.heightEm! * (fontSize ?? em)
: null,
Expand Down
62 changes: 62 additions & 0 deletions test/model/content_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,67 @@ class ContentExample {
]),
]);

static const mathBlockKatexNulldelimiter = ContentExample(
'math block; KaTeX nulldelimiter',
// https://chat.zulip.org/#narrow/channel/7-test-here/topic/Rajesh/near/2205534
'```math\n\\left. a \\middle. b \\right.\n```',
'<p>'
'<span class="katex-display"><span class="katex">'
'<span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>a</mi><mo fence="true" lspace="0.05em" rspace="0.05em">.</mo><mi>b</mi></mrow><annotation encoding="application/x-tex">\\left. a \\middle. b \\right.</annotation></semantics></math></span>'
'<span class="katex-html" aria-hidden="true">'
'<span class="base">'
'<span class="strut" style="height:0.6944em;"></span>'
'<span class="minner">'
'<span class="mopen nulldelimiter"></span>'
'<span class="mord mathnormal">a</span>'
'<span class="nulldelimiter"></span>'
'<span class="mord mathnormal">b</span>'
'<span class="mclose nulldelimiter"></span></span></span></span></span></span></p>',
[
MathBlockNode(
texSource: '\\left. a \\middle. b \\right.',
nodes: [
KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
KatexSpanNode(
styles: KatexSpanStyles(heightEm: 0.6944),
text: null,
nodes: []),
KatexSpanNode(
styles: KatexSpanStyles(),
text: null,
nodes: [
KatexSpanNode(
styles: KatexSpanStyles(widthEm: 0.12),
text: null,
nodes: []),
KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
text: 'a',
nodes: null),
KatexSpanNode(
styles: KatexSpanStyles(widthEm: 0.12),
text: null,
nodes: []),
KatexSpanNode(
styles: KatexSpanStyles(
fontFamily: 'KaTeX_Math',
fontStyle: KatexSpanFontStyle.italic),
text: 'b',
nodes: null),
KatexSpanNode(
styles: KatexSpanStyles(widthEm: 0.12),
text: null,
nodes: []),
]),
]),
]),
]);

static const imageSingle = ContentExample(
'single image',
// https://chat.zulip.org/#narrow/stream/7-test-here/topic/Thumbnails/near/1900103
Expand Down Expand Up @@ -1967,6 +2028,7 @@ void main() async {
// `vertical-align` in inline styles. Currently it fails
// because `strut` span has `vertical-align`.
testParseExample(ContentExample.mathBlockKatexDelimSizing, skip: true);
testParseExample(ContentExample.mathBlockKatexNulldelimiter);

testParseExample(ContentExample.imageSingle);
testParseExample(ContentExample.imageSingleNoDimensions);
Expand Down