Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6193: IHtmlFilter and TokenFilter improvements and bugfixes #6938

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Orchard.Web/Core/Common/Drivers/BodyPartDriver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web;
using Orchard.Mvc.Html;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
Expand All @@ -15,13 +13,13 @@

namespace Orchard.Core.Common.Drivers {
public class BodyPartDriver : ContentPartDriver<BodyPart> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterRunner _htmlFilterRunner;
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, IHtmlFilterRunner htmlFilterRunner, RequestContext requestContext) {
_htmlFilterRunner = htmlFilterRunner;
Services = services;
_requestContext = requestContext;
}
Expand All @@ -36,12 +34,12 @@ protected override DriverResult Display(BodyPart part, string displayType, dynam
return Combined(
ContentShape("Parts_Common_Body",
() => {
var bodyText = _htmlFilters.Aggregate(part.Text, (text, filter) => filter.ProcessContent(text, GetFlavor(part)));
var bodyText = _htmlFilterRunner.RunFilters(part.Text, GetFlavor(part), 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)));
var bodyText = _htmlFilterRunner.RunFilters(part.Text, GetFlavor(part), part);
return shapeHelper.Parts_Common_Body_Summary(Html: new HtmlString(bodyText));
})
);
Expand Down
9 changes: 4 additions & 5 deletions src/Orchard.Web/Core/Common/Drivers/TextFieldDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Orchard.Core.Common.Drivers {
public class TextFieldDriver : ContentFieldDriver<TextField> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterRunner _htmlFilterRunner;

public TextFieldDriver(IOrchardServices services, IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
public TextFieldDriver(IOrchardServices services, IHtmlFilterRunner htmlFilterRunner) {
_htmlFilterRunner = htmlFilterRunner;
Services = services;
T = NullLocalizer.Instance;
}
Expand All @@ -36,8 +36,7 @@ protected override DriverResult Display(ContentPart part, TextField field, strin
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(_htmlFilterRunner.RunFilters(field.Value, settings.Flavor, part));
return shapeHelper.Fields_Common_Text(Name: field.Name, Value: fieldValue);
});
}
Expand Down
21 changes: 10 additions & 11 deletions src/Orchard.Web/Core/Common/Services/BbcodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,26 @@
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);
}
Expand Down Expand Up @@ -63,7 +62,7 @@ private static string BbcodeReplace(string text) {
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));
}
Expand All @@ -85,7 +84,7 @@ private static string BbcodeReplace(string text) {

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)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Orchard.Web/Core/Common/Services/TextFieldFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ namespace Orchard.Core.Feeds.StandardBuilders {
public class CorePartsFeedItemBuilder : IFeedItemBuilder {
private readonly IContentManager _contentManager;
private readonly RouteCollection _routes;
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
private readonly IHtmlFilterRunner _htmlFilterRunner;

public CorePartsFeedItemBuilder(
IContentManager contentManager,
RouteCollection routes,
IEnumerable<IHtmlFilter> htmlFilters) {
IHtmlFilterRunner htmlFilterRunner) {
_contentManager = contentManager;
_routes = routes;
_htmlFilters = htmlFilters;
_htmlFilterRunner = htmlFilterRunner;
}

public void Populate(FeedContext context) {
foreach (var feedItem in context.Response.Items.OfType<FeedItem<ContentItem>>()) {

var inspector = new ItemInspector(
feedItem.Item,
_contentManager.GetItemMetadata(feedItem.Item),
_htmlFilters);
_contentManager.GetItemMetadata(feedItem.Item),
_htmlFilterRunner);

// author is intentionally left empty as it could result in unwanted spam

Expand Down
12 changes: 6 additions & 6 deletions src/Orchard.Web/Core/Feeds/StandardBuilders/ItemInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 IHtmlFilterRunner _htmlFilterRunner;
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, IHtmlFilterRunner htmlFilterRunner) {
_item = item;
_metadata = metadata;
_htmlFilters = htmlFilters;
_htmlFilterRunner = htmlFilterRunner;
_common = item.Get<ICommonPart>();
_titleAspect = item.Get<ITitleAspect>();
_body = item.Get<BodyPart>();
Expand All @@ -49,8 +49,8 @@ public RouteValueDictionary Link {

public string Description {
get {
if (_body != null && !string.IsNullOrEmpty(_body.Text)) {
return _htmlFilters.Aggregate(_body.Text, (text, filter) => filter.ProcessContent(text, GetFlavor(_body)));
if (_htmlFilterRunner != null && _body != null && !string.IsNullOrEmpty(_body.Text)) {
return _htmlFilterRunner.RunFilters(_body.Text, GetFlavor(_body), _body);
}
return Title;
}
Expand Down
13 changes: 6 additions & 7 deletions src/Orchard.Web/Core/Feeds/StandardQueries/ContainerFeedQuery.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Xml.Linq;
using Orchard.ContentManagement;
Expand All @@ -8,16 +7,16 @@
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 IHtmlFilterRunner _htmlFilterRunner;

public ContainerFeedQuery(IContentManager contentManager, IEnumerable<IHtmlFilter> htmlFilters) {
public ContainerFeedQuery(IContentManager contentManager, IHtmlFilterRunner htmlFilterRunner) {
_contentManager = contentManager;
_htmlFilters = htmlFilters;
_htmlFilterRunner = htmlFilterRunner;
}

public FeedQueryMatch Match(FeedContext context) {
Expand Down Expand Up @@ -55,7 +54,7 @@ public void Execute(FeedContext context) {
return;
}

var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilters);
var inspector = new ItemInspector(container, _contentManager.GetItemMetadata(container), _htmlFilterRunner);
if (context.Format == "rss") {
var link = new XElement("link");
context.Response.Element.SetElementValue("title", inspector.Title);
Expand Down
14 changes: 7 additions & 7 deletions src/Orchard.Web/Modules/Markdown/Services/MarkdownFilter.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Web.ApplicationServices;
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;

var markdown = new MarkdownSharp.Markdown();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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;
Expand All @@ -23,8 +23,8 @@ public CloudVideoFilter(IShapeFactory shapeFactory, IContentManager contentManag
_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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 IHtmlFilterRunner _runner;

public HeadingElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public HeadingElementDriver(IHtmlFilterRunner runner) {
_runner = runner;
}

protected override EditorResult OnBuildEditor(Heading element, ElementEditorContext context) {
Expand All @@ -30,7 +30,7 @@ protected override EditorResult OnBuildEditor(Heading element, ElementEditorCont
}

protected override void OnDisplaying(Heading element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _runner.RunFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
context.ElementShape.Level = element.Level;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 IHtmlFilterRunner _runner;

public HtmlElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public HtmlElementDriver(IHtmlFilterRunner runner) {
_runner = runner;
}

protected override EditorResult OnBuildEditor(Html element, ElementEditorContext context) {
Expand All @@ -28,7 +29,7 @@ protected override EditorResult OnBuildEditor(Html element, ElementEditorContext
}

protected override void OnDisplaying(Html element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _runner.RunFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 IHtmlFilterRunner _runner;
public MarkdownElementDriver(IHtmlFilterRunner runner) {
_runner = runner;
}

protected override EditorResult OnBuildEditor(MarkdownElement element, ElementEditorContext context) {
Expand All @@ -29,7 +30,7 @@ protected override EditorResult OnBuildEditor(MarkdownElement element, ElementEd
}

protected override void OnDisplaying(MarkdownElement element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "markdown", context.GetTokenData());
context.ElementShape.ProcessedContent = _runner.RunFilters(element.Content, new HtmlFilterContext { Flavor = "markdown", Data = context.GetTokenData() });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 IHtmlFilterRunner _runner;

public ParagraphElementDriver(IElementFilterProcessor processor) {
_processor = processor;
public ParagraphElementDriver(IHtmlFilterRunner runner) {
_runner = runner;
}

protected override EditorResult OnBuildEditor(Paragraph element, ElementEditorContext context) {
Expand All @@ -28,7 +28,7 @@ protected override EditorResult OnBuildEditor(Paragraph element, ElementEditorCo
}

protected override void OnDisplaying(Paragraph element, ElementDisplayingContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.ProcessedContent = _runner.RunFilters(element.Content, new HtmlFilterContext { Flavor = "html", Data = context.GetTokenData() });
}
}
}
Loading