/// <summary> /// Initializes a new instance of the <see cref="HtmlContentWrapperTextWriter"/> class. /// </summary> /// <param name="contentBuilder">The <see cref="IHtmlContentBuilder"/> to write to.</param> /// <param name="encoding">The <see cref="System.Text.Encoding"/> in which output is written.</param> public HtmlContentWrapperTextWriter(IHtmlContentBuilder contentBuilder, Encoding encoding) { if (contentBuilder == null) { throw new ArgumentNullException(nameof(contentBuilder)); } if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } ContentBuilder = contentBuilder; Encoding = encoding; }
private static IHtmlContentBuilder AppendJavaScript(this IHtmlContentBuilder content, string path) { return(content.AppendHtml($"\n<script src='{path}'></script>")); }
private async Task RenderControllerAction(string controller, string action, Rendering rendering, IHtmlContentBuilder output, ViewContext viewContext, PageData pageData) { var context = await GetViewContext(rendering, viewContext, pageData, new NullView(), viewContext.Writer); var toContext = _viewComponentHelper as IViewContextAware; if (toContext != null) { toContext.Contextualize(context); } try { var result = await _viewComponentHelper.InvokeAsync(action + ": " + controller); output.AppendHtml(result); } catch (InvalidOperationException exc) { output.AppendHtml($"<div class=\"alert alert-danger\"><strong>{exc.Message}</strong> not found!</div>"); } }
public void MoveTo(IHtmlContentBuilder destination) { CopyTo(destination); Clear(); }
/// <summary> /// Appends an <see cref="Environment.NewLine"/>. /// </summary> /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param> /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns> public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder) { builder.Append(HtmlEncodedString.NewLine); return(builder); }
/// <summary> /// Sets the content to the <see cref="IHtmlContent"/> value. /// </summary> /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param> /// <param name="value">The <see cref="IHtmlContent"/> value that replaces the content.</param> /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns> public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, IHtmlContent content) { builder.Clear(); builder.Append(content); return(builder); }
/// <inheritdoc /> public override void CopyTo(IHtmlContentBuilder destination) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if (!_hasContent) { return; } if (_isSingleContentSet) { CopyToCore(_singleContent, destination); } else { for (var i = 0; i < (_buffer?.Count ?? 0); i++) { CopyToCore(Buffer[i], destination); } } }
/// <inheritdoc /> public void CopyTo(IHtmlContentBuilder destination) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } destination.AppendHtml(Name); if (ValueStyle == HtmlAttributeValueStyle.Minimized) { return; } var valuePrefix = GetAttributeValuePrefix(ValueStyle); if (valuePrefix != null) { destination.AppendHtml(valuePrefix); } string valueAsString; IHtmlContentContainer valueAsHtmlContainer; IHtmlContent valueAsHtmlContent; if ((valueAsString = Value as string) != null) { destination.Append(valueAsString); } else if ((valueAsHtmlContainer = Value as IHtmlContentContainer) != null) { valueAsHtmlContainer.CopyTo(destination); } else if ((valueAsHtmlContent = Value as IHtmlContent) != null) { destination.AppendHtml(valueAsHtmlContent); } else if (Value != null) { destination.Append(Value.ToString()); } var valueSuffix = GetAttributeValueSuffix(ValueStyle); if (valueSuffix != null) { destination.AppendHtml(valueSuffix); } }
private static void CopyElement(TagHelperOutput output, IHtmlContentBuilder destination, out IHtmlContentBuilder contentBuilder) { contentBuilder = destination; if (output.TagName.HasValue()) { var tag = new TagBuilder(output.TagName); foreach (var attribute in output.Attributes) { tag.Attributes.Add(attribute.Name, attribute.Value?.ToString()); } if (output.TagMode == TagMode.StartTagAndEndTag) { tag.TagRenderMode = TagRenderMode.Normal; contentBuilder = tag.InnerHtml; } else if (output.TagMode == TagMode.SelfClosing) { tag.TagRenderMode = TagRenderMode.SelfClosing; contentBuilder = null; } else // StartTagOnly { tag.TagRenderMode = TagRenderMode.StartTag; contentBuilder = null; } destination.AppendHtml(tag); } }
private static void RenderOnScreenModeZoneWrapperEnd(bool isWidgetEditMode, string zoneName, IHtmlContentBuilder builder) { builder.AppendHtml($"<!--end zone {zoneName}-->"); }
private static void RenderOnScreenModeZoneWrapperStart(bool isWidgetEditMode, string zoneName, IHtmlContentBuilder builder) { if (isWidgetEditMode) { builder.AppendHtml($"<!--start zone {zoneName} {{ isRecursive='{ZoneIsRecursive(zoneName).ToString().ToLowerInvariant()}' isGlobal='{ZoneIsGlobal(zoneName).ToString().ToLowerInvariant()}' }}-->"); } else { builder.AppendHtml($"<!--start zone {zoneName}-->"); } }
private static void RenderOnScreenModeWidgetWrapperStart(bool renderFullInfo, IHtmlContentBuilder builder, IAbstractItem widget) { if (renderFullInfo) { builder.AppendHtml($"<!--start widget {widget.Id} {{ alias='{widget.Alias}' title='{widget.Title.Replace("'", "").Replace("}", "").Replace("{", "")}' type='{widget.GetMetadata(OnScreenWidgetMetadataKeys.Type)}' published='{widget.GetMetadata(OnScreenWidgetMetadataKeys.Published)?.ToString()?.ToLower()}' order='{widget.SortOrder}' }}-->"); } else { builder.AppendHtml($"<!--start widget-->"); } }
/// <inheritdoc /> public void MoveTo(IHtmlContentBuilder destination) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } for (var i = 0; i < Entries.Count; i++) { var entry = Entries[i]; string entryAsString; IHtmlContentContainer entryAsContainer; if ((entryAsString = entry as string) != null) { destination.Append(entryAsString); } else if ((entryAsContainer = entry as IHtmlContentContainer) != null) { // Since we're moving, do a deep flatten. entryAsContainer.MoveTo(destination); } else { // Only string, IHtmlContent values can be added to the buffer. destination.AppendHtml((IHtmlContent)entry); } } Entries.Clear(); }
public void BaseHrefRender(IHtmlContentBuilder htmlContentBuilder) { htmlContentBuilder.AppendHtml($"<base href='{_mainAttributeValue}'>"); }
public void MetaRender(IHtmlContentBuilder htmlContentBuilder) { htmlContentBuilder.AppendHtml($"<meta name='{_name}' content='{_mainAttributeValue}'>"); }
public void CharsetRender(IHtmlContentBuilder builder) { builder.AppendHtml($"<meta charset='{_mainAttributeValue}'>"); }
void IHtmlContentContainer.MoveTo(IHtmlContentBuilder destination) { }
public void MoveTo(IHtmlContentBuilder destination) { CopyTo(destination); Clear(); }
private void MoveToCore(object entry, IHtmlContentBuilder destination) { if (entry == null) { return; } string entryAsString; IHtmlContentContainer entryAsContainer; if ((entryAsString = entry as string) != null) { destination.Append(entryAsString); } else if ((entryAsContainer = entry as IHtmlContentContainer) != null) { entryAsContainer.MoveTo(destination); } else { destination.AppendHtml((IHtmlContent)entry); } }
void IHtmlContentContainer.MoveTo(IHtmlContentBuilder destination) { if (destination == null) { throw new ArgumentNullException(nameof(destination)); } _preElement?.MoveTo(destination); var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(TagName); if (!isTagNameNullOrWhitespace) { destination.AppendHtml("<"); destination.AppendHtml(TagName); // Perf: Avoid allocating enumerator for (var i = 0; i < Attributes.Count; i++) { var attribute = Attributes[i]; destination.AppendHtml(" "); attribute.MoveTo(destination); } if (TagMode == TagMode.SelfClosing) { destination.AppendHtml(" /"); } destination.AppendHtml(">"); } if (isTagNameNullOrWhitespace || TagMode == TagMode.StartTagAndEndTag) { _preContent?.MoveTo(destination); _content?.MoveTo(destination); _postContent?.MoveTo(destination); } if (!isTagNameNullOrWhitespace && TagMode == TagMode.StartTagAndEndTag) { destination.AppendHtml("</"); destination.AppendHtml(TagName); destination.AppendHtml(">"); } _postElement?.MoveTo(destination); // Depending on the code path we took, these might need to be cleared. _preContent?.Clear(); _content?.Clear(); _postContent?.Clear(); Attributes.Clear(); }
/// <summary> /// Appends an <see cref="Environment.NewLine"/> after appending the <see cref="string"/> value. /// The value is treated as HTML encoded as-provided, and no further encoding will be performed. /// </summary> /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param> /// <param name="content">The HTML encoded <see cref="string"/> to append.</param> /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns> public static IHtmlContentBuilder AppendLineEncoded(this IHtmlContentBuilder builder, string encoded) { builder.AppendEncoded(encoded); builder.Append(HtmlEncodedString.NewLine); return(builder); }
public async Task PopulateHtmlTagAsync(ITagBuilderOptions options, ReadOnlyTagHelperAttributeList attributeList, Asset asset, IHtmlContentBuilder output) { TagBuilder tagBuilder = new TagBuilder("script"); tagBuilder.TagRenderMode = TagRenderMode.Normal; tagBuilder.Attributes.Add("type", "text/javascript"); if (attributeList.ContainsName("defer")) { tagBuilder.Attributes.Add("defer", null); } else if (attributeList.ContainsName("async")) { tagBuilder.Attributes.Add("async", null); } var outputMode = options.Output; if (!this.Options.BundleJavascripts && !this.Options.MinifyJavascripts && OptimizationTagOutput.Loader != outputMode) { outputMode = OptimizationTagOutput.Tag; } else if (OptimizationTagOutput.Tag == outputMode && (this.Options.BundleJavascripts || this.Options.MinifyJavascripts)) { outputMode = OptimizationTagOutput.StaticFile; } switch (outputMode) { case OptimizationTagOutput.Default: //same as OptimizationTagOutput.Inline string content = await asset.ReadContentAsStringAsync(); tagBuilder.InnerHtml.SetHtmlContent(content); break; case OptimizationTagOutput.Tag: tagBuilder.Attributes.Add("src", asset.GetWebLocation(this.Options)); if (null != asset?.Content) { await asset.Content.DisposeAsync(); } break; case OptimizationTagOutput.StaticFile: case OptimizationTagOutput.Loader: string webPath = null; PathString relativeFilePath = new PathString(this.Options.StaticAssetsPath).Add($"/{asset.Name}"); var assetFileInfo = this.HostingEnvironment.WebRootFileProvider.GetFileInfo(relativeFilePath); if (!assetFileInfo.Exists) { content = await asset.ReadContentAsStringAsync(); await File.WriteAllTextAsync(assetFileInfo.PhysicalPath, content, Encoding.UTF8); } if (string.IsNullOrWhiteSpace(this.Options.AssetRootUrl)) { webPath = this.HttpContextAccessor.HttpContext.Request.PathBase.Add(relativeFilePath); } else { if (!Uri.TryCreate(this.Options.AssetRootUrl, UriKind.Absolute, out Uri cdnRootUri)) { throw new UriFormatException($"'{this.Options.AssetRootUrl}' is an invalid Uri. An absolute Uri is expected."); } Uri.TryCreate(cdnRootUri, $"{asset.Name}", out Uri webPathUri); webPath = webPathUri.ToString(); } if (OptimizationTagOutput.StaticFile == outputMode) { tagBuilder.Attributes.Add("src", webPath); } else { tagBuilder.InnerHtml.SetHtmlContent($"adriva.optimization.loader.push('{webPath}', 1)"); } if (null != asset?.Content) { await asset.Content.DisposeAsync(); } break; } output.AppendHtml(tagBuilder); }
/// <summary> /// Sets the content to the <see cref="string"/> value. The value is treated as HTML encoded as-provided, and /// no further encoding will be performed. /// </summary> /// <param name="builder">The <see cref="IHtmlContentBuilder"/>.</param> /// <param name="content">The HTML encoded <see cref="string"/> that replaces the content.</param> /// <returns>The <see cref="IHtmlContentBuilder"/>.</returns> public static IHtmlContentBuilder SetContentEncoded(this IHtmlContentBuilder builder, string encoded) { builder.Clear(); builder.AppendEncoded(encoded); return(builder); }
public void StylesheetRender(IHtmlContentBuilder htmlContentBuilder) { htmlContentBuilder.AppendHtml($" <link rel='stylesheet' href='{_mainAttributeValue}'>"); }
public void CopyTo(IHtmlContentBuilder destination) { foreach (var entry in Entries) { destination.AppendHtml(entry); } }
public void TitleRender(IHtmlContentBuilder htmlContentBuilder) { htmlContentBuilder.AppendHtml($"<title>{_mainAttributeValue}</title>"); }
HtmlString BuildLinks() { if (Model == null) { throw new ArgumentNullException(nameof(Model)); } if (Model.Size == 0) { throw new ArgumentNullException(nameof(Model.Size)); } var currentPage = Model.Page; if (currentPage < 1) { currentPage = 1; } var firstPage = Math.Max(1, Model.Page - (NumberOfPagesToShow / 2)); var lastPage = Math.Min(_totalPageCount, Model.Page + (int)(NumberOfPagesToShow / 2)); IHtmlContentBuilder output = null; if (NumberOfPagesToShow > 0 && lastPage > 1) { output = new HtmlContentBuilder(); for (var i = firstPage; i <= lastPage; i++) { if (i == 1) { _routeData.Remove(pageKey); } else { _routeData[pageKey] = i; } var url = _urlHelper.RouteUrl(new UrlRouteContext { Values = _routeData }); var builder = new HtmlContentBuilder(); output.AppendHtml(builder .AppendHtml("<li class=\"page-item") .AppendHtml(i == Model.Page ? " active" : "") .AppendHtml("\">") .AppendHtml("<a class=\"page-link\" href=\"") .AppendHtml(url) .AppendHtml("\" aria-label=\"") .AppendHtml(i.ToString()) .AppendHtml("\">") .AppendHtml(i.ToString()) .AppendHtml("</a>") .AppendHtml("</li>")); } } if (output != null) { return(output.ToHtmlString()); } return(HtmlString.Empty); }
public void CopyTo(IHtmlContentBuilder destination) { }
public async Task DoRender(string placeholderName, bool isDynamic, IHtmlContentBuilder output, ViewContext viewContext) { if (viewContext?.ViewData == null) { output.Append("ViewContext/ViewData cannot be null"); return; } if (isDynamic && viewContext.ViewData[BocUniqueId] == null) { output.Append("Nested placeholders must be inside a known component"); return; } var placeholderStack = viewContext.ViewData[BocPlaceholderStack] as Stack <string>; if (placeholderStack == null) { placeholderStack = (Stack <string>)(viewContext.ViewData[BocPlaceholderStack] = new Stack <string>()); } var usedName = isDynamic ? placeholderName + "_" + (Guid)viewContext.ViewData[BocUniqueId] : placeholderName; placeholderStack.Push(usedName); var placeHolderKey = placeholderStack.Count > 1 ? "/" + string.Join("/", placeholderStack.ToArray().Reverse()) : usedName; var pathandQuery = viewContext.HttpContext.Request.Path.Value + viewContext.HttpContext.Request.QueryString; var pageData = await GetPageData(pathandQuery); if (pageData == null || string.IsNullOrEmpty(placeHolderKey)) { output.AppendHtml("UNABLE TO LOAD PLACEHOLDER " + placeHolderKey); return; } RenderingChrome?placeholderChrome = null; if (viewContext.HttpContext.IsInCmsMode()) { placeholderChrome = await _sitecoreService.Get <RenderingChrome>(pathandQuery + (pathandQuery.Contains("?") ? "&" : "?") + "placeholderKey=" + placeHolderKey, _baseAddress); if (placeholderChrome.HasValue) { output.AppendHtmlLine(placeholderChrome.Value.Start); } } foreach (var rendering in pageData.Renderings.Where(r => usedName.Equals(r.Placeholder, StringComparison.OrdinalIgnoreCase) || placeHolderKey.Equals(r.Placeholder, StringComparison.OrdinalIgnoreCase))) { if (viewContext.HttpContext.IsInCmsMode() && rendering.RenderingChrome != null) { output.AppendHtmlLine(rendering.RenderingChrome.Value.Start); } JToken path = null; if (rendering.RenderingItem.TryGetValue("Path", out path)) { await RenderPartialView(path.Value <string>(), rendering, output, viewContext, pageData); } else { JToken controller = null; JToken controllerAction = null; if (rendering.RenderingItem.TryGetValue("Controller", out controller) && rendering.RenderingItem.TryGetValue("Controller Action", out controllerAction)) { await RenderControllerAction(controller.Value <string>(), controllerAction.Value <string>(), rendering, output, viewContext, pageData); } } if (viewContext.HttpContext.IsInCmsMode() && rendering.RenderingChrome != null) { output.AppendHtmlLine(rendering.RenderingChrome.Value.End); } } if (placeholderChrome.HasValue) { output.AppendHtmlLine(placeholderChrome.Value.End); } placeholderStack.Pop(); }
public void MoveTo(IHtmlContentBuilder destination) { }
private static IHtmlContentBuilder AppendStyleSheet(this IHtmlContentBuilder content, string path) { return(content.AppendHtml($"\n<link href='{path}' rel='stylesheet' />")); }
protected virtual async Task RenderAssetAsync(IOptimizationResultTagBuilder optimizationResultTagBuilder, RendererTagAttributes attributes, Asset asset, string extension, IHtmlContentBuilder htmlContentBuilder) { TagBuilderOptions options = new TagBuilderOptions(extension, OptimizationTagOutput.Tag); await optimizationResultTagBuilder.PopulateHtmlTagAsync(options, attributes, asset, htmlContentBuilder); }
void IHtmlContentContainer.CopyTo(IHtmlContentBuilder destination) { }
public CardRow Content(IHtmlContentBuilder content) { contentText.AppendHtml(content); return(this); }
/// <inheritdoc /> public abstract void CopyTo(IHtmlContentBuilder destination);
public TableCell() { _tag = new TagBuilder("td"); _builder = _tag.InnerHtml; }