#6193: IHtmlFilter and TokenFilter improvements and bugfixes (#6938)

* IHtmlFilter and TokenFilter improvements and bugfixes.

This changeset unifies the two separate TokenFilter implementations (one for BodyPart, TextField, etc and another one for certain elements such as Html).
It also fixes a bug with the TokenFilter when executing for HtmlWidget, where the wrong content item is being recorded by the handler (the original implementation).
Thirdly, it removes awkward code repetition by moving filter execution into a dedicated HtmlFilterRunner service.

* Renaming IHtmlFilterRunner to IHtmlFilterProcessor (and its references)

* Applying IHtmlFilterProcessor to HtmlMenuItems too + code styling in BodyPartDriver

* Fixing FeedControllerTests.CorePartValuesAreExtracted

* Code styling

---------

Co-authored-by: Jean-Thierry Kéchichian <jean-thierry.kechichian@wanadoo.fr>
Co-authored-by: Sipke Schoorstra <sipke@ideliverable.com>
Co-authored-by: Benedek Farkas <benedek.farkas@lombiq.com>
This commit is contained in:
Sipke Schoorstra 2024-04-19 11:20:32 +02:00 committed by GitHub
parent 9b0d78b4f6
commit cf4335e5ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
33 changed files with 202 additions and 238 deletions

View File

@ -20,6 +20,7 @@ using Orchard.Core.Feeds.StandardBuilders;
using Orchard.Tests.Modules;
using Orchard.Tests.Stubs;
using Orchard.Core.Title.Models;
using Orchard.Services;
namespace Orchard.Core.Tests.Feeds.Controllers {
[TestFixture]
@ -177,6 +178,7 @@ namespace Orchard.Core.Tests.Feeds.Controllers {
builder.RegisterInstance(mockContentManager.Object).As<IContentManager>();
builder.RegisterType<RssFeedBuilder>().As<IFeedBuilderProvider>();
builder.RegisterType<CorePartsFeedItemBuilder>().As<IFeedItemBuilder>();
builder.RegisterType<HtmlFilterProcessor>().As<IHtmlFilterProcessor>();
builder.RegisterInstance(query).As<IFeedQueryProvider>();
var container = builder.Build();

View File

@ -1,12 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web;
using Orchard.Mvc.Html;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
using Orchard.ContentManagement.Drivers;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Settings;
using Orchard.Core.Common.ViewModels;
using Orchard.Services;
using System.Web.Mvc;
@ -15,13 +12,13 @@ using Orchard.ContentManagement.Handlers;
namespace Orchard.Core.Common.Drivers {
public class BodyPartDriver : ContentPartDriver<BodyPart> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
private readonly RequestContext _requestContext;
private const string TemplateName = "Parts.Common.Body";
public BodyPartDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters, RequestContext requestContext) {
_htmlFilters = htmlFilters;
public BodyPartDriver(IOrchardServices services, IHtmlFilterProcessor htmlFilterProcessor, RequestContext requestContext) {
_htmlFilterProcessor = htmlFilterProcessor;
Services = services;
_requestContext = requestContext;
}
@ -33,32 +30,27 @@ namespace Orchard.Core.Common.Drivers {
}
protected override DriverResult Display(BodyPart part, string displayType, dynamic shapeHelper) {
string GetProcessedBodyText() => _htmlFilterProcessor.ProcessFilters(part.Text, part.GetFlavor(), part);
return Combined(
ContentShape("Parts_Common_Body",
() => {
var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text, GetFlavor(part)));
return shapeHelper.Parts_Common_Body(Html: new HtmlString(bodyText));
}),
ContentShape("Parts_Common_Body_Summary",
() => {
var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text, GetFlavor(part)));
return shapeHelper.Parts_Common_Body_Summary(Html: new HtmlString(bodyText));
})
);
ContentShape("Parts_Common_Body", () =>
shapeHelper.Parts_Common_Body(Html: new HtmlString(GetProcessedBodyText()))),
ContentShape("Parts_Common_Body_Summary", () =>
shapeHelper.Parts_Common_Body_Summary(Html: new HtmlString(GetProcessedBodyText()))));
}
protected override DriverResult Editor(BodyPart part, dynamic shapeHelper) {
var model = BuildEditorViewModel(part,_requestContext);
return ContentShape("Parts_Common_Body_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
return ContentShape("Parts_Common_Body_Edit", () =>
shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
protected override DriverResult Editor(BodyPart part, IUpdateModel updater, dynamic shapeHelper) {
var model = BuildEditorViewModel(part, _requestContext);
updater.TryUpdateModel(model, Prefix, null, null);
return ContentShape("Parts_Common_Body_Edit",
() => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
return ContentShape("Parts_Common_Body_Edit", () =>
shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
protected override void Importing(BodyPart part, ContentManagement.Handlers.ImportContentContext context) {
@ -83,18 +75,11 @@ namespace Orchard.Core.Common.Drivers {
private static BodyEditorViewModel BuildEditorViewModel(BodyPart part,RequestContext requestContext) {
return new BodyEditorViewModel {
BodyPart = part,
EditorFlavor = GetFlavor(part),
EditorFlavor = part.GetFlavor(),
AddMediaPath = new PathBuilder(part,requestContext).AddContentType().AddContainerSlug().ToString()
};
}
private static string GetFlavor(BodyPart part) {
var typePartSettings = part.Settings.GetModel<BodyTypePartSettings>();
return (typePartSettings != null && !string.IsNullOrWhiteSpace(typePartSettings.Flavor))
? typePartSettings.Flavor
: part.PartDefinition.Settings.GetModel<BodyPartSettings>().FlavorDefault;
}
class PathBuilder {
private readonly IContent _content;
private string _path;

View File

@ -14,10 +14,10 @@ using Orchard.Utility.Extensions;
namespace Orchard.Core.Common.Drivers {
public class TextFieldDriver : ContentFieldDriver<TextField> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public TextFieldDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
public TextFieldDriver(IOrchardServices services, IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
Services = services;
T = NullLocalizer.Instance;
}
@ -37,8 +37,7 @@ namespace Orchard.Core.Common.Drivers {
return ContentShape("Fields_Common_Text", GetDifferentiator(field, part),
() => {
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
object fieldValue = new HtmlString(_htmlFilters.Aggregate(field.Value, (text, filter) => filter.ProcessContent(text, settings.Flavor)));
var fieldValue = new HtmlString(_htmlFilterProcessor.ProcessFilters(field.Value, settings.Flavor, part));
return shapeHelper.Fields_Common_Text(Name: field.Name, Value: fieldValue);
});
}

View File

@ -1,4 +1,5 @@
using Orchard.ContentManagement;
using Orchard.Core.Common.Settings;
namespace Orchard.Core.Common.Models {
public class BodyPart : ContentPart<BodyPartRecord> {
@ -11,5 +12,12 @@ namespace Orchard.Core.Common.Models {
get { return Retrieve(x => x.Format); }
set { Store(x => x.Format, value); }
}
public string GetFlavor() {
var typePartSettings = Settings.GetModel<BodyTypePartSettings>();
return string.IsNullOrWhiteSpace(typePartSettings?.Flavor)
? PartDefinition.Settings.GetModel<BodyPartSettings>().FlavorDefault
: typePartSettings.Flavor;
}
}
}

View File

@ -5,27 +5,26 @@ using System.Web;
using Orchard.Services;
namespace Orchard.Core.Common.Services {
public class BbcodeFilter : IHtmlFilter {
public string ProcessContent(string text, string flavor) {
return BbcodeReplace(text);
public class BbcodeFilter : HtmlFilter {
public override string ProcessContent(string text, HtmlFilterContext context) {
return BbcodeReplace(text, context);
}
// Can be moved somewhere else once we have IoC enabled body text filters.
private static string BbcodeReplace(string text) {
if (string.IsNullOrEmpty(text))
return string.Empty;
private static string BbcodeReplace(string text, HtmlFilterContext context) {
if (String.IsNullOrEmpty(text))
return String.Empty;
// optimize code path if nothing to do
// Optimize code path if nothing to do.
if (!text.Contains("[url]") && !text.Contains("[img]") && !text.Contains("[url=")) {
return text;
}
var sb = new StringBuilder(text);
var index = -1;
var allIndexes = new List<int>();
// process all [url]
// Process all [url].
while (-1 != (index = text.IndexOf("[url]", index + 1, StringComparison.Ordinal))) {
allIndexes.Add(index);
}
@ -63,7 +62,7 @@ namespace Orchard.Core.Common.Services {
var url = text.Substring(start + 5, urlEnd - start - 5);
var title = text.Substring(urlEnd + 1, end - urlEnd - 1);
// substitue [url] by <a>
// Substitute [url] by <a>.
sb.Remove(start, end - start + 6);
sb.Insert(start, String.Format("<a href=\"{0}\">{1}</a>", url, title));
}
@ -85,7 +84,7 @@ namespace Orchard.Core.Common.Services {
var url = text.Substring(start + 5, end - start - 5);
// substitue [url] by <a>
// Substitue [url] by <a>.
sb.Remove(start, end - start + 6);
if (!string.IsNullOrEmpty(url)) {

View File

@ -4,10 +4,10 @@ using Orchard.Services;
using Orchard.Utility.Extensions;
namespace Orchard.Core.Common.Services {
public class TextFieldFilter : IHtmlFilter {
public string ProcessContent(string text, string flavor) {
public class TextFieldFilter : HtmlFilter {
public override string ProcessContent(string text, HtmlFilterContext context) {
// Flavor is null for a normal input/text field
return flavor == null || string.Equals(flavor, "textarea", StringComparison.OrdinalIgnoreCase) ? ReplaceNewLines(text) : text;
return context.Flavor == null || String.Equals(context.Flavor, "textarea", StringComparison.OrdinalIgnoreCase) ? ReplaceNewLines(text) : text;
}
private static string ReplaceNewLines(string text) {

View File

@ -13,15 +13,15 @@ namespace Orchard.Core.Feeds.StandardBuilders {
public class CorePartsFeedItemBuilder : IFeedItemBuilder {
private readonly IContentManager _contentManager;
private readonly RouteCollection _routes;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public CorePartsFeedItemBuilder(
IContentManager contentManager,
RouteCollection routes,
IEnumerable<IHtmlFilter> htmlFilters) {
IHtmlFilterProcessor htmlFilterProcessor) {
_contentManager = contentManager;
_routes = routes;
_htmlFilters = htmlFilters;
_htmlFilterProcessor = htmlFilterProcessor;
}
public void Populate(FeedContext context) {
@ -29,8 +29,8 @@ namespace Orchard.Core.Feeds.StandardBuilders {
var inspector = new ItemInspector(
feedItem.Item,
_contentManager.GetItemMetadata(feedItem.Item),
_htmlFilters);
_contentManager.GetItemMetadata(feedItem.Item),
_htmlFilterProcessor);
// author is intentionally left empty as it could result in unwanted spam

View File

@ -12,17 +12,17 @@ namespace Orchard.Core.Feeds.StandardBuilders {
public class ItemInspector {
private readonly IContent _item;
private readonly ContentItemMetadata _metadata;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
private readonly ICommonPart _common;
private readonly ITitleAspect _titleAspect;
private readonly BodyPart _body;
public ItemInspector(IContent item, ContentItemMetadata metadata) : this(item, metadata, Enumerable.Empty<IHtmlFilter>()) {}
public ItemInspector(IContent item, ContentItemMetadata metadata) : this(item, metadata, null) {}
public ItemInspector(IContent item, ContentItemMetadata metadata, IEnumerable<IHtmlFilter> htmlFilters) {
public ItemInspector(IContent item, ContentItemMetadata metadata, IHtmlFilterProcessor htmlFilterProcessor) {
_item = item;
_metadata = metadata;
_htmlFilters = htmlFilters;
_htmlFilterProcessor = htmlFilterProcessor;
_common = item.Get<ICommonPart>();
_titleAspect = item.Get<ITitleAspect>();
_body = item.Get<BodyPart>();
@ -49,8 +49,8 @@ namespace Orchard.Core.Feeds.StandardBuilders {
public string Description {
get {
if (_body != null && !string.IsNullOrEmpty(_body.Text)) {
return _htmlFilters.Aggregate(_body.Text, (text, filter) => filter.ProcessContent(text, GetFlavor(_body)));
if (_htmlFilterProcessor != null && _body != null && !string.IsNullOrEmpty(_body.Text)) {
return _htmlFilterProcessor.ProcessFilters(_body.Text, GetFlavor(_body), _body);
}
return Title;
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.ContentManagement;
@ -8,16 +7,16 @@ using Orchard.Core.Feeds.Models;
using Orchard.Core.Feeds.StandardBuilders;
using Orchard.Mvc.Extensions;
using Orchard.Services;
using Orchard.Utility.Extensions;
namespace Orchard.Core.Feeds.StandardQueries {
namespace Orchard.Core.Feeds.StandardQueries
{
public class ContainerFeedQuery : IFeedQueryProvider, IFeedQuery {
private readonly IContentManager _contentManager;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public ContainerFeedQuery(IContentManager contentManager, IEnumerable<IHtmlFilter> htmlFilters) {
public ContainerFeedQuery(IContentManager contentManager, IHtmlFilterProcessor htmlFilterProcessor) {
_contentManager = contentManager;
_htmlFilters = htmlFilters;
_htmlFilterProcessor = htmlFilterProcessor;
}
public FeedQueryMatch Match(FeedContext context) {
@ -55,7 +54,7 @@ namespace Orchard.Core.Feeds.StandardQueries {
return;
}
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilters);
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilterProcessor);
if (context.Format == "rss") {
var link = new XElement("link");
context.Response.Element.SetElementValue("title", inspector.Title);

View File

@ -1 +1,10 @@
<span class="raw">@Html.Raw(Model.Content.BodyPart.Text)</span>
@using Orchard.Core.Common.Models
@using Orchard.Services
@{
var htmlFilterProcessor = WorkContext.Resolve<IHtmlFilterProcessor>();
var bodyPart = Model.Content.BodyPart as BodyPart;
var bodyText = htmlFilterProcessor.ProcessFilters(bodyPart.Text, bodyPart.GetFlavor(), bodyPart);
}
<span class="raw">@Html.Raw(bodyText)</span>

View File

@ -1,15 +1,16 @@
using System;
using Orchard.Services;
namespace Markdown.Services {
public class MarkdownFilter : IHtmlFilter {
public string ProcessContent(string text, string flavor) {
return String.Equals(flavor, "markdown", StringComparison.OrdinalIgnoreCase) ? MarkdownReplace(text) : text;
namespace Markdown.Services
{
public class MarkdownFilter : HtmlFilter {
public override string ProcessContent(string text, HtmlFilterContext context) {
return String.Equals(context.Flavor, "markdown", StringComparison.OrdinalIgnoreCase) ? MarkdownReplace(text) : text;
}
private static string MarkdownReplace(string text) {
if (string.IsNullOrEmpty(text))
return string.Empty;
if (String.IsNullOrEmpty(text))
return String.Empty;
return Markdig.Markdown.ToHtml(text);
}

View File

@ -12,7 +12,7 @@ using Orchard.MediaLibrary.Models;
using Orchard.Services;
namespace Orchard.Azure.MediaServices.Services.Rendering {
public class CloudVideoFilter : IHtmlFilter {
public class CloudVideoFilter : HtmlFilter {
private readonly IShapeFactory _shapeFactory;
private readonly IContentManager _contentManager;
private readonly IShapeDisplay _shapeDisplay;
@ -23,8 +23,8 @@ namespace Orchard.Azure.MediaServices.Services.Rendering {
_shapeDisplay = shapeDisplay;
}
public string ProcessContent(string text, string flavor) {
return String.Equals(flavor, "html", StringComparison.OrdinalIgnoreCase) ? ReplaceVideoPlaceholder(text) : text;
public override string ProcessContent(string text, HtmlFilterContext context) {
return String.Equals(context.Flavor, "html", StringComparison.OrdinalIgnoreCase) ? ReplaceVideoPlaceholder(text) : text;
}
private string ReplaceVideoPlaceholder(string text) {

View File

@ -2,15 +2,15 @@
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class HeadingElementDriver : ElementDriver<Heading> {
private readonly IElementFilterProcessor _processor;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public HeadingElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public HeadingElementDriver(IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
}
protected override EditorResult OnBuildEditor(Heading element, ElementEditorContext context) {
@ -30,7 +30,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Heading element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _htmlFilterProcessor.ProcessFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
context.ElementShape.Level = element.Level;
}
}

View File

@ -2,15 +2,16 @@
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
namespace Orchard.Layouts.Drivers
{
public class HtmlElementDriver : ElementDriver<Html> {
private readonly IElementFilterProcessor _processor;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public HtmlElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public HtmlElementDriver(IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
}
protected override EditorResult OnBuildEditor(Html element, ElementEditorContext context) {
@ -29,7 +30,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Html element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _htmlFilterProcessor.ProcessFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
}
}
}

View File

@ -2,16 +2,17 @@
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
using MarkdownElement = Orchard.Layouts.Elements.Markdown;
namespace Orchard.Layouts.Drivers {
namespace Orchard.Layouts.Drivers
{
[OrchardFeature("Orchard.Layouts.Markdown")]
public class MarkdownElementDriver : ElementDriver<MarkdownElement> {
private readonly IElementFilterProcessor _processor;
public MarkdownElementDriver(IElementFilterProcessor processor) {
_processor = processor;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public MarkdownElementDriver(IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
}
protected override EditorResult OnBuildEditor(MarkdownElement element, ElementEditorContext context) {
@ -29,7 +30,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(MarkdownElement element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "markdown", context.GetTokenData());
context.ElementShape.ProcessedContent = _htmlFilterProcessor.ProcessFilters(element.Content, new HtmlFilterContext { Flavor = "markdown", Data = context.GetTokenData() });
}
}
}

View File

@ -2,15 +2,15 @@
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class ParagraphElementDriver : ElementDriver<Paragraph> {
private readonly IElementFilterProcessor _processor;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public ParagraphElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public ParagraphElementDriver(IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
}
protected override EditorResult OnBuildEditor(Paragraph element, ElementEditorContext context) {
@ -28,7 +28,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Paragraph element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _htmlFilterProcessor.ProcessFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
}
}
}

View File

@ -2,15 +2,16 @@
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
namespace Orchard.Layouts.Drivers
{
public class TextElementDriver : ElementDriver<Text> {
private readonly IElementFilterProcessor _processor;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public TextElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public TextElementDriver(IHtmlFilterProcessor htmlFilterProcessor) {
_htmlFilterProcessor = htmlFilterProcessor;
}
protected override EditorResult OnBuildEditor(Text element, ElementEditorContext context) {
@ -28,7 +29,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Text element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "textarea", context.GetTokenData());
context.ElementShape.ProcessedContent = _htmlFilterProcessor.ProcessFilters(element.Content, new HtmlFilterContext { Flavor = "textarea", Data = context.GetTokenData() });
}
}
}

View File

@ -1,34 +0,0 @@
using System;
using Orchard.Environment.Extensions;
using Orchard.Layouts.Services;
using Orchard.Tokens;
using System.Collections.Generic;
namespace Orchard.Layouts.Filters {
[OrchardFeature("Orchard.Layouts.Tokens")]
public class TokensFilter : IElementFilter {
private readonly ITokenizer _tokenizer;
public TokensFilter(ITokenizer tokenizer) {
_tokenizer = tokenizer;
}
public string ProcessContent(string text, string flavor) {
return ProcessContent(text, flavor, new Dictionary<string, object>());
}
public string ProcessContent(string text, string flavor, IDictionary<string, object> context) {
if (String.IsNullOrEmpty(text))
return "";
if (!text.Contains("#{")) {
return text;
}
text = _tokenizer.Replace(text, context, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
return text;
}
}
}

View File

@ -29,7 +29,7 @@ Features:
Dependencies: Orchard.Layouts, Orchard.Projections
Orchard.Layouts.Tokens:
Name: Element Tokens
Description: Provides an element token provider that enables elements to be rendered using a token and enables tokens to be used inside of various elements such as Html, Text and Paragraph.
Description: Provides an element token provider that enables elements to be rendered using a token.
Category: Layout
Dependencies: Orchard.Layouts, Orchard.Tokens
Orchard.Layouts.UI:

View File

@ -319,7 +319,6 @@
<Compile Include="Elements\Break.cs" />
<Compile Include="Elements\UIElement.cs" />
<Compile Include="Elements\PlaceableContentItem.cs" />
<Compile Include="Filters\TokensFilter.cs" />
<Compile Include="Framework\Display\ElementDisplayedContext.cs" />
<Compile Include="Framework\Display\ElementDisplayingContext.cs" />
<Compile Include="Framework\Drivers\ImportContentContextWrapper.cs" />
@ -375,9 +374,6 @@
<Compile Include="Services\ElementEventContext.cs" />
<Compile Include="Services\ElementEventHandlerBase.cs" />
<Compile Include="Models\ElementSessionState.cs" />
<Compile Include="Services\ElementFilterProcessor.cs" />
<Compile Include="Services\IElementFilterProcessor.cs" />
<Compile Include="Services\IElementFilter.cs" />
<Compile Include="Services\ICurrentControllerAccessor.cs" />
<Compile Include="Services\IElementEventHandler.cs" />
<Compile Include="Services\ILayoutEditorFactory.cs" />

View File

@ -1,19 +0,0 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public class ElementFilterProcessor : IElementFilterProcessor {
private readonly IEnumerable<IHtmlFilter> _filters;
public ElementFilterProcessor(IEnumerable<IHtmlFilter> filters) {
_filters = filters;
}
public string ProcessContent(string text, string flavor, IDictionary<string, object> context) {
foreach (var htmlFilter in _filters) {
var elementFilter = htmlFilter as IElementFilter;
text = elementFilter != null ? elementFilter.ProcessContent(text, flavor, context) : htmlFilter.ProcessContent(text, flavor);
}
return text;
}
}
}

View File

@ -1,8 +0,0 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public interface IElementFilter : IHtmlFilter {
string ProcessContent(string text, string flavor, IDictionary<string, object> context);
}
}

View File

@ -1,8 +0,0 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public interface IElementFilterProcessor : IDependency {
string ProcessContent(string text, string flavor, IDictionary<string, object> context);
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
@ -11,22 +10,22 @@ using Orchard.Mvc.Extensions;
using Orchard.Projections.Models;
using Orchard.Projections.Services;
using Orchard.Services;
using Orchard.Utility.Extensions;
namespace Orchard.Projections.StandardQueries {
namespace Orchard.Projections.StandardQueries
{
public class QueryFeedQuery : IFeedQueryProvider, IFeedQuery {
private readonly IContentManager _contentManager;
private readonly IProjectionManager _projectionManager;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterProcessor _htmlFilterProcessor;
public QueryFeedQuery(
IContentManager contentManager,
IProjectionManager projectionManager,
IEnumerable<IHtmlFilter> htmlFilters)
IHtmlFilterProcessor htmlFilterProcessor)
{
_contentManager = contentManager;
_projectionManager = projectionManager;
_htmlFilters = htmlFilters;
_htmlFilterProcessor = htmlFilterProcessor;
}
public FeedQueryMatch Match(FeedContext context) {
@ -55,7 +54,7 @@ namespace Orchard.Projections.StandardQueries {
return;
}
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilters);
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilterProcessor);
if (context.Format == "rss") {
var link = new XElement("link");
context.Response.Element.SetElementValue("title", inspector.Title);

View File

@ -1,34 +1,21 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.Core.Common.Models;
using Orchard.Core.Feeds.Models;
using Orchard.Core.Feeds.StandardBuilders;
using Orchard.Mvc.Extensions;
using Orchard.Services;
using Orchard.Utility.Extensions;
using Orchard.Core.Feeds;
using Orchard.Tags.Services;
using Orchard.Localization;
using System.Web.Routing;
using System.Xml.Linq;
using Orchard.Core.Feeds;
using Orchard.Core.Feeds.Models;
using Orchard.Environment.Extensions;
using Orchard.Localization;
using Orchard.Mvc.Extensions;
using Orchard.Tags.Services;
namespace Orchard.Tags.Feeds {
[OrchardFeature("Orchard.Tags.Feeds")]
public class TagFeedQuery : IFeedQueryProvider, IFeedQuery {
private readonly IContentManager _contentManager;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly ITagService _tagService;
public TagFeedQuery(
IContentManager contentManager,
IEnumerable<IHtmlFilter> htmlFilters,
ITagService tagService) {
_contentManager = contentManager;
public TagFeedQuery(ITagService tagService) {
_tagService = tagService;
_htmlFilters = htmlFilters;
T = NullLocalizer.Instance;
}
@ -42,11 +29,11 @@ namespace Orchard.Tags.Feeds {
var tagName = (string)tagIdValue.ConvertTo(typeof(string));
var tag = _tagService.GetTagByName(tagName);
if (tag == null) {
return null;
}
return new FeedQueryMatch { FeedQuery = this, Priority = -5 };
}
@ -57,10 +44,10 @@ namespace Orchard.Tags.Feeds {
var limitValue = context.ValueProvider.GetValue("limit");
var limit = 20;
if (limitValue != null) {
if (limitValue != null) {
Int32.TryParse(Convert.ToString(limitValue), out limit);
}
limit = Math.Min(limit, 100);
var tagName = (string)tagIdValue.ConvertTo(typeof(string));

View File

@ -1,49 +1,33 @@
using System;
using System.Collections.Generic;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Environment.Extensions;
using Orchard.Services;
namespace Orchard.Tokens.Filters {
[OrchardFeature("Orchard.Tokens.HtmlFilter")]
public class TokensFilter : ContentHandler, IHtmlFilter {
public class TokensFilter : HtmlFilter {
private readonly ITokenizer _tokenizer;
private ContentItem _displayed;
public TokensFilter(ITokenizer tokenizer) {
_tokenizer = tokenizer;
}
protected override void BuildDisplayShape(BuildDisplayContext context) {
_displayed = context.ContentItem;
public override string ProcessContent(string text, HtmlFilterContext context) {
return TokensReplace(text, context);
}
public string ProcessContent(string text, string flavor) {
return TokensReplace(text);
}
private string TokensReplace(string text) {
private string TokensReplace(string text, HtmlFilterContext context) {
if (String.IsNullOrEmpty(text))
return String.Empty;
// Optimize code path if nothing to do.
if (!text.Contains("#{")) {
if (!text.Contains("#{"))
return text;
}
var data = new Dictionary<string, object>();
if (_displayed != null)
data["Content"] = _displayed;
text = _tokenizer.Replace(text, data);
_displayed = null;
return text;
return _tokenizer.Replace(text, context.Data,
String.Equals(context.Flavor, "html", StringComparison.OrdinalIgnoreCase)
? new ReplaceOptions { Encoding = ReplaceOptions.HtmlEncode }
: new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
}
}
}

View File

@ -20,3 +20,4 @@ Features:
Description: Evaluates tokens in a body.
Category: Content
Dependencies: Orchard.Tokens
Priority: -1

View File

@ -432,7 +432,11 @@
<Compile Include="Services\Clock.cs" />
<Compile Include="Services\ClientHostAddressAccessor.cs" />
<Compile Include="Services\DefaultJsonConverter.cs" />
<Compile Include="Services\HtmlFilterContext.cs" />
<Compile Include="Services\HtmlFilterProcessor.cs" />
<Compile Include="Services\IClientHostAddressAccessor.cs" />
<Compile Include="Services\HtmlFilter.cs" />
<Compile Include="Services\IHtmlFilterProcessor.cs" />
<Compile Include="Services\IJsonConverter.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />

View File

@ -0,0 +1,5 @@
namespace Orchard.Services {
public abstract class HtmlFilter : Component, IHtmlFilter {
public abstract string ProcessContent(string text, HtmlFilterContext context);
}
}

View File

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace Orchard.Services {
public class HtmlFilterContext {
public string Flavor { get; set; }
public IDictionary<string, object> Data { get; set; } = new Dictionary<string, object>();
}
}

View File

@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Linq;
namespace Orchard.Services {
public class HtmlFilterProcessor : IHtmlFilterProcessor {
private readonly IEnumerable<IHtmlFilter> _filters;
public HtmlFilterProcessor(IEnumerable<IHtmlFilter> filters) {
_filters = filters;
}
public string ProcessFilters(string text, HtmlFilterContext context) {
return _filters.Aggregate(text, (current, htmlFilter) => htmlFilter.ProcessContent(current, context));
}
}
}

View File

@ -1,5 +1,5 @@
namespace Orchard.Services {
public interface IHtmlFilter : IDependency {
string ProcessContent(string text, string flavor);
string ProcessContent(string text, HtmlFilterContext context);
}
}
}

View File

@ -0,0 +1,28 @@
using System.Collections.Generic;
using Orchard.ContentManagement;
namespace Orchard.Services {
public interface IHtmlFilterProcessor : IDependency {
string ProcessFilters(string text, HtmlFilterContext context);
}
public static class HtmlFilterProcessorExtensions {
public static string ProcessFilters(
this IHtmlFilterProcessor processor,
string text,
string flavor,
IDictionary<string, object> data) =>
processor.ProcessFilters(text, new HtmlFilterContext { Flavor = flavor, Data = data });
public static string ProcessFilters(this IHtmlFilterProcessor processor, string text, string flavor) =>
processor.ProcessFilters(text, new HtmlFilterContext { Flavor = flavor });
public static string ProcessFilters(this IHtmlFilterProcessor processor, string text, string flavor, IContent content) =>
processor.ProcessFilters(
text,
new HtmlFilterContext {
Flavor = flavor,
Data = new Dictionary<string, object> { { "Content", content.ContentItem } }
});
}
}