Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Modify parser to group html begin/end elements.
Browse files Browse the repository at this point in the history
- Added a "Tag" block type.
- Wrapped all begin/end elements in a "Tag" Markup block.

#75
  • Loading branch information
NTaylorMullen committed Aug 26, 2014
1 parent ff944e5 commit 6114d5d
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override string GetSample(HtmlSymbolType type)
return "<";
case HtmlSymbolType.Bang:
return "!";
case HtmlSymbolType.Solidus:
case HtmlSymbolType.ForwardSlash:
return "/";
case HtmlSymbolType.QuestionMark:
return "?";
Expand Down
262 changes: 197 additions & 65 deletions src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs

Large diffs are not rendered by default.

64 changes: 46 additions & 18 deletions src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,65 @@ public override void ParseDocument()
/// Reads the content of a tag (if present) in the MarkupDocument (or MarkupSection) context,
/// where we don't care about maintaining a stack of tags.
/// </summary>
/// <returns>A boolean indicating if we scanned at least one tag.</returns>
private bool ScanTagInDocumentContext()
private void ScanTagInDocumentContext()
{
if (Optional(HtmlSymbolType.OpenAngle))
if (At(HtmlSymbolType.OpenAngle))
{
if (At(HtmlSymbolType.Bang))
if (NextIs(HtmlSymbolType.Bang))
{
AcceptAndMoveNext(); // Accept '<'
BangTag();
return true;
}
else if (At(HtmlSymbolType.QuestionMark))
else if (NextIs(HtmlSymbolType.QuestionMark))
{
AcceptAndMoveNext(); // Accept '<'
XmlPI();
return true;
}
else if (!At(HtmlSymbolType.Solidus))
else
{
bool scriptTag = At(HtmlSymbolType.Text) &&
String.Equals(CurrentSymbol.Content, "script", StringComparison.OrdinalIgnoreCase);
Optional(HtmlSymbolType.Text);
TagContent(); // Parse the tag, don't care about the content
Optional(HtmlSymbolType.Solidus);
Optional(HtmlSymbolType.CloseAngle);
if (scriptTag)
Output(SpanKind.Markup);

// Start tag block
var tagBlock = Context.StartBlock(BlockType.Tag);

AcceptAndMoveNext(); // Accept '<'

if (!At(HtmlSymbolType.ForwardSlash))
{
SkipToEndScriptAndParseCode();
// Parsing a start tag
var scriptTag = At(HtmlSymbolType.Text) &&
string.Equals(CurrentSymbol.Content, "script", StringComparison.OrdinalIgnoreCase);
Optional(HtmlSymbolType.Text);
TagContent(); // Parse the tag, don't care about the content
Optional(HtmlSymbolType.ForwardSlash);
Optional(HtmlSymbolType.CloseAngle);

if (scriptTag)
{
Output(SpanKind.Markup);
tagBlock.Dispose();

SkipToEndScriptAndParseCode();
return;
}
}
return true;
else
{
// Parsing an end tag
// This section can accept things like: '</p >' or '</p>' etc.
Optional(HtmlSymbolType.ForwardSlash);
// Whitespace here is invalid (according to the spec)
Optional(HtmlSymbolType.Text);
AcceptAll(HtmlSymbolType.WhiteSpace);
Optional(HtmlSymbolType.CloseAngle);
}

Output(SpanKind.Markup);

// End tag block
tagBlock.Dispose();
}
}
return false;
}
}
}
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Razor/Parser/SyntaxTree/BlockType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum BlockType
Template,

// Special
Comment
Comment,
Tag
}
}
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Razor/Tokenizer/HtmlTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private HtmlSymbol Symbol()
case '!':
return EndSymbol(HtmlSymbolType.Bang);
case '/':
return EndSymbol(HtmlSymbolType.Solidus);
return EndSymbol(HtmlSymbolType.ForwardSlash);
case '?':
return EndSymbol(HtmlSymbolType.QuestionMark);
case '[':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum HtmlSymbolType
NewLine, // Newline
OpenAngle, // <
Bang, // !
Solidus, // /
ForwardSlash, // /
QuestionMark, // ?
DoubleHyphen, // --
LeftBracket, // [
Expand Down
11 changes: 3 additions & 8 deletions src/Microsoft.AspNet.Razor/Utils/DisposableAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.AspNet.Razor.Utils
internal class DisposableAction : IDisposable
{
private Action _action;
private bool _invoked;

public DisposableAction(Action action)
{
Expand All @@ -20,16 +21,10 @@ public DisposableAction(Action action)

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
// If we were disposed by the finalizer it's because the user didn't use a "using" block, so don't do anything!
if (disposing)
if (!_invoked)
{
_action();
_invoked = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void Bang_Is_Recognized()
[Fact]
public void Solidus_Is_Recognized()
{
TestSingleToken("/", HtmlSymbolType.Solidus);
TestSingleToken("/", HtmlSymbolType.ForwardSlash);
}

[Fact]
Expand Down

0 comments on commit 6114d5d

Please sign in to comment.