/// <summary> /// Initialises a new instance of <see cref="ContentWidgetResult"/>. /// </summary> /// <param name="encodedContent"></param> public ContentWidgetResult(HtmlString encodedContent) { if (encodedContent == null) { throw new ArgumentNullException(nameof(encodedContent)); } EncodedContent = encodedContent; Content = WebUtility.HtmlDecode(encodedContent.ToString()); }
public void FromEncodedText_DoesNotEncodeOnWrite() { // Arrange var expectedText = "Hello"; // Act var content = new HtmlString(expectedText); // Assert Assert.Equal(expectedText, content.ToString()); }
/// <inheritdoc /> public IHtmlContentBuilder AppendHtml(string encoded) { if (encoded == null) { return this; } var value = new HtmlString(encoded); AppendValue(new ViewBufferValue(value)); return this; }
public void ToString_ReturnsText() { // Arrange var expectedText = "Hello"; var content = new HtmlString(expectedText); // Act var result = content.ToString(); // Assert Assert.Equal(expectedText, result); }
private HtmlString GetContent() { var sb = new StringBuilder(); sb.Append("<div>"); for (int i = 1; i < 6; i++) { sb.Append(i <= Rating ? GetStarDiv("rating-full") : GetStarDiv("rating-empty")); } sb.Append("</div>"); var html = new HtmlString(sb.ToString()); return html; }
public void WriteTo_WritesToTheSpecifiedWriter() { // Arrange var expectedText = "Some Text"; var content = new HtmlString(expectedText); var writer = new StringWriter(); // Act content.WriteTo(writer, new CommonTestEncoder()); // Assert Assert.Equal(expectedText, writer.ToString()); writer.Dispose(); }
public void Append_AddsHtmlContentRazorValue() { // Arrange var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name"); var content = new HtmlString("hello-world"); // Act buffer.AppendHtml(content); // Assert var segment = Assert.Single(buffer.BufferSegments); Assert.Equal(1, buffer.CurrentCount); Assert.Same(content, segment[0].Value); }
public void Write_Object_HtmlContent_AddsToEntries() { // Arrange var buffer = new TestHtmlContentBuilder(); var writer = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8); var content = new HtmlString("Hello, world!"); // Act writer.Write((object)content); // Assert Assert.Collection( buffer.Values, item => Assert.Same(content, item)); }
public static HtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag); } //return MvcHtmlString.Create(result.ToString()); var pageLinks = new HtmlString(result.ToString()); return pageLinks; }
public void PartialWithModel_InvokesPartialAsyncWithPassedInModel() { // Arrange var expected = new HtmlString("value"); var model = new object(); var helper = new Mock<IHtmlHelper>(MockBehavior.Strict); helper.Setup(h => h.PartialAsync("test", model, null)) .Returns(Task.FromResult((IHtmlContent)expected)) .Verifiable(); // Act var actual = helper.Object.Partial("test", model); // Assert Assert.Same(expected, actual); helper.Verify(); }
public void Partial_InvokesPartialAsyncWithCurrentModel() { // Arrange var expected = new HtmlString("value"); var model = new object(); var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider()) { Model = model }; var helper = new Mock<IHtmlHelper>(MockBehavior.Strict); helper.Setup(h => h.PartialAsync("test", model, null)) .Returns(Task.FromResult((IHtmlContent)expected)) .Verifiable(); helper.SetupGet(h => h.ViewData) .Returns(viewData); // Act var actual = helper.Object.Partial("test"); // Assert Assert.Same(expected, actual); helper.Verify(); }
public void Write_Object_HtmlContent_AddsToEntries() { // Arrange var writer = new StringCollectionTextWriter(Encoding.UTF8); var content = new HtmlString("Hello, world!"); // Act writer.Write((object)content); // Assert Assert.Collection( writer.Entries, item => Assert.Same(content, item)); }
public void Write_HtmlContent_AfterFlush_GoesToStream() { // Arrange var stringWriter = new StringWriter(); var writer = new RazorTextWriter(stringWriter, Encoding.UTF8, new CommonTestEncoder()); writer.Flush(); var content = new HtmlString("Hello, world!"); // Act writer.Write(content); // Assert Assert.Equal("Hello, world!", stringWriter.ToString()); }
public void WriteLine_Object_HtmlContent_AddsToEntries() { // Arrange var writer = new RazorTextWriter(TextWriter.Null, Encoding.UTF8, new CommonTestEncoder()); var content = new HtmlString("Hello, world!"); // Act writer.WriteLine(content); // Assert Assert.Collection( writer.BufferedWriter.Entries, item => Assert.Same(content, item), item => Assert.Equal(Environment.NewLine, item)); }
public void Write_HtmlContent_AfterFlush_GoesToStream() { // Arrange var stringWriter = new StringWriter(); var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name"); var writer = new RazorTextWriter(stringWriter, buffer, new HtmlTestEncoder()); writer.Flush(); var content = new HtmlString("Hello, world!"); // Act writer.Write(content); // Assert Assert.Equal("Hello, world!", stringWriter.ToString()); }
private void Verify(HtmlString html, string src, string alt) { var doc = new XmlDocument(); doc.LoadXml(html.ToString()); Assert.Equal(1, doc.ChildNodes.Count); var node = doc.ChildNodes.Item(0); Assert.Equal("img", node.Name); var srcNode = node.Attributes.GetNamedItem("src"); Assert.NotNull(srcNode); Assert.Equal("src", srcNode.Name); Assert.Equal(src, srcNode.Value); var altNode = node.Attributes.GetNamedItem("alt"); if (!string.IsNullOrWhiteSpace(alt)) { Assert.NotNull(altNode); Assert.Equal("alt", altNode.Name); Assert.Equal(alt, altNode.Value); } else { Assert.Null(altNode); } }
private void VerifyContentLinks(HtmlString html, string path, ContentLinkType contentType, ILookup<string, string> lookup) { var xmlDoc = new XmlDocument(); var doc = xmlDoc.CreateDocumentFragment(); doc.InnerXml = html.ToString(); var childNodes = doc.ChildNodes.Cast<XmlNode>().Where(n => !(n is XmlWhitespace)).ToList(); var expectedList = lookup[path]; Assert.Equal(expectedList.Count(), childNodes.Count); foreach (var pair in childNodes.Zip(expectedList, Tuple.Create)) { var node = pair.Item1; var expected = pair.Item2; if (contentType == ContentLinkType.Javascript) { Assert.Equal("script", node.Name); Assert.Equal(2, node.Attributes.Count); Assert.Equal("text/javascript", node.Attributes.GetNamedItem("type").Value); Assert.Equal(expected, node.Attributes.GetNamedItem("src").Value); } else { Assert.Equal("link", node.Name); Assert.Equal(2, node.Attributes.Count); Assert.Equal("stylesheet", node.Attributes.GetNamedItem("rel").Value); Assert.Equal(expected, node.Attributes.GetNamedItem("href").Value); } } }
/// <summary> /// Initialises a new instance of <see cref="ContentWidgetResult"/>. /// </summary> /// <param name="content">The raw content.</param> public ContentWidgetResult(string content) { Content = content ?? string.Empty; EncodedContent = new HtmlString(WebUtility.HtmlEncode(Content)); }
public void PartialWithViewDataAndModel_InvokesPartialAsyncWithPassedInViewDataAndModel() { // Arrange var expected = new HtmlString("value"); var passedInModel = new object(); var passedInViewData = new ViewDataDictionary(new EmptyModelMetadataProvider()); var helper = new Mock<IHtmlHelper>(MockBehavior.Strict); helper.Setup(h => h.PartialAsync("test", passedInModel, passedInViewData)) .Returns(Task.FromResult(expected)) .Verifiable(); // Act var actual = helper.Object.Partial("test", passedInModel, passedInViewData); // Assert Assert.Same(expected, actual); helper.Verify(); }
public void Process_DoesNotResolveNonTildeSlashValues_InHtmlString(HtmlString url) { // Arrange var tagHelperOutput = new TagHelperOutput( tagName: "a", attributes: new TagHelperAttributeList { { "href", url } }, getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null)); var urlHelperMock = new Mock<IUrlHelper>(); urlHelperMock .Setup(urlHelper => urlHelper.Content(It.IsAny<string>())) .Returns("approot/home/index.html"); var urlHelperFactory = new Mock<IUrlHelperFactory>(); urlHelperFactory .Setup(f => f.GetUrlHelper(It.IsAny<ActionContext>())) .Returns(urlHelperMock.Object); var tagHelper = new UrlResolutionTagHelper(urlHelperFactory.Object, new HtmlTestEncoder()); var context = new TagHelperContext( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( Enumerable.Empty<IReadOnlyTagHelperAttribute>()), items: new Dictionary<object, object>(), uniqueId: "test"); // Act tagHelper.Process(context, tagHelperOutput); // Assert var attribute = Assert.Single(tagHelperOutput.Attributes); Assert.Equal("href", attribute.Name, StringComparer.Ordinal); var attributeValue = Assert.IsType<HtmlString>(attribute.Value); Assert.Equal(url.ToString(), attributeValue.ToString(), StringComparer.Ordinal); Assert.False(attribute.Minimized); }