From 566944c42d80ee5ae973a5e3896b33401b564d54 Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Thu, 1 Dec 2022 17:05:30 +0100 Subject: [PATCH] Fix broken/incomplete regex patterns in TexNode::texContainsFunc I compared with https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/services/texvcjs/+/6c6988c4f6fe6f0a83a3d44bf1152dafbec2409a/lib/astutil.js and found two mistakes: * Missing space at the end of the color regex. * Not enough backslash escaping in the last regex. Note how the code in lines #116 and #130 is now identical. Change-Id: I13b75ad4a1e4da0766c0d73b8786b21865945697 --- src/TexVC/Nodes/TexNode.php | 4 ++-- tests/phpunit/unit/TexVC/Nodes/TexNodeTest.php | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/TexVC/Nodes/TexNode.php b/src/TexVC/Nodes/TexNode.php index 54d05ac16..24e1f208d 100644 --- a/src/TexVC/Nodes/TexNode.php +++ b/src/TexVC/Nodes/TexNode.php @@ -120,14 +120,14 @@ class TexNode { // special case #3: \\color, \\pagecolor, \\definecolor $matches = []; - $m = preg_match( '/^(\\\\(color|pagecolor|definecolor))/', $t, $matches ); + $m = preg_match( '/^(\\\\(?:page|define)?color) /', $t, $matches ); if ( $m == 1 ) { return self::match( $target, $matches[1] ); } // special case #4: \\mathbb, \\mathrm $matches = []; - $m = preg_match( '/^(\\\\math..) \{(\\.*)}$/', $t, $matches ); + $m = preg_match( '/^(\\\\math..) \{(\\\\.*)}$/', $t, $matches ); if ( $m == 1 ) { return self::match( $target, $matches[1] ) ?: self::match( $target, $matches[2] ); } diff --git a/tests/phpunit/unit/TexVC/Nodes/TexNodeTest.php b/tests/phpunit/unit/TexVC/Nodes/TexNodeTest.php index 2665b8a3f..031fbd439 100644 --- a/tests/phpunit/unit/TexVC/Nodes/TexNodeTest.php +++ b/tests/phpunit/unit/TexVC/Nodes/TexNodeTest.php @@ -137,21 +137,17 @@ class TexNodeTest extends MediaWikiUnitTestCase { [ '\\mismatch', '\\mbox{\\somefunc}', false ], [ '\\somefunc', '\\mbox {\\somefunc}', false ], [ '\\color', '\\color' ], - // FIXME: This might be to relaxed; maybe add a \b to the regex? - [ '\\color', '\\colorscheme because the rest is ignored' ], + [ '\\color', '\\color the rest is ignored' ], [ '\\pagecolor', '\\pagecolor' ], [ '\\definecolor', '\\definecolor' ], - [ '\\mathbb', '\\mathbb {}' ], + [ '\\mathbb', '\\mathbb {}', false ], [ '\\mathbb', '\\mathbb {A}', false ], [ '\\mathbb', '\\mathbb {foo}', false ], [ '\\mathbb', '\\mathbb{}', false ], - - // FIXME: I believe these don't make sense; mistake in the regex? - [ '\\mathbb', '\\mathbb {.}' ], - [ '\\mathbb', '\\mathbb {..........}' ], - // FIXME: I believe these should both succeed - [ '\\mathbb', '\\mathbb {\\foo}', false ], - [ '\\foo', '\\mathbb {\\foo}', false ], + [ '\\mathbb', '\\mathbb {.}', false ], + [ '\\mathbb', '\\mathbb {..........}', false ], + [ '\\mathbb', '\\mathbb {\\foo}' ], + [ '\\foo', '\\mathbb {\\foo}' ], ]; }