1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.Razor.Editor;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Resources;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer.Symbols;
namespace System.Web.Razor.Parser
{
public partial class HtmlMarkupParser
{
private SourceLocation _lastTagStart = SourceLocation.Zero;
private HtmlSymbol _bufferedOpenAngle;
public override void ParseBlock()
{
if (Context == null)
{
throw new InvalidOperationException(RazorResources.Parser_Context_Not_Set);
}
using (PushSpanConfig(DefaultMarkupSpan))
{
using (Context.StartBlock(BlockType.Markup))
{
if (!NextToken())
{
return;
}
AcceptWhile(IsSpacingToken(includeNewLines: true));
if (CurrentSymbol.Type == HtmlSymbolType.OpenAngle)
{
// "<" => Implicit Tag Block
TagBlock(new Stack<Tuple<HtmlSymbol, SourceLocation>>());
}
else if (CurrentSymbol.Type == HtmlSymbolType.Transition)
{
// "@" => Explicit Tag/Single Line Block OR Template
Output(SpanKind.Markup);
// Definitely have a transition span
Assert(HtmlSymbolType.Transition);
AcceptAndMoveNext();
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
Span.CodeGenerator = SpanCodeGenerator.Null;
Output(SpanKind.Transition);
if (At(HtmlSymbolType.Transition))
{
Span.CodeGenerator = SpanCodeGenerator.Null;
AcceptAndMoveNext();
Output(SpanKind.MetaCode);
}
AfterTransition();
}
else
{
Context.OnError(CurrentSymbol.Start, RazorResources.ParseError_MarkupBlock_Must_Start_With_Tag);
}
Output(SpanKind.Markup);
}
}
}
private void DefaultMarkupSpan(SpanBuilder span)
{
span.CodeGenerator = new MarkupCodeGenerator();
span.EditHandler = new SpanEditHandler(Language.TokenizeString, AcceptedCharacters.Any);
}
private void AfterTransition()
{
// "@:" => Explicit Single Line Block
if (CurrentSymbol.Type == HtmlSymbolType.Text && CurrentSymbol.Content.Length > 0 && CurrentSymbol.Content[0] == ':')
{
// Split the token
Tuple<HtmlSymbol, HtmlSymbol> split = Language.SplitSymbol(CurrentSymbol, 1, HtmlSymbolType.Colon);
// The first part (left) is added to this span and we return a MetaCode span
Accept(split.Item1);
Span.CodeGenerator = SpanCodeGenerator.Null;
Output(SpanKind.MetaCode);
if (split.Item2 != null)
{
Accept(split.Item2);
}
NextToken();
SingleLineMarkup();
}
else if (CurrentSymbol.Type == HtmlSymbolType.OpenAngle)
{
TagBlock(new Stack<Tuple<HtmlSymbol, SourceLocation>>());
}
}
private void SingleLineMarkup()
{
// Parse until a newline, it's that simple!
// First, signal to code parser that whitespace is significant to us.
bool old = Context.WhiteSpaceIsSignificantToAncestorBlock;
Context.WhiteSpaceIsSignificantToAncestorBlock = true;
Span.EditHandler = new SingleLineMarkupEditHandler(Language.TokenizeString);
SkipToAndParseCode(HtmlSymbolType.NewLine);
if (!EndOfFile && CurrentSymbol.Type == HtmlSymbolType.NewLine)
{
AcceptAndMoveNext();
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
}
PutCurrentBack();
Context.WhiteSpaceIsSignificantToAncestorBlock = old;
Output(SpanKind.Markup);
}
private void TagBlock(Stack<Tuple<HtmlSymbol, SourceLocation>> tags)
{
// Skip Whitespace and Text
bool complete = false;
do
{
SkipToAndParseCode(HtmlSymbolType.OpenAngle);
if (EndOfFile)
{
EndTagBlock(tags, complete: true);
}
else
{
_bufferedOpenAngle = null;
_lastTagStart = CurrentLocation;
Assert(HtmlSymbolType.OpenAngle);
_bufferedOpenAngle = CurrentSymbol;
SourceLocation tagStart = CurrentLocation;
if (!NextToken())
{
Accept(_bufferedOpenAngle);
EndTagBlock(tags, complete: false);
}
else
{
complete = AfterTagStart(tagStart, tags);
}
}
}
while (tags.Count > 0);
EndTagBlock(tags, complete);
}
private bool AfterTagStart(SourceLocation tagStart, Stack<Tuple<HtmlSymbol, SourceLocation>> tags)
{
if (!EndOfFile)
{
switch (CurrentSymbol.Type)
{
case HtmlSymbolType.Solidus:
// End Tag
return EndTag(tagStart, tags);
case HtmlSymbolType.Bang:
// Comment
Accept(_bufferedOpenAngle);
return BangTag();
case HtmlSymbolType.QuestionMark:
// XML PI
Accept(_bufferedOpenAngle);
return XmlPI();
default:
// Start Tag
return StartTag(tags);
}
}
if (tags.Count == 0)
{
Context.OnError(CurrentLocation, RazorResources.ParseError_OuterTagMissingName);
}
return false;
}
private bool XmlPI()
{
// Accept "?"
Assert(HtmlSymbolType.QuestionMark);
AcceptAndMoveNext();
return AcceptUntilAll(HtmlSymbolType.QuestionMark, HtmlSymbolType.CloseAngle);
}
private bool BangTag()
{
// Accept "!"
Assert(HtmlSymbolType.Bang);
AcceptAndMoveNext();
if (CurrentSymbol.Type == HtmlSymbolType.DoubleHyphen)
{
AcceptAndMoveNext();
return AcceptUntilAll(HtmlSymbolType.DoubleHyphen, HtmlSymbolType.CloseAngle);
}
else if (CurrentSymbol.Type == HtmlSymbolType.LeftBracket)
{
AcceptAndMoveNext();
return CData();
}
else
{
AcceptAndMoveNext();
return AcceptUntilAll(HtmlSymbolType.CloseAngle);
}
}
private bool CData()
{
if (CurrentSymbol.Type == HtmlSymbolType.Text && String.Equals(CurrentSymbol.Content, "cdata", StringComparison.OrdinalIgnoreCase))
{
AcceptAndMoveNext();
if (CurrentSymbol.Type == HtmlSymbolType.LeftBracket)
{
return AcceptUntilAll(HtmlSymbolType.RightBracket, HtmlSymbolType.RightBracket, HtmlSymbolType.CloseAngle);
}
}
return false;
}
private bool EndTag(SourceLocation tagStart, Stack<Tuple<HtmlSymbol, SourceLocation>> tags)
{
// Accept "/" and move next
Assert(HtmlSymbolType.Solidus);
HtmlSymbol solidus = CurrentSymbol;
if (!NextToken())
{
Accept(_bufferedOpenAngle);
Accept(solidus);
return false;
}
else
{
string tagName = String.Empty;
if (At(HtmlSymbolType.Text))
{
tagName = CurrentSymbol.Content;
}
bool matched = RemoveTag(tags, tagName, tagStart);
if (tags.Count == 0 &&
String.Equals(tagName, SyntaxConstants.TextTagName, StringComparison.OrdinalIgnoreCase) &&
matched)
{
Output(SpanKind.Markup);
return EndTextTag(solidus);
}
Accept(_bufferedOpenAngle);
Accept(solidus);
AcceptUntil(HtmlSymbolType.CloseAngle);
// Accept the ">"
return Optional(HtmlSymbolType.CloseAngle);
}
}
private bool EndTextTag(HtmlSymbol solidus)
{
SourceLocation start = _bufferedOpenAngle.Start;
Accept(_bufferedOpenAngle);
Accept(solidus);
Assert(HtmlSymbolType.Text);
AcceptAndMoveNext();
bool seenCloseAngle = Optional(HtmlSymbolType.CloseAngle);
if (!seenCloseAngle)
{
Context.OnError(start, RazorResources.ParseError_TextTagCannotContainAttributes);
}
else
{
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
}
Span.CodeGenerator = SpanCodeGenerator.Null;
Output(SpanKind.Transition);
return seenCloseAngle;
}
private bool IsTagRecoveryStopPoint(HtmlSymbol sym)
{
return sym.Type == HtmlSymbolType.CloseAngle ||
sym.Type == HtmlSymbolType.Solidus ||
sym.Type == HtmlSymbolType.OpenAngle ||
sym.Type == HtmlSymbolType.SingleQuote ||
sym.Type == HtmlSymbolType.DoubleQuote;
}
private void TagContent()
{
if (!At(HtmlSymbolType.WhiteSpace))
{
// We should be right after the tag name, so if there's no whitespace, something is wrong
RecoverToEndOfTag();
}
else
{
// We are here ($): <tag$ foo="bar" biz="~/Baz" />
while (!EndOfFile && !IsEndOfTag())
{
BeforeAttribute();
}
}
}
private bool IsEndOfTag()
{
if (At(HtmlSymbolType.Solidus))
{
if (NextIs(HtmlSymbolType.CloseAngle))
{
return true;
}
else
{
AcceptAndMoveNext();
}
}
return At(HtmlSymbolType.CloseAngle) || At(HtmlSymbolType.OpenAngle);
}
private void BeforeAttribute()
{
// http://dev.w3.org/html5/spec/tokenization.html#before-attribute-name-state
// Capture whitespace
var whitespace = ReadWhile(sym => sym.Type == HtmlSymbolType.WhiteSpace || sym.Type == HtmlSymbolType.NewLine);
if (At(HtmlSymbolType.Transition))
{
// Transition outside of attribute value => Switch to recovery mode
Accept(whitespace);
RecoverToEndOfTag();
return;
}
// http://dev.w3.org/html5/spec/tokenization.html#attribute-name-state
// Read the 'name' (i.e. read until the '=' or whitespace/newline)
var name = Enumerable.Empty<HtmlSymbol>();
if (At(HtmlSymbolType.Text))
{
name = ReadWhile(sym =>
sym.Type != HtmlSymbolType.WhiteSpace &&
sym.Type != HtmlSymbolType.NewLine &&
sym.Type != HtmlSymbolType.Equals &&
sym.Type != HtmlSymbolType.CloseAngle &&
sym.Type != HtmlSymbolType.OpenAngle &&
(sym.Type != HtmlSymbolType.Solidus || !NextIs(HtmlSymbolType.CloseAngle)));
}
else
{
// Unexpected character in tag, enter recovery
Accept(whitespace);
RecoverToEndOfTag();
return;
}
if (!At(HtmlSymbolType.Equals))
{
// Saw a space or newline after the name, so just skip this attribute and continue around the loop
Accept(whitespace);
Accept(name);
return;
}
Output(SpanKind.Markup);
// Start a new markup block for the attribute
using (Context.StartBlock(BlockType.Markup))
{
AttributePrefix(whitespace, name);
}
}
private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<HtmlSymbol> nameSymbols)
{
// First, determine if this is a 'data-' attribute (since those can't use conditional attributes)
LocationTagged<string> name = nameSymbols.GetContent(Span.Start);
bool attributeCanBeConditional = !name.Value.StartsWith("data-", StringComparison.OrdinalIgnoreCase);
// Accept the whitespace and name
Accept(whitespace);
Accept(nameSymbols);
Assert(HtmlSymbolType.Equals); // We should be at "="
AcceptAndMoveNext();
HtmlSymbolType quote = HtmlSymbolType.Unknown;
if (At(HtmlSymbolType.SingleQuote) || At(HtmlSymbolType.DoubleQuote))
{
quote = CurrentSymbol.Type;
AcceptAndMoveNext();
}
// We now have the prefix: (i.e. ' foo="')
LocationTagged<string> prefix = Span.GetContent();
if (attributeCanBeConditional)
{
Span.CodeGenerator = SpanCodeGenerator.Null; // The block code generator will render the prefix
Output(SpanKind.Markup);
// Read the values
while (!EndOfFile && !IsEndOfAttributeValue(quote, CurrentSymbol))
{
AttributeValue(quote);
}
// Capture the suffix
LocationTagged<string> suffix = new LocationTagged<string>(String.Empty, CurrentLocation);
if (quote != HtmlSymbolType.Unknown && At(quote))
{
suffix = CurrentSymbol.GetContent();
AcceptAndMoveNext();
}
if (Span.Symbols.Count > 0)
{
Span.CodeGenerator = SpanCodeGenerator.Null; // Again, block code generator will render the suffix
Output(SpanKind.Markup);
}
// Create the block code generator
Context.CurrentBlock.CodeGenerator = new AttributeBlockCodeGenerator(
name, prefix, suffix);
}
else
{
// Not a "conditional" attribute, so just read the value
SkipToAndParseCode(sym => IsEndOfAttributeValue(quote, sym));
if (quote != HtmlSymbolType.Unknown)
{
Optional(quote);
}
Output(SpanKind.Markup);
}
}
private void AttributeValue(HtmlSymbolType quote)
{
SourceLocation prefixStart = CurrentLocation;
var prefix = ReadWhile(sym => sym.Type == HtmlSymbolType.WhiteSpace || sym.Type == HtmlSymbolType.NewLine);
Accept(prefix);
if (At(HtmlSymbolType.Transition))
{
SourceLocation valueStart = CurrentLocation;
PutCurrentBack();
// Output the prefix but as a null-span. DynamicAttributeBlockCodeGenerator will render it
Span.CodeGenerator = SpanCodeGenerator.Null;
// Dynamic value, start a new block and set the code generator
using (Context.StartBlock(BlockType.Markup))
{
Context.CurrentBlock.CodeGenerator = new DynamicAttributeBlockCodeGenerator(prefix.GetContent(prefixStart), valueStart);
OtherParserBlock();
}
}
else if (At(HtmlSymbolType.Text) && CurrentSymbol.Content.Length > 0 && CurrentSymbol.Content[0] == '~' && NextIs(HtmlSymbolType.Solidus))
{
// Virtual Path value
SourceLocation valueStart = CurrentLocation;
VirtualPath();
Span.CodeGenerator = new LiteralAttributeCodeGenerator(
prefix.GetContent(prefixStart),
new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), valueStart));
}
else
{
// Literal value
// 'quote' should be "Unknown" if not quoted and symbols coming from the tokenizer should never have "Unknown" type.
var value = ReadWhile(sym =>
// These three conditions find separators which break the attribute value into portions
sym.Type != HtmlSymbolType.WhiteSpace &&
sym.Type != HtmlSymbolType.NewLine &&
sym.Type != HtmlSymbolType.Transition &&
// This condition checks for the end of the attribute value (it repeats some of the checks above but for now that's ok)
!IsEndOfAttributeValue(quote, sym));
Accept(value);
Span.CodeGenerator = new LiteralAttributeCodeGenerator(prefix.GetContent(prefixStart), value.GetContent(prefixStart));
}
Output(SpanKind.Markup);
}
private bool IsEndOfAttributeValue(HtmlSymbolType quote, HtmlSymbol sym)
{
return EndOfFile || sym == null ||
(quote != HtmlSymbolType.Unknown
? sym.Type == quote // If quoted, just wait for the quote
: IsUnquotedEndOfAttributeValue(sym));
}
private bool IsUnquotedEndOfAttributeValue(HtmlSymbol sym)
{
// If unquoted, we have a larger set of terminating characters:
// http://dev.w3.org/html5/spec/tokenization.html#attribute-value-unquoted-state
// Also we need to detect "/" and ">"
return sym.Type == HtmlSymbolType.DoubleQuote ||
sym.Type == HtmlSymbolType.SingleQuote ||
sym.Type == HtmlSymbolType.OpenAngle ||
sym.Type == HtmlSymbolType.Equals ||
(sym.Type == HtmlSymbolType.Solidus && NextIs(HtmlSymbolType.CloseAngle)) ||
sym.Type == HtmlSymbolType.CloseAngle ||
sym.Type == HtmlSymbolType.WhiteSpace ||
sym.Type == HtmlSymbolType.NewLine;
}
private void VirtualPath()
{
Assert(HtmlSymbolType.Text);
Debug.Assert(CurrentSymbol.Content.Length > 0 && CurrentSymbol.Content[0] == '~');
// Parse until a transition symbol, whitespace, newline or quote. We support only a fairly minimal subset of Virtual Paths
AcceptUntil(HtmlSymbolType.Transition, HtmlSymbolType.WhiteSpace, HtmlSymbolType.NewLine, HtmlSymbolType.SingleQuote, HtmlSymbolType.DoubleQuote);
// Output a Virtual Path span
Span.EditHandler.EditorHints = EditorHints.VirtualPath;
}
private void RecoverToEndOfTag()
{
// Accept until ">", "/" or "<", but parse code
while (!EndOfFile)
{
SkipToAndParseCode(IsTagRecoveryStopPoint);
if (!EndOfFile)
{
EnsureCurrent();
switch (CurrentSymbol.Type)
{
case HtmlSymbolType.SingleQuote:
case HtmlSymbolType.DoubleQuote:
ParseQuoted();
break;
case HtmlSymbolType.OpenAngle:
// Another "<" means this tag is invalid.
case HtmlSymbolType.Solidus:
// Empty tag
case HtmlSymbolType.CloseAngle:
// End of tag
return;
default:
AcceptAndMoveNext();
break;
}
}
}
}
private void ParseQuoted()
{
HtmlSymbolType type = CurrentSymbol.Type;
AcceptAndMoveNext();
ParseQuoted(type);
}
private void ParseQuoted(HtmlSymbolType type)
{
SkipToAndParseCode(type);
if (!EndOfFile)
{
Assert(type);
AcceptAndMoveNext();
}
}
private bool StartTag(Stack<Tuple<HtmlSymbol, SourceLocation>> tags)
{
// If we're at text, it's the name, otherwise the name is ""
HtmlSymbol tagName;
if (At(HtmlSymbolType.Text))
{
tagName = CurrentSymbol;
}
else
{
tagName = new HtmlSymbol(CurrentLocation, String.Empty, HtmlSymbolType.Unknown);
}
Tuple<HtmlSymbol, SourceLocation> tag = Tuple.Create(tagName, _lastTagStart);
if (tags.Count == 0 && String.Equals(tag.Item1.Content, SyntaxConstants.TextTagName, StringComparison.OrdinalIgnoreCase))
{
Output(SpanKind.Markup);
Span.CodeGenerator = SpanCodeGenerator.Null;
Accept(_bufferedOpenAngle);
Assert(HtmlSymbolType.Text);
AcceptAndMoveNext();
int bookmark = CurrentLocation.AbsoluteIndex;
IEnumerable<HtmlSymbol> tokens = ReadWhile(IsSpacingToken(includeNewLines: true));
bool empty = At(HtmlSymbolType.Solidus);
if (empty)
{
Accept(tokens);
Assert(HtmlSymbolType.Solidus);
AcceptAndMoveNext();
bookmark = CurrentLocation.AbsoluteIndex;
tokens = ReadWhile(IsSpacingToken(includeNewLines: true));
}
if (!Optional(HtmlSymbolType.CloseAngle))
{
Context.Source.Position = bookmark;
NextToken();
Context.OnError(tag.Item2, RazorResources.ParseError_TextTagCannotContainAttributes);
}
else
{
Accept(tokens);
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
}
if (!empty)
{
tags.Push(tag);
}
Output(SpanKind.Transition);
return true;
}
Accept(_bufferedOpenAngle);
Optional(HtmlSymbolType.Text);
return RestOfTag(tag, tags);
}
private bool RestOfTag(Tuple<HtmlSymbol, SourceLocation> tag, Stack<Tuple<HtmlSymbol, SourceLocation>> tags)
{
TagContent();
// We are now at a possible end of the tag
// Found '<', so we just abort this tag.
if (At(HtmlSymbolType.OpenAngle))
{
return false;
}
bool isEmpty = At(HtmlSymbolType.Solidus);
// Found a solidus, so don't accept it but DON'T push the tag to the stack
if (isEmpty)
{
AcceptAndMoveNext();
}
// Check for the '>' to determine if the tag is finished
bool seenClose = Optional(HtmlSymbolType.CloseAngle);
if (!seenClose)
{
Context.OnError(tag.Item2, RazorResources.ParseError_UnfinishedTag, tag.Item1.Content);
}
else
{
if (!isEmpty)
{
// Is this a void element?
string tagName = tag.Item1.Content.Trim();
if (VoidElements.Contains(tagName))
{
// Technically, void elements like "meta" are not allowed to have end tags. Just in case they do,
// we need to look ahead at the next set of tokens. If we see "<", "/", tag name, accept it and the ">" following it
// Place a bookmark
int bookmark = CurrentLocation.AbsoluteIndex;
// Skip whitespace
IEnumerable<HtmlSymbol> ws = ReadWhile(IsSpacingToken(includeNewLines: true));
// Open Angle
if (At(HtmlSymbolType.OpenAngle) && NextIs(HtmlSymbolType.Solidus))
{
HtmlSymbol openAngle = CurrentSymbol;
NextToken();
Assert(HtmlSymbolType.Solidus);
HtmlSymbol solidus = CurrentSymbol;
NextToken();
if (At(HtmlSymbolType.Text) && String.Equals(CurrentSymbol.Content, tagName, StringComparison.OrdinalIgnoreCase))
{
// Accept up to here
Accept(ws);
Accept(openAngle);
Accept(solidus);
AcceptAndMoveNext();
// Accept to '>', '<' or EOF
AcceptUntil(HtmlSymbolType.CloseAngle, HtmlSymbolType.OpenAngle);
// Accept the '>' if we saw it. And if we do see it, we're complete
return Optional(HtmlSymbolType.CloseAngle);
} // At(HtmlSymbolType.Text) && String.Equals(CurrentSymbol.Content, tagName, StringComparison.OrdinalIgnoreCase)
} // At(HtmlSymbolType.OpenAngle) && NextIs(HtmlSymbolType.Solidus)
// Go back to the bookmark and just finish this tag at the close angle
Context.Source.Position = bookmark;
NextToken();
}
else if (String.Equals(tagName, "script", StringComparison.OrdinalIgnoreCase))
{
SkipToEndScriptAndParseCode();
}
else
{
// Push the tag on to the stack
tags.Push(tag);
}
}
}
return seenClose;
}
private void SkipToEndScriptAndParseCode()
{
// Special case for <script>: Skip to end of script tag and parse code
bool seenEndScript = false;
while (!seenEndScript && !EndOfFile)
{
SkipToAndParseCode(HtmlSymbolType.OpenAngle);
SourceLocation tagStart = CurrentLocation;
AcceptAndMoveNext();
AcceptWhile(HtmlSymbolType.WhiteSpace);
if (Optional(HtmlSymbolType.Solidus))
{
AcceptWhile(HtmlSymbolType.WhiteSpace);
if (At(HtmlSymbolType.Text) && String.Equals(CurrentSymbol.Content, "script", StringComparison.OrdinalIgnoreCase))
{
// </script!
SkipToAndParseCode(HtmlSymbolType.CloseAngle);
if (!Optional(HtmlSymbolType.CloseAngle))
{
Context.OnError(tagStart, RazorResources.ParseError_UnfinishedTag, "script");
}
seenEndScript = true;
}
}
}
}
private bool AcceptUntilAll(params HtmlSymbolType[] endSequence)
{
while (!EndOfFile)
{
SkipToAndParseCode(endSequence[0]);
if (AcceptAll(endSequence))
{
return true;
}
}
Debug.Assert(EndOfFile);
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.Any;
return false;
}
private bool RemoveTag(Stack<Tuple<HtmlSymbol, SourceLocation>> tags, string tagName, SourceLocation tagStart)
{
Tuple<HtmlSymbol, SourceLocation> currentTag = null;
while (tags.Count > 0)
{
currentTag = tags.Pop();
if (String.Equals(tagName, currentTag.Item1.Content, StringComparison.OrdinalIgnoreCase))
{
// Matched the tag
return true;
}
}
if (currentTag != null)
{
Context.OnError(currentTag.Item2, RazorResources.ParseError_MissingEndTag, currentTag.Item1.Content);
}
else
{
Context.OnError(tagStart, RazorResources.ParseError_UnexpectedEndTag, tagName);
}
return false;
}
private void EndTagBlock(Stack<Tuple<HtmlSymbol, SourceLocation>> tags, bool complete)
{
if (tags.Count > 0)
{
// Ended because of EOF, not matching close tag. Throw error for last tag
while (tags.Count > 1)
{
tags.Pop();
}
Tuple<HtmlSymbol, SourceLocation> tag = tags.Pop();
Context.OnError(tag.Item2, RazorResources.ParseError_MissingEndTag, tag.Item1.Content);
}
else if (complete)
{
Span.EditHandler.AcceptedCharacters = AcceptedCharacters.None;
}
tags.Clear();
if (!Context.DesignTimeMode)
{
AcceptWhile(HtmlSymbolType.WhiteSpace);
if (!EndOfFile && CurrentSymbol.Type == HtmlSymbolType.NewLine)
{
AcceptAndMoveNext();
}
}
else if (Span.EditHandler.AcceptedCharacters == AcceptedCharacters.Any)
{
AcceptWhile(HtmlSymbolType.WhiteSpace);
Optional(HtmlSymbolType.NewLine);
}
PutCurrentBack();
if (!complete)
{
AddMarkerSymbolIfNecessary();
}
Output(SpanKind.Markup);
}
}
}
|