@@ -36,17 +36,22 @@ func (s *rawHTMLParser) Parse(parent ast.Node, block text.Reader, pc Context) as
3636 return s .parseComment (block , pc )
3737 }
3838 if bytes .HasPrefix (line , openProcessingInstruction ) {
39- return s .parseUntil (block , closeProcessingInstruction , pc )
39+ return s .parseUntil (block , closeProcessingInstruction , closeProcessingInstructionCacheKey , pc )
4040 }
4141 if len (line ) > 2 && line [1 ] == '!' && line [2 ] >= 'A' && line [2 ] <= 'Z' {
42- return s .parseUntil (block , closeDecl , pc )
42+ return s .parseUntil (block , closeDecl , closeDeclCacheKey , pc )
4343 }
4444 if bytes .HasPrefix (line , openCDATA ) {
45- return s .parseUntil (block , closeCDATA , pc )
45+ return s .parseUntil (block , closeCDATA , closeCDATACacheKey , pc )
4646 }
4747 return nil
4848}
4949
50+ var closeProcessingInstructionCacheKey = NewContextKey ()
51+ var closeDeclCacheKey = NewContextKey ()
52+ var closeCDATACacheKey = NewContextKey ()
53+ var closeCommentCacheKey = NewContextKey ()
54+
5055var tagnamePattern = `([A-Za-z][A-Za-z0-9-]*)`
5156var spaceOrOneNewline = `(?:[ \t]|(?:\r\n|\n){0,1})`
5257var attributePattern = `(?:[\r\n \t]+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:[\r\n \t]*=[\r\n \t]*(?:[^\"'=<>` + "`" + `\x00-\x20]+|'[^']*'|"[^"]*"))?)` //nolint:golint,lll
@@ -63,7 +68,7 @@ var emptyComment2 = []byte("<!--->")
6368var openComment = []byte ("<!--" )
6469var closeComment = []byte ("-->" )
6570
66- func (s * rawHTMLParser ) parseComment (block text.Reader , _ Context ) ast.Node {
71+ func (s * rawHTMLParser ) parseComment (block text.Reader , pc Context ) ast.Node {
6772 savedLine , savedSegment := block .Position ()
6873 node := ast .NewRawHTML ()
6974 line , segment := block .PeekLine ()
@@ -77,8 +82,14 @@ func (s *rawHTMLParser) parseComment(block text.Reader, _ Context) ast.Node {
7782 block .Advance (len (emptyComment2 ))
7883 return node
7984 }
85+ // A previous search failed up to cache.pos, so there is no closer before it.
86+ cache := pc .ComputeIfAbsent (closeCommentCacheKey , func () any { return & posCache {} }).(* posCache )
87+ if segment .Start < cache .pos {
88+ return nil
89+ }
8090 offset := len (openComment )
8191 line = line [offset :]
92+ lastStop := segment .Stop
8293 for {
8394 index := bytes .Index (line , closeComment )
8495 if index > - 1 {
@@ -88,33 +99,44 @@ func (s *rawHTMLParser) parseComment(block text.Reader, _ Context) ast.Node {
8899 }
89100 offset = 0
90101 node .Segments .Append (segment )
102+ lastStop = segment .Stop
91103 block .AdvanceLine ()
92104 line , segment = block .PeekLine ()
93105 if line == nil {
94106 break
95107 }
96108 }
109+ cache .pos = lastStop
97110 block .SetPosition (savedLine , savedSegment )
98111 return nil
99112}
100113
101- func (s * rawHTMLParser ) parseUntil (block text.Reader , closer []byte , _ Context ) ast.Node {
114+ func (s * rawHTMLParser ) parseUntil (block text.Reader , closer []byte , ck ContextKey , pc Context ) ast.Node {
102115 savedLine , savedSegment := block .Position ()
103116 node := ast .NewRawHTML ()
117+ // A previous search failed up to cache.pos, so there is no closer before it.
118+ cache := pc .ComputeIfAbsent (ck , func () any { return & posCache {} }).(* posCache )
119+ lastStop := 0
104120 for {
105121 line , segment := block .PeekLine ()
106122 if line == nil {
107123 break
108124 }
125+ if segment .Start < cache .pos {
126+ block .SetPosition (savedLine , savedSegment )
127+ return nil
128+ }
109129 index := bytes .Index (line , closer )
110130 if index > - 1 {
111131 node .Segments .Append (segment .WithStop (segment .Start + index + len (closer )))
112132 block .Advance (index + len (closer ))
113133 return node
114134 }
115135 node .Segments .Append (segment )
136+ lastStop = segment .Stop
116137 block .AdvanceLine ()
117138 }
139+ cache .pos = lastStop
118140 block .SetPosition (savedLine , savedSegment )
119141 return nil
120142}
0 commit comments