Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Do not generate html folds in ASP.NET for elements that exist on a si…
Browse files Browse the repository at this point in the history
…ngle line.
  • Loading branch information
mrward committed Dec 28, 2011
1 parent 8de91e4 commit 9f38b13
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
Expand Up @@ -100,5 +100,10 @@ public bool IsNextCharacterPercentSign()
{
return reader.Peek() == '%';
}

public bool IsLineFeed()
{
return CurrentCharacter == '\n';
}
}
}
Expand Up @@ -10,10 +10,6 @@ public class HtmlElementFold : NewFolding
{
string elementName = String.Empty;

public HtmlElementFold()
{
}

public string ElementName {
get { return elementName; }
set {
Expand All @@ -27,6 +23,8 @@ void UpdateFoldName()
Name = String.Format("<{0}>", elementName);
}

public int Line { get; set; }

public override bool Equals(object obj)
{
var rhs = obj as HtmlElementFold;
Expand Down
Expand Up @@ -51,7 +51,8 @@ void SaveFoldStartOnStack()
{
var fold = new HtmlElementFold() {
ElementName = htmlReader.Value,
StartOffset = htmlReader.Offset
StartOffset = htmlReader.Offset,
Line = htmlReader.Line
};
foldStack.Push(fold);
}
Expand All @@ -62,13 +63,20 @@ void AddFoldForCompletedElement()
var fold = foldStack.Pop();
if (fold.ElementName == htmlReader.Value) {
fold.EndOffset = htmlReader.EndOffset;
folds.Add(fold);
AddFoldIfEndElementOnDifferentLineToStartElement(fold);
} else {
AddFoldForCompletedElement();
}
}
}

void AddFoldIfEndElementOnDifferentLineToStartElement(HtmlElementFold fold)
{
if (htmlReader.Line > fold.Line) {
folds.Add(fold);
}
}

void SortFoldsByStartOffset()
{
folds.Sort((fold1, fold2) => fold1.StartOffset.CompareTo(fold2.StartOffset));
Expand Down
Expand Up @@ -24,6 +24,7 @@ public HtmlReader(TextReader reader)
public HtmlReader(CharacterReader reader)
{
this.reader = reader;
this.Line = 1;
}

public string Value {
Expand All @@ -32,6 +33,7 @@ public HtmlReader(CharacterReader reader)

public int Offset { get; private set; }
public int Length { get; private set; }
public int Line { get; private set; }

public int EndOffset {
get { return Offset + Length; }
Expand Down Expand Up @@ -77,7 +79,16 @@ protected virtual bool IsHtml()

bool ReadNextCharacter()
{
return reader.Read();
bool result = reader.Read();
UpdateLineCountIfNewLineCharacterRead();
return result;
}

void UpdateLineCountIfNewLineCharacterRead()
{
if (reader.IsLineFeed()) {
Line++;
}
}

bool IsElementStartCharacter()
Expand Down
Expand Up @@ -113,6 +113,7 @@
<Compile Include="Src\CodeTemplates\RazorCSharpEmptyViewTemplateTests.cs" />
<Compile Include="Src\CodeTemplates\RazorCSharpListViewTemplateTests.cs" />
<Compile Include="Src\Folding\CharacterReaderTests.cs" />
<Compile Include="Src\Folding\HtmlReaderTests.cs" />
<Compile Include="Src\Folding\RazorHtmlFoldParserTests.cs" />
<Compile Include="Src\Folding\HtmlElementFoldTests.cs" />
<Compile Include="Src\Folding\RazorHtmlReaderTests.cs" />
Expand Down
@@ -0,0 +1,47 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using ICSharpCode.AspNet.Mvc.Folding;
using NUnit.Framework;

namespace AspNet.Mvc.Tests.Folding
{
[TestFixture]
public class HtmlReaderTests
{
HtmlReader reader;

void CreateReader(string html)
{
reader = new HtmlReader(html);
}

[Test]
public void Line_ReadParagraphStartTagOnFirstLine_ReturnsOne()
{
CreateReader("<p>");
reader.Read();

int line = reader.Line;

Assert.AreEqual(1, line);
}

[Test]
public void Line_ReadParagraphEndTagOnSecondLine_ReturnsTwo()
{
string html =
"<p>\r\n" +
"</p>\r\n";

CreateReader(html);
reader.Read();
reader.Read();

int line = reader.Line;

Assert.AreEqual(2, line);
}
}
}
Expand Up @@ -57,7 +57,8 @@ public void GetFolds_EndAnchorTagInsideIfStatement_ReturnsOneFoldForParagraphTag
string text =
"@if (i<a || b>i) {\r\n" +
" </a>\r\n" +
" <p></p>\r\n" +
" <p>\r\n" +
" </p>\r\n" +
"}\r\n";

GetFolds(text);
Expand All @@ -66,7 +67,7 @@ public void GetFolds_EndAnchorTagInsideIfStatement_ReturnsOneFoldForParagraphTag
new HtmlElementFold() {
ElementName = "p",
StartOffset = 34,
EndOffset = 41
EndOffset = 47
}
};

Expand Down
Expand Up @@ -177,5 +177,14 @@ public void GetFolds_ScriptTagAttributeHasAspxTagsInsideWithDoubleQuotes_Returns

CollectionAssert.AreEqual(expectedFolds, folds);
}

[Test]
public void GetFolds_ScriptStartAndEndTagsOnSameLine_ReturnsNoFolds()
{
CreateParser();
GetFolds("<script></script>");

Assert.AreEqual(0, folds.Count);
}
}
}

0 comments on commit 9f38b13

Please sign in to comment.