PageRenderTime 76ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Web.Razor.Test/Parser/Html/HtmlUrlAttributeTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 270 lines | 259 code | 11 blank | 0 comment | 0 complexity | 2893e4a01887233ec4241b361faea196 MD5 | raw file
  1. using System.Web.Razor.Editor;
  2. using System.Web.Razor.Generator;
  3. using System.Web.Razor.Parser;
  4. using System.Web.Razor.Parser.SyntaxTree;
  5. using System.Web.Razor.Test.Framework;
  6. using System.Web.Razor.Text;
  7. using Xunit;
  8. namespace System.Web.Razor.Test.Parser.Html
  9. {
  10. public class HtmlUrlAttributeTest : CsHtmlMarkupParserTestBase
  11. {
  12. [Fact]
  13. public void SimpleUrlInAttributeInMarkupBlock()
  14. {
  15. ParseBlockTest("<a href='~/Foo/Bar/Baz' />",
  16. new MarkupBlock(
  17. Factory.Markup("<a"),
  18. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 22, 0, 22)),
  19. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  20. Factory.Markup("~/Foo/Bar/Baz")
  21. .WithEditorHints(EditorHints.VirtualPath)
  22. .With(new LiteralAttributeCodeGenerator(
  23. new LocationTagged<string>(String.Empty, 9, 0, 9),
  24. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  25. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  26. Factory.Markup(" />").Accepts(AcceptedCharacters.None)));
  27. }
  28. [Fact]
  29. public void SimpleUrlInAttributeInMarkupDocument()
  30. {
  31. ParseDocumentTest("<a href='~/Foo/Bar/Baz' />",
  32. new MarkupBlock(
  33. Factory.Markup("<a"),
  34. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 22, 0, 22)),
  35. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  36. Factory.Markup("~/Foo/Bar/Baz")
  37. .WithEditorHints(EditorHints.VirtualPath)
  38. .With(new LiteralAttributeCodeGenerator(
  39. new LocationTagged<string>(String.Empty, 9, 0, 9),
  40. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  41. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  42. Factory.Markup(" />")));
  43. }
  44. [Fact]
  45. public void SimpleUrlInAttributeInMarkupSection()
  46. {
  47. ParseDocumentTest("@section Foo { <a href='~/Foo/Bar/Baz' /> }",
  48. new MarkupBlock(
  49. Factory.EmptyHtml(),
  50. new SectionBlock(new SectionCodeGenerator("Foo"),
  51. Factory.CodeTransition(),
  52. Factory.MetaCode("section Foo {")
  53. .AutoCompleteWith(null, atEndOfSpan: true)
  54. .Accepts(AcceptedCharacters.Any),
  55. new MarkupBlock(
  56. Factory.Markup(" <a"),
  57. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 17, 0, 17), new LocationTagged<string>("'", 37, 0, 37)),
  58. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  59. Factory.Markup("~/Foo/Bar/Baz")
  60. .WithEditorHints(EditorHints.VirtualPath)
  61. .With(new LiteralAttributeCodeGenerator(
  62. new LocationTagged<string>(String.Empty, 24, 0, 24),
  63. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 24, 0, 24))),
  64. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  65. Factory.Markup(" /> ")),
  66. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
  67. Factory.EmptyHtml()));
  68. }
  69. [Fact]
  70. public void UrlWithExpressionsInAttributeInMarkupBlock()
  71. {
  72. ParseBlockTest("<a href='~/Foo/@id/Baz' />",
  73. new MarkupBlock(
  74. Factory.Markup("<a"),
  75. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 22, 0, 22)),
  76. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  77. Factory.Markup("~/Foo/")
  78. .WithEditorHints(EditorHints.VirtualPath)
  79. .With(new LiteralAttributeCodeGenerator(
  80. new LocationTagged<string>(String.Empty, 9, 0, 9),
  81. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  82. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 15, 0, 15), 15, 0, 15),
  83. new ExpressionBlock(
  84. Factory.CodeTransition().Accepts(AcceptedCharacters.None),
  85. Factory.Code("id")
  86. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  87. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  88. Factory.Markup("/Baz")
  89. .With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 18, 0, 18), new LocationTagged<string>("/Baz", 18, 0, 18))),
  90. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  91. Factory.Markup(" />").Accepts(AcceptedCharacters.None)));
  92. }
  93. [Fact]
  94. public void UrlWithExpressionsInAttributeInMarkupDocument()
  95. {
  96. ParseDocumentTest("<a href='~/Foo/@id/Baz' />",
  97. new MarkupBlock(
  98. Factory.Markup("<a"),
  99. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 22, 0, 22)),
  100. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  101. Factory.Markup("~/Foo/")
  102. .WithEditorHints(EditorHints.VirtualPath)
  103. .With(new LiteralAttributeCodeGenerator(
  104. new LocationTagged<string>(String.Empty, 9, 0, 9),
  105. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  106. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 15, 0, 15), 15, 0, 15),
  107. new ExpressionBlock(
  108. Factory.CodeTransition().Accepts(AcceptedCharacters.None),
  109. Factory.Code("id")
  110. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  111. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  112. Factory.Markup("/Baz")
  113. .With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 18, 0, 18), new LocationTagged<string>("/Baz", 18, 0, 18))),
  114. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  115. Factory.Markup(" />")));
  116. }
  117. [Fact]
  118. public void UrlWithExpressionsInAttributeInMarkupSection()
  119. {
  120. ParseDocumentTest("@section Foo { <a href='~/Foo/@id/Baz' /> }",
  121. new MarkupBlock(
  122. Factory.EmptyHtml(),
  123. new SectionBlock(new SectionCodeGenerator("Foo"),
  124. Factory.CodeTransition(),
  125. Factory.MetaCode("section Foo {")
  126. .AutoCompleteWith(null, atEndOfSpan: true),
  127. new MarkupBlock(
  128. Factory.Markup(" <a"),
  129. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 17, 0, 17), new LocationTagged<string>("'", 37, 0, 37)),
  130. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  131. Factory.Markup("~/Foo/")
  132. .WithEditorHints(EditorHints.VirtualPath)
  133. .With(new LiteralAttributeCodeGenerator(
  134. new LocationTagged<string>(String.Empty, 24, 0, 24),
  135. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 24, 0, 24))),
  136. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 30, 0, 30), 30, 0, 30),
  137. new ExpressionBlock(
  138. Factory.CodeTransition().Accepts(AcceptedCharacters.None),
  139. Factory.Code("id")
  140. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  141. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  142. Factory.Markup("/Baz")
  143. .With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 33, 0, 33), new LocationTagged<string>("/Baz", 33, 0, 33))),
  144. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  145. Factory.Markup(" /> ")),
  146. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
  147. Factory.EmptyHtml()));
  148. }
  149. [Fact]
  150. public void UrlWithComplexCharactersInAttributeInMarkupBlock()
  151. {
  152. ParseBlockTest("<a href='~/Foo+Bar:Baz(Biz),Boz' />",
  153. new MarkupBlock(
  154. Factory.Markup("<a"),
  155. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 31, 0, 31)),
  156. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  157. Factory.Markup("~/Foo+Bar:Baz(Biz),Boz")
  158. .WithEditorHints(EditorHints.VirtualPath)
  159. .With(new LiteralAttributeCodeGenerator(
  160. new LocationTagged<string>(String.Empty, 9, 0, 9),
  161. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  162. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  163. Factory.Markup(" />").Accepts(AcceptedCharacters.None)));
  164. }
  165. [Fact]
  166. public void UrlWithComplexCharactersInAttributeInMarkupDocument()
  167. {
  168. ParseDocumentTest("<a href='~/Foo+Bar:Baz(Biz),Boz' />",
  169. new MarkupBlock(
  170. Factory.Markup("<a"),
  171. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href='", 2, 0, 2), new LocationTagged<string>("'", 31, 0, 31)),
  172. Factory.Markup(" href='").With(SpanCodeGenerator.Null),
  173. Factory.Markup("~/Foo+Bar:Baz(Biz),Boz")
  174. .WithEditorHints(EditorHints.VirtualPath)
  175. .With(new LiteralAttributeCodeGenerator(
  176. new LocationTagged<string>(String.Empty, 9, 0, 9),
  177. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 9, 0, 9))),
  178. Factory.Markup("'").With(SpanCodeGenerator.Null)),
  179. Factory.Markup(" />")));
  180. }
  181. [Fact]
  182. public void UrlInUnquotedAttributeValueInMarkupBlock()
  183. {
  184. ParseBlockTest("<a href=~/Foo+Bar:Baz(Biz),Boz/@id/Boz />",
  185. new MarkupBlock(
  186. Factory.Markup("<a"),
  187. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href=", 2, 0, 2), new LocationTagged<string>(String.Empty, 38, 0, 38)),
  188. Factory.Markup(" href=").With(SpanCodeGenerator.Null),
  189. Factory.Markup("~/Foo+Bar:Baz(Biz),Boz/")
  190. .WithEditorHints(EditorHints.VirtualPath)
  191. .With(new LiteralAttributeCodeGenerator(
  192. new LocationTagged<string>(String.Empty, 8, 0, 8),
  193. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 8, 0, 8))),
  194. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 31, 0, 31), 31, 0, 31),
  195. new ExpressionBlock(
  196. Factory.CodeTransition()
  197. .Accepts(AcceptedCharacters.None),
  198. Factory.Code("id")
  199. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  200. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  201. Factory.Markup("/Boz").With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 34, 0, 34), new LocationTagged<string>("/Boz", 34, 0, 34)))),
  202. Factory.Markup(" />").Accepts(AcceptedCharacters.None)));
  203. }
  204. [Fact]
  205. public void UrlInUnquotedAttributeValueInMarkupDocument()
  206. {
  207. ParseDocumentTest("<a href=~/Foo+Bar:Baz(Biz),Boz/@id/Boz />",
  208. new MarkupBlock(
  209. Factory.Markup("<a"),
  210. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href=", 2, 0, 2), new LocationTagged<string>(String.Empty, 38, 0, 38)),
  211. Factory.Markup(" href=").With(SpanCodeGenerator.Null),
  212. Factory.Markup("~/Foo+Bar:Baz(Biz),Boz/")
  213. .WithEditorHints(EditorHints.VirtualPath)
  214. .With(new LiteralAttributeCodeGenerator(
  215. new LocationTagged<string>(String.Empty, 8, 0, 8),
  216. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 8, 0, 8))),
  217. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 31, 0, 31), 31, 0, 31),
  218. new ExpressionBlock(
  219. Factory.CodeTransition()
  220. .Accepts(AcceptedCharacters.None),
  221. Factory.Code("id")
  222. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  223. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  224. Factory.Markup("/Boz").With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 34, 0, 34), new LocationTagged<string>("/Boz", 34, 0, 34)))),
  225. Factory.Markup(" />")));
  226. }
  227. [Fact]
  228. public void UrlInUnquotedAttributeValueInMarkupSection()
  229. {
  230. ParseDocumentTest("@section Foo { <a href=~/Foo+Bar:Baz(Biz),Boz/@id/Boz /> }",
  231. new MarkupBlock(
  232. Factory.EmptyHtml(),
  233. new SectionBlock(new SectionCodeGenerator("Foo"),
  234. Factory.CodeTransition(),
  235. Factory.MetaCode("section Foo {")
  236. .AutoCompleteWith(null, atEndOfSpan: true),
  237. new MarkupBlock(
  238. Factory.Markup(" <a"),
  239. new MarkupBlock(new AttributeBlockCodeGenerator("href", new LocationTagged<string>(" href=", 17, 0, 17), new LocationTagged<string>(String.Empty, 53, 0, 53)),
  240. Factory.Markup(" href=").With(SpanCodeGenerator.Null),
  241. Factory.Markup("~/Foo+Bar:Baz(Biz),Boz/")
  242. .WithEditorHints(EditorHints.VirtualPath)
  243. .With(new LiteralAttributeCodeGenerator(
  244. new LocationTagged<string>(String.Empty, 23, 0, 23),
  245. new LocationTagged<SpanCodeGenerator>(new ResolveUrlCodeGenerator(), 23, 0, 23))),
  246. new MarkupBlock(new DynamicAttributeBlockCodeGenerator(new LocationTagged<string>(String.Empty, 46, 0, 46), 46, 0, 46),
  247. new ExpressionBlock(
  248. Factory.CodeTransition()
  249. .Accepts(AcceptedCharacters.None),
  250. Factory.Code("id")
  251. .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
  252. .Accepts(AcceptedCharacters.NonWhiteSpace))),
  253. Factory.Markup("/Boz").With(new LiteralAttributeCodeGenerator(new LocationTagged<string>(String.Empty, 49, 0, 49), new LocationTagged<string>("/Boz", 49, 0, 49)))),
  254. Factory.Markup(" /> ")),
  255. Factory.MetaCode("}").Accepts(AcceptedCharacters.None)),
  256. Factory.EmptyHtml()));
  257. }
  258. }
  259. }