Skip to content
Browse files

Do not generate html folds in ASP.NET for elements that exist on a si…

…ngle line.
  • Loading branch information...
1 parent 8de91e4 commit 9f38b13c20bcafcbf4cd891a91614a3065a4e466 @mrward mrward committed Dec 28, 2011
View
5 src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/Folding/CharacterReader.cs
@@ -100,5 +100,10 @@ public bool IsNextCharacterPercentSign()
{
return reader.Peek() == '%';
}
+
+ public bool IsLineFeed()
+ {
+ return CurrentCharacter == '\n';
+ }
}
}
View
6 src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/Folding/HtmlElementFold.cs
@@ -10,10 +10,6 @@ public class HtmlElementFold : NewFolding
{
string elementName = String.Empty;
- public HtmlElementFold()
- {
- }
-
public string ElementName {
get { return elementName; }
set {
@@ -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;
View
12 src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/Folding/HtmlFoldParser.cs
@@ -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);
}
@@ -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));
View
13 src/AddIns/BackendBindings/AspNet.Mvc/Project/Src/Folding/HtmlReader.cs
@@ -24,6 +24,7 @@ public HtmlReader(TextReader reader)
public HtmlReader(CharacterReader reader)
{
this.reader = reader;
+ this.Line = 1;
}
public string Value {
@@ -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; }
@@ -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()
View
1 src/AddIns/BackendBindings/AspNet.Mvc/Test/AspNet.Mvc.Tests.csproj
@@ -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" />
View
47 src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/Folding/HtmlReaderTests.cs
@@ -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);
+ }
+ }
+}
View
5 src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/Folding/RazorHtmlFoldParserTests.cs
@@ -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);
@@ -66,7 +67,7 @@ public void GetFolds_EndAnchorTagInsideIfStatement_ReturnsOneFoldForParagraphTag
new HtmlElementFold() {
ElementName = "p",
StartOffset = 34,
- EndOffset = 41
+ EndOffset = 47
}
};
View
9 src/AddIns/BackendBindings/AspNet.Mvc/Test/Src/Folding/WebFormsHtmlFoldParserTests.cs
@@ -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.
Something went wrong with that request. Please try again.