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

Parse the whitespace surrounding equals in attribute correctly #503

Merged
merged 1 commit into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ private void BeforeAttribute()
// 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>();
var whitespaceAfterAttributeName = Enumerable.Empty<HtmlSymbol>();
if (At(HtmlSymbolType.Text))
{
name = ReadWhile(sym =>
Expand All @@ -472,6 +473,10 @@ private void BeforeAttribute()
sym.Type != HtmlSymbolType.CloseAngle &&
sym.Type != HtmlSymbolType.OpenAngle &&
(sym.Type != HtmlSymbolType.ForwardSlash || !NextIs(HtmlSymbolType.CloseAngle)));

// capture whitespace after attribute name (if any)
whitespaceAfterAttributeName = ReadWhile(
sym => sym.Type == HtmlSymbolType.WhiteSpace || sym.Type == HtmlSymbolType.NewLine);
}
else
{
Expand All @@ -485,6 +490,10 @@ private void BeforeAttribute()
{
// Minimized attribute

// We are at the prefix of the next attribute or the end of tag. Put it back so it is parsed later.
PutCurrentBack();
PutBack(whitespaceAfterAttributeName);

// Output anything prior to the attribute, in most cases this will be the tag name:
// |<input| checked />. If in-between other attributes this will noop or output malformed attribute
// content (if the previous attribute was malformed).
Expand All @@ -507,11 +516,14 @@ private void BeforeAttribute()
// Start a new markup block for the attribute
using (Context.StartBlock(BlockType.Markup))
{
AttributePrefix(whitespace, name);
AttributePrefix(whitespace, name, whitespaceAfterAttributeName);
}
}

private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<HtmlSymbol> nameSymbols)
private void AttributePrefix(
IEnumerable<HtmlSymbol> whitespace,
IEnumerable<HtmlSymbol> nameSymbols,
IEnumerable<HtmlSymbol> whitespaceAfterAttributeName)
{
// First, determine if this is a 'data-' attribute (since those can't use conditional attributes)
var name = nameSymbols.GetContent(Span.Start);
Expand All @@ -520,14 +532,27 @@ private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<Htm
// Accept the whitespace and name
Accept(whitespace);
Accept(nameSymbols);

// Since this is not a minimized attribute, the whitespace after attribute name belongs to this attribute.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate this comment from preceding lines with a blank line.

Accept(whitespaceAfterAttributeName);
Assert(HtmlSymbolType.Equals); // We should be at "="
AcceptAndMoveNext();

var whitespaceAfterEquals = ReadWhile(sym => sym.Type == HtmlSymbolType.WhiteSpace || sym.Type == HtmlSymbolType.NewLine);
var quote = HtmlSymbolType.Unknown;
if (At(HtmlSymbolType.SingleQuote) || At(HtmlSymbolType.DoubleQuote))
{
// Found a quote, the whitespace belongs to this attribute.
Accept(whitespaceAfterEquals);
quote = CurrentSymbol.Type;
AcceptAndMoveNext();
}
else if (whitespaceAfterEquals.Any())
{
// No quotes found after the whitespace. Put it back so that it can be parsed later.
PutCurrentBack();
PutBack(whitespaceAfterEquals);
}

// We now have the prefix: (i.e. ' foo="')
var prefix = Span.GetContent();
Expand All @@ -537,10 +562,15 @@ private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<Htm
Span.ChunkGenerator = SpanChunkGenerator.Null; // The block chunk generator will render the prefix
Output(SpanKind.Markup);

// Read the values
while (!EndOfFile && !IsEndOfAttributeValue(quote, CurrentSymbol))
// Read the attribute value only if the value is quoted
// or if there is no whitespace between '=' and the unquoted value.
if (quote != HtmlSymbolType.Unknown || !whitespaceAfterEquals.Any())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens now with <input name=@value /> ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition will return true. No change in the behavior.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
AttributeValue(quote);
// Read the attribute value.
while (!EndOfFile && !IsEndOfAttributeValue(quote, CurrentSymbol))
{
AttributeValue(quote);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Values" for a single attribute i.e. name / value pair? Do you mean "symbols in the value"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single attribute value. This will run for every part of the attribute value. abc="foo bar baz" will run thrice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the comment so that it is clear. Wasn't sure what you meant.

}

// Capture the suffix
Expand All @@ -567,6 +597,11 @@ private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<Htm
// Output the attribute name, the equals and optional quote. Ex: foo="
Output(SpanKind.Markup);

if (quote == HtmlSymbolType.Unknown && whitespaceAfterEquals.Any())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What text input makes this evaluate to true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<p data-foo= blah />

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would this evaluate to false? You're in an else block for quote != HtmlSymbolType.Unknown || !whitespaceAfterEquals.Any(). Do a bit of Boolean algebra on !(A != B || !C) and you get A == B && C.

Looks line line 594 is unreachable. What is lost if that code is never evaluated?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. @dougbu you may be misreading the diff. This is the else of if (attributeCanBeConditional)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NTaylorMullen yup, thought I expanded everything in between but noooo...

{
return;
}

// Not a "conditional" attribute, so just read the value
SkipToAndParseCode(sym => IsEndOfAttributeValue(quote, sym));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,32 @@ private static TryParseResult TryParseSpan(
// The goal here is to consume the equal sign and the optional single/double-quote.

// The coming symbols will either be a quote or value (in the case that the value is unquoted).
// Spaces after/before the equal symbol are not yet supported:
// https://github.com/aspnet/Razor/issues/123

// TODO: Handle malformed tags, if there's an '=' then there MUST be a value.
// https://github.com/aspnet/Razor/issues/104

SourceLocation symbolStartLocation;

// Skip the whitespace preceding the start of the attribute value.
var valueStartIndex = i + 1; // Start from the symbol after '='.
while (valueStartIndex < htmlSymbols.Length &&
(htmlSymbols[valueStartIndex].Type == HtmlSymbolType.WhiteSpace ||
htmlSymbols[valueStartIndex].Type == HtmlSymbolType.NewLine))
{
valueStartIndex++;
}

// Check for attribute start values, aka single or double quote
if ((i + 1) < htmlSymbols.Length && IsQuote(htmlSymbols[i + 1]))
if (valueStartIndex < htmlSymbols.Length && IsQuote(htmlSymbols[valueStartIndex]))
{
// Move past the attribute start so we can accept the true value.
i++;
symbolStartLocation = htmlSymbols[i].Start;
valueStartIndex++;
symbolStartLocation = htmlSymbols[valueStartIndex].Start;

// If there's a start quote then there must be an end quote to be valid, skip it.
symbolOffset = 1;

i = valueStartIndex - 1;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,32 +848,36 @@ public static TheoryData DesignTimeTagHelperTestData
DefaultPAndInputTagHelperDescriptors,
new List<LineMapping>
{
BuildLineMapping(documentAbsoluteIndex: 14,
documentLineIndex: 0,
generatedAbsoluteIndex: 493,
generatedLineIndex: 15,
characterOffsetIndex: 14,
contentLength: 11),
BuildLineMapping(documentAbsoluteIndex: 62,
documentLineIndex: 3,
documentCharacterOffsetIndex: 26,
generatedAbsoluteIndex: 1289,
generatedLineIndex: 39,
generatedCharacterOffsetIndex: 28,
contentLength: 0),
BuildLineMapping(documentAbsoluteIndex: 122,
documentLineIndex: 5,
generatedAbsoluteIndex: 1634,
generatedLineIndex: 48,
characterOffsetIndex: 30,
contentLength: 0),
BuildLineMapping(documentAbsoluteIndex: 88,
documentLineIndex: 4,
documentCharacterOffsetIndex: 12,
generatedAbsoluteIndex: 1789,
generatedLineIndex: 54,
generatedCharacterOffsetIndex: 19,
contentLength: 0)
BuildLineMapping(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did these change at all or did you just format it? Want to make sure I'm not missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just formatting.

documentAbsoluteIndex: 14,
documentLineIndex: 0,
generatedAbsoluteIndex: 493,
generatedLineIndex: 15,
characterOffsetIndex: 14,
contentLength: 11),
BuildLineMapping(
documentAbsoluteIndex: 63,
documentLineIndex: 3,
documentCharacterOffsetIndex: 27,
generatedAbsoluteIndex: 1289,
generatedLineIndex: 39,
generatedCharacterOffsetIndex: 28,
contentLength: 0),
BuildLineMapping(
documentAbsoluteIndex: 122,
documentLineIndex: 5,
generatedAbsoluteIndex: 1634,
generatedLineIndex: 48,
characterOffsetIndex: 30,
contentLength: 0),
BuildLineMapping(
documentAbsoluteIndex: 89,
documentLineIndex: 4,
documentCharacterOffsetIndex: 13,
generatedAbsoluteIndex: 1789,
generatedLineIndex: 54,
generatedCharacterOffsetIndex: 19,
contentLength: 0),
}
},
{
Expand Down Expand Up @@ -1484,6 +1488,7 @@ public static TheoryData RuntimeTimeTagHelperTestData
{
{ "SingleTagHelper", null, DefaultPAndInputTagHelperDescriptors },
{ "SingleTagHelperWithNewlineBeforeAttributes", null, DefaultPAndInputTagHelperDescriptors },
{ "TagHelpersWithWeirdlySpacedAttributes", null, DefaultPAndInputTagHelperDescriptors },
{ "BasicTagHelpers", null, DefaultPAndInputTagHelperDescriptors },
{ "BasicTagHelpers.RemoveTagHelper", null, DefaultPAndInputTagHelperDescriptors },
{ "BasicTagHelpers.Prefixed", null, PrefixedPAndInputTagHelperDescriptors },
Expand Down
Loading