@@ -725,13 +725,48 @@ def testInlineProcessorDoesntCrashWithWrongAtomicString(self):
725725 '<div><p>a <b>atomic</b> c</p></div>'
726726 )
727727
728+ def testInlineProcessorAdvancesSearchIndex (self ):
729+ """Test that repeated matches resume after the previous match."""
730+ pattern = _InlineProcessorThatRecordsSearchIndex (r'x' , self .md )
731+ self .md .inlinePatterns .register (pattern , 'record-search-index' , 1000 )
732+
733+ self .assertEqual (self .md .convert ('xxxx' ), '<p>xxxx</p>' )
734+ self .assertEqual (pattern .start_indices [0 ], 0 )
735+ self .assertGreater (pattern .start_indices [1 ], pattern .start_indices [0 ])
736+
728737
729738class _InlineProcessorThatReturnsAtomicString (inlinepatterns .InlineProcessor ):
730739 """ Return a simple text of `group(1)` of a Pattern. """
731740 def handleMatch (self , m , data ):
732741 return markdown .util .AtomicString ('<b>atomic</b>' ), m .start (0 ), m .end (0 )
733742
734743
744+ class _RecordingPattern :
745+ """Proxy a compiled pattern while recording the search offsets."""
746+
747+ def __init__ (self , pattern , start_indices ):
748+ self .pattern = pattern
749+ self .start_indices = start_indices
750+
751+ def finditer (self , data , start_index = 0 ):
752+ self .start_indices .append (start_index )
753+ return self .pattern .finditer (data , start_index )
754+
755+
756+ class _InlineProcessorThatRecordsSearchIndex (inlinepatterns .InlineProcessor ):
757+ """Record each offset passed to the processor's compiled expression."""
758+
759+ def __init__ (self , pattern , md ):
760+ super ().__init__ (pattern , md )
761+ self .start_indices = []
762+
763+ def getCompiledRegExp (self ):
764+ return _RecordingPattern (self .compiled_re , self .start_indices )
765+
766+ def handleMatch (self , m , data ):
767+ return m .group (0 ), m .start (0 ), m .end (0 )
768+
769+
735770class TestConfigParsing (unittest .TestCase ):
736771 def assertParses (self , value , result ):
737772 self .assertIs (markdown .util .parseBoolValue (value , False ), result )
0 commit comments