public void ToStringTest() { var text = "Hello"; Action<TextWriter> action = tw => tw.Write(text); var helper = new HelperResult(action); Assert.Equal(text, helper.ToString()); }
public void WriteToTest() { var text = "Hello"; Action<TextWriter> action = tw => tw.Write(text); var helper = new HelperResult(action); var writer = new StringWriter(); helper.WriteTo(writer); Assert.AreEqual(text, writer.ToString()); }
public HelperResult RenderSection(string name, IHtmlString defaultContents) { if (this.IsSectionDefined(name)) { return this.RenderSection(name); } var result = new HelperResult((x) => x.Write(defaultContents.ToString())); return this.RenderSection(name, (x) => result); }
public void RenderPanel(HtmlTextWriter writer) { writer.AddAttribute("id", PanelId); writer.RenderBeginTag(HtmlTextWriterTag.Div); if (Content != null) { System.Web.WebPages.HelperResult res = Content(this); writer.Write(res.ToHtmlString()); } writer.RenderEndTag(); }
public void ToHtmlStringDoesNotEncode() { // Arrange string text = "<strong>This is a test & it uses html.</strong>"; Action<TextWriter> action = writer => writer.Write(text); HelperResult helperResult = new HelperResult(action); // Act string result = helperResult.ToHtmlString(); // Assert Assert.Equal(result, text); }
public void ToHtmlStringReturnsSameResultAsWriteTo() { // Arrange string text = "<strong>This is a test & it uses html.</strong>"; Action<TextWriter> action = writer => writer.Write(text); HelperResult helperResult = new HelperResult(action); StringWriter stringWriter = new StringWriter(); // Act string htmlString = helperResult.ToHtmlString(); helperResult.WriteTo(stringWriter); // Assert Assert.Equal(htmlString, stringWriter.ToString()); }
/// <summary> /// Initialize system placeholders (for map path to template styles, etc.) /// </summary> /// <param name="tpl"></param> /// <returns></returns> public static string InitializeSystemPlaceholders(HelperResult body, string templateVirtualPathPattern, string template, string templateName) { // Replace with body template = template.Replace(_phBody, body.ToString()); // Remove comments template = RemoveComments(template); // If template exists if( !string.IsNullOrEmpty( templateName ) ) { // Initialize template path placeholders template = InitTemplatePathPlaceholders(templateVirtualPathPattern, template, templateName); } return template; }
public abstract void Write(HelperResult result);
public override void Write(HelperResult result) { WriteTo(Output, result); }
// This method is called by generated code and needs to stay in sync with the parser public static void WriteTo(TextWriter writer, HelperResult content) { if (content != null) { content.WriteTo(writer); } }
public override void Write(HelperResult result) { WebPageExecutingBase.WriteTo(this.Output, result); }
/// <summary> /// 显示可访问的按钮脚本。 /// </summary> /// <param name="html">HTML呈现助手</param> /// <param name="button">button名称</param> /// <param name="scriptContext">脚本回调</param> /// <returns>经过编码的字符串</returns> public static IHtmlString RenderButtonScript(this HtmlHelper html, string button, Func<SystemMenu, object> scriptContext) { var rspAccessButtons = _permissionService.GetAccessibleButtons( WebHelper.GetLogOnUserId(), html.ViewContext.RequestContext.HttpContext.Request.GetCurrentNavigateUrl()); if (rspAccessButtons.IsSuccess && !rspAccessButtons.Datas.IsEmpty()) { var accessButton = rspAccessButtons.Datas.FirstOrDefault(s => s.Name == button); if (accessButton != null) { var helperResult = new HelperResult(writer => writer.Write(scriptContext(accessButton))); return html.Raw(helperResult); } } return new HtmlString(string.Empty); }
public void HelperResultConstructorNullTest() { ExceptionAssert.ThrowsArgNull(() => { var helper = new HelperResult(null); }, "action"); }
public string ToHtmlString() { var items = new HelperResult(writer => { // render the first/previous if needed be if (_alwaysShowNavigation || (_totalPages > 1 && _currentPage != 1)) { writer.Write(_firstLink(1)); writer.Write(_previousLink(Math.Max(1, _currentPage - 1))); } // build the page number looks 'around' the curent page BuildPageRange((pageNumber) => writer.Write(_pageLink(pageNumber))); // render the next/last if needed be if (_alwaysShowNavigation || (_currentPage < _totalPages)) { writer.Write(_nextLink(Math.Min(_currentPage + 1, _totalPages))); writer.Write(_lastLink(_totalPages)); } }); return _layoutTemplate(items).ToHtmlString(); }
private static void RenderSectionAsLayoutRegion(WebPageBase webPage, string partialViewHtml, string sectionName) { webPage.DefineSection( sectionName, () => { Action<TextWriter> writerAction = tw => tw.Write(partialViewHtml); var result = new HelperResult(writerAction); webPage.Write(result); }); }
public void ShouldRenderAs(string describe, string expected, HelperResult helperResult) { object actualResult = null; Exception exception = null; try { actualResult = helperResult.ToHtmlString(); } catch (Exception ex) { exception = ex; } ShouldEqual(describe, expected, actualResult, exception); }
/// <summary> /// Writes the specified <see cref="T:System.Web.WebPages.HelperResult"/> object as an HTML-encoded string. /// </summary> /// <param name="result">The helper result to encode and write.</param> public override void Write(HelperResult result);
private bool TryRender(string path, object model, out HelperResult result) { try { var htmlString = page.RenderPage(path, model).ToHtmlString(); result = new HelperResult((writer) => { writer.Write(htmlString); }); return true; } catch (HttpException ex) { HttpContext.Current.Trace.Warn(ex.ToString()); } result = null; return false; }
/// <summary> /// 获取当前用户在当前界面可访问的按钮。 /// </summary> /// <param name="html">HTML呈现助手</param> /// <param name="leftPart">附加HTML标签区域</param> /// <returns>经过HTML编码的字符串</returns> public static IHtmlString RenderAccessibleButtons(this HtmlHelper html, Func<IList<SystemMenu>, object> leftPart = null) { var tagBuilder = new TagBuilder("div"); tagBuilder.AddCssClass("toolbar panel panel-default"); var contentBuilder = new TagBuilder("div"); contentBuilder.AddCssClass("panel-heading"); var rspAccessButtons = _permissionService.GetAccessibleButtons( WebHelper.GetLogOnUserId(), html.ViewContext.RequestContext.HttpContext.Request.GetCurrentNavigateUrl()); var leftTag = new TagBuilder("div"); leftTag.AddCssClass("left"); if (leftPart != null) { var helperResult = new HelperResult(writer => writer.Write(leftPart(rspAccessButtons.Datas))); leftTag.InnerHtml = helperResult.ToHtmlString(); } var rightTag = new TagBuilder("div"); rightTag.AddCssClass("right btn-group"); if (rspAccessButtons.IsSuccess) { if (!rspAccessButtons.Datas.IsEmpty()) { foreach (var item in rspAccessButtons.Datas) { rightTag.InnerHtml += $"<a href=\"#\" onclick=\"{item.NavigateUrl}()\" class=\"btn btn-default\"><i class=\"{item.Image}\"></i> {item.Title}</a>"; } } } contentBuilder.InnerHtml += leftTag.ToString(); contentBuilder.InnerHtml += rightTag.ToString(); tagBuilder.InnerHtml += contentBuilder.ToString(); return new MvcHtmlString(tagBuilder.ToString()); }
public static IHtmlString RequireStyleForLayout(this HtmlHelper html, Func<object, HelperResult> style) { var helperResult = new HelperResult(writer => style(null).WriteTo(writer)).ToString(); var a = new AssetManager.Asset(helperResult, AssetManager.AssetType.Style); AssetManager.GetInstance(html.ViewContext.HttpContext).Require(a, true); return new MvcHtmlString(""); }
public override void Write(HelperResult result) { ChildPage.Write(result); }
public HelperResult RenderSection(string name, bool required) { EnsurePageCanBeRequestedDirectly("RenderSection"); if (PreviousSectionWriters.ContainsKey(name)) { var result = new HelperResult(tw => { if (_renderedSections.Contains(name)) { throw new HttpException(String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionAleadyRendered, name)); } var body = PreviousSectionWriters[name]; // Since the body can also call RenderSection, we need to temporarily remove // the current sections from the stack. var top = SectionWritersStack.Pop(); bool pushed = false; try { if (Output != tw) { OutputStack.Push(tw); pushed = true; } body(); } finally { if (pushed) { OutputStack.Pop(); } } SectionWritersStack.Push(top); _renderedSections.Add(name); }); return result; } else if (required) { // If the section is not found, and it is not optional, throw an error. throw new HttpException(String.Format(CultureInfo.InvariantCulture, WebPageResources.WebPage_SectionNotDefined, name)); } else { // If the section is optional and not found, then don't do anything. return null; } }
public static HelperResult RenderSection(this WebPageBase webPage, string name, HelperResult defaultContents) { return webPage.IsSectionDefined(name) ? webPage.RenderSection(name) : defaultContents; }
/// <summary> /// 表单附属标签。 /// </summary> /// <param name="buttonPart">按钮设置区域</param> /// <returns>表单控件</returns> public TextBoxControl AddOn(Func<PropertyMetadata, object> buttonPart) { var helperResult = new HelperResult(writer => writer.Write(buttonPart(this._metadata))); this._addOn = new TagBuilder("div"); this._addOn.AddCssClass("input-group-btn"); this._addOn.InnerHtml = helperResult.ToHtmlString(); return this; }
/// <summary> /// 创建表单。 /// </summary> /// <returns>表单信息</returns> protected override TagBuilder CreateForm() { var container = new TagBuilder("div"); var helperResult = new HelperResult(writer => writer.Write(this._formPartFunc(this._metadata))); container.InnerHtml = helperResult.ToHtmlString(); return container; }
public Tab(string id, string title, HelperResult body) : this(id, MvcHtmlString.Create(HttpUtility.HtmlEncode(title)), body) { }
public static void WriteLiteralTo(TextWriter writer, HelperResult value) { WebPageBase.WriteLiteralTo(writer, value); }
public Tab(string id, MvcHtmlString title, HelperResult body) : this(id, new HelperResult(writer => writer.Write(title)), body) { }
public Tab(string id, HelperResult title, MvcHtmlString body) : this(id, title, new HelperResult(writer => writer.Write(body))) { }
public override void Write(HelperResult result) { if (result != null) { result.WriteTo(Output); } }
public Tab(string id, HelperResult title, HelperResult body) { this.Id = id; this.Title = title; this.Body = body; }