public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { if (string.IsNullOrEmpty(Capture)) { return; } var attributes = context.AllAttributes .Where(a => !SystemAttributes.Contains(a.Name)) .ToDictionary(k => k.Name, v => v.Value); var content = await output.GetChildContentAsync(); var key = $"Element_{Capture}"; ContentCapture capture = null; if (ViewContext.HttpContext.Items.ContainsKey(key)) { capture = ViewContext.HttpContext.Items[key] as ContentCapture; } if (capture == null) { capture = new ContentCapture(); ViewContext.HttpContext.Items.Add(key, capture); } var order = Priority ?? int.MaxValue; capture.Add(content, attributes, output.TagName, NoTag.GetValueOrDefault(false), order, AllowMerge); output.SuppressOutput(); }
private async Task RenderBlocks(TextWriter tw, ContentCapture capture) { const string srcAttribute = "src"; const string idAttribute = "id"; var blocks = capture.Blocks; if (!string.IsNullOrWhiteSpace(NoDuplicateSource) && bool.TryParse(NoDuplicateSource, out var hasBoolNoDuplicateSource) && hasBoolNoDuplicateSource) { blocks = blocks .GroupBy(b => b.Attributes.ContainsKey(srcAttribute) ? b.Attributes[srcAttribute] : b.Attributes.ContainsKey(idAttribute) ? b.Attributes[idAttribute] : Guid.NewGuid()) .Select(b => b.First()); } else if (!string.IsNullOrWhiteSpace(NoDuplicateSource)) { blocks = blocks .GroupBy(b => b.Attributes.ContainsKey(NoDuplicateSource) ? b.Attributes[NoDuplicateSource] : Guid.NewGuid()) .Select(b => b.First()); } var orderedBlocks = blocks.OrderBy(b => b.Order); var mergableBlocks = orderedBlocks.Where(b => ((AutoMerge && (!b.CanMerge.HasValue || b.CanMerge.Value)) || (!AutoMerge && b.CanMerge.HasValue && b.CanMerge.Value)) && !b.Content.IsEmptyOrWhiteSpace); var otherBlocks = orderedBlocks.Except(mergableBlocks); await RenderMergedBlocks(tw, mergableBlocks); await RenderSeparateBlocks(tw, otherBlocks); }
private async Task RenderBlocks(TextWriter tw, ContentCapture capture) { var blocks = capture.Blocks; if (NoDuplicates) { blocks = blocks .GroupBy(b => b.Attributes.ContainsKey(NoDuplicateSource) ? b.Attributes[NoDuplicateSource] : Guid.NewGuid()) .Select(b => b.First()); } var orderedBlocks = blocks.OrderBy(b => b.Order); var mergableBlocks = orderedBlocks.Where(b => ((AutoMerge && (!b.CanMerge.HasValue || b.CanMerge.Value)) || (!AutoMerge && b.CanMerge.HasValue && b.CanMerge.Value)) && !b.Content.IsEmptyOrWhiteSpace); var otherBlocks = orderedBlocks.Except(mergableBlocks); await RenderMergedBlocks(tw, mergableBlocks); await RenderSeparateBlocks(tw, otherBlocks); }