From c0d4267ca90d50ec3657a03637d91860b9927a91 Mon Sep 17 00:00:00 2001 From: Sgt_Sinky <1676751+Sinkleberg@users.noreply.github.com> Date: Fri, 9 Oct 2026 23:03:39 -0400 Subject: [PATCH] Recognize abbreviations containing ampersands --- Michelf/MarkdownExtra.php | 3 ++- test/unit/MarkdownExtraTest.php | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Michelf/MarkdownExtra.php b/Michelf/MarkdownExtra.php index 78e265d4..b93aca94 100644 --- a/Michelf/MarkdownExtra.php +++ b/Michelf/MarkdownExtra.php @@ -170,6 +170,7 @@ protected function setup() { $this->footnotes_assembled = null; foreach ($this->predef_abbr as $abbr_word => $abbr_desc) { + $abbr_word = $this->encodeAmpsAndAngles($abbr_word); if ($this->abbr_word_re) $this->abbr_word_re .= '|'; $this->abbr_word_re .= preg_quote($abbr_word); @@ -1827,7 +1828,7 @@ protected function stripAbbreviations($text) { * @return string */ protected function _stripAbbreviations_callback($matches) { - $abbr_word = $matches[1]; + $abbr_word = $this->encodeAmpsAndAngles($matches[1]); $abbr_desc = $matches[2]; if ($this->abbr_word_re) { $this->abbr_word_re .= '|'; diff --git a/test/unit/MarkdownExtraTest.php b/test/unit/MarkdownExtraTest.php index 42d7b8a6..5fba73e3 100644 --- a/test/unit/MarkdownExtraTest.php +++ b/test/unit/MarkdownExtraTest.php @@ -36,6 +36,41 @@ public function testSetupOfMultiplePredefinedAttributes() ); } + public function testAbbreviationsContainingAmpersands() + { + $obj = new \Michelf\MarkdownExtra(); + $result = $obj->transform("AB&D and AB&Ds. `AB&D`\n\n*[AB&D]: Automated Build & Distribution"); + + $this->assertSame( + '
AB&D' .
+ ' and AB&Ds. AB&D
AB&D
', + trim($obj->transform('AB&D')) + ); + } + + public function testAbbreviationsRespectNoEntities() + { + $obj = new \Michelf\MarkdownExtra(); + $obj->no_entities = true; + $obj->predef_abbr = array('AB&D' => 'Build & Distribution'); + + $this->assertSame( + 'AB&D
', + trim($obj->transform('AB&D')) + ); + } + public function testTransformWithNoMarkup() { $obj = new \Michelf\MarkdownExtra();