Skip to content

Commit

Permalink
implement impvored scrub on string #3146
Browse files Browse the repository at this point in the history
  • Loading branch information
iJungleboy committed Aug 15, 2023
1 parent 85b2efc commit e09bfb7
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Src/Sxc/ToSic.Sxc/Context/Query/Parameters_Typed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bo
=> GetV(name, noParamOrder: noParamOrder, fallback: fallback);

[PrivateApi]
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
{
var value = GetV(name, noParamOrder: noParamOrder, fallback: fallback);
#pragma warning disable CS0618
return scrubHtml ? Tags.Strip(value) : value;
#pragma warning restore CS0618
if (scrubHtml != default)
throw new NotSupportedException($"{nameof(scrubHtml)} is not supported on this object");
return value;
}

[PrivateApi]
Expand Down
9 changes: 6 additions & 3 deletions Src/Sxc/ToSic.Sxc/Data/Dynamic/CodeItemHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using ToSic.Eav.Plumbing;
using ToSic.Razor.Blade;
using ToSic.Razor.Markup;
using ToSic.Sxc.Data.Typed;
using static ToSic.Eav.Parameters;
using static ToSic.Sxc.Data.Typed.TypedHelpers;

Expand Down Expand Up @@ -67,12 +69,13 @@ public IRawHtmlString Attribute(string name, string noParamOrder, string fallbac
return strValue is null ? null : new RawHtmlString(WebUtility.HtmlEncode(strValue));
}

public string String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
public string String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml = default)
{
var value = G4T(name, noParamOrder: noParamOrder, fallback: fallback, required: required);
return scrubHtml ? Helper.Cdf.Services.Scrub.All(value) : value;
return TypedItemHelpers.MaybeScrub(value, scrubHtml, () => Helper.Cdf.Services.Scrub);
}


public string Url(string name, string noParamOrder, string fallback, bool? required)
{
// TODO: STRICT
Expand Down
2 changes: 1 addition & 1 deletion Src/Sxc/ToSic.Sxc/Data/DynamicEntity/TypedItemOfEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bo
=> ItemHelper.G4T(name, noParamOrder: noParamOrder, fallback: fallback, required: required);

[PrivateApi]
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
=> ItemHelper.String(name, noParamOrder, fallback, required, scrubHtml);

[PrivateApi]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bo
=> G4T(name, noParamOrder: noParamOrder, fallback: fallback);

[PrivateApi]
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
{
var value = G4T(name, noParamOrder: noParamOrder, fallback: fallback);
#pragma warning disable CS0618
return scrubHtml ? Tags.Strip(value) : value;
#pragma warning restore CS0618
return TypedItemHelpers.MaybeScrub(value, scrubHtml, () => Wrapper.Cdf.Value.Services.Scrub);
//#pragma warning disable CS0618
// return scrubHtml ? Tags.Strip(value) : value;
//#pragma warning restore CS0618
}

[PrivateApi]
Expand Down
2 changes: 1 addition & 1 deletion Src/Sxc/ToSic.Sxc/Data/Metadata/Metadata_Typed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bo
=> ItemHelper.G4T(name, noParamOrder: noParamOrder, fallback: fallback, required: required);

[PrivateApi]
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
=> ItemHelper.String(name, noParamOrder, fallback, required, scrubHtml);

[PrivateApi]
Expand Down
2 changes: 1 addition & 1 deletion Src/Sxc/ToSic.Sxc/Data/Stack/TypedStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bo
=> _itemHelper.G4T(name, noParamOrder: noParamOrder, fallback: fallback, required: required);

[PrivateApi]
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
=> _itemHelper.String(name, noParamOrder, fallback, required, scrubHtml);

[PrivateApi]
Expand Down
20 changes: 19 additions & 1 deletion Src/Sxc/ToSic.Sxc/Data/Typed/TypedItemHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ToSic.Eav.Plumbing;
using System.Linq;
using System;
using ToSic.Eav.Plumbing;
using ToSic.Razor.Blade;
using ToSic.Sxc.Images;
using static ToSic.Eav.Parameters;
Expand Down Expand Up @@ -47,5 +49,21 @@ object recipe
: kit.Image.Picture(field, settings: settings, factor: factor, width: width, imgAlt: imgAlt, imgAltFallback: imgAltFallback, imgClass: imgClass, recipe: recipe);
}

public static string MaybeScrub(string value, object scrubHtml, Func<IScrub> scrubSvc)
{
if (value == null) return null;
switch (scrubHtml)
{
case string scrubStr:
return scrubStr.HasValue()
? scrubSvc().Only(value, scrubStr.Split(',').Select(s => s.Trim()).ToArray())
: value;
case bool scrubBln:
return scrubBln ? scrubSvc().All(value) : value;
default:
return value;
}
}

}
}
10 changes: 5 additions & 5 deletions Src/Sxc/ToSic.Sxc/Data/Typed/WrapObjectTyped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ bool ITyped.Bool(string name, string noParamOrder, bool fallback, bool? required
DateTime ITyped.DateTime(string name, string noParamOrder, DateTime fallback, bool? required)
=> PreWrap.TryGet(name, noParamOrder: noParamOrder, fallback: fallback, required: required);

string ITyped.String(string name, string noParamOrder, string fallback, bool? required, bool scrubHtml)
string ITyped.String(string name, string noParamOrder, string fallback, bool? required, object scrubHtml)
{
var value = PreWrap.TryGet(name, noParamOrder: noParamOrder, fallback: fallback, required: required);
#pragma warning disable CS0618
return scrubHtml ? Tags.Strip(value) : value;
#pragma warning restore CS0618

return TypedItemHelpers.MaybeScrub(value, scrubHtml, () => Wrapper.Cdf.Value.Services.Scrub);
//#pragma warning disable CS0618
// return scrubHtml ? Tags.Strip(value) : value;
//#pragma warning restore CS0618
}

int ITyped.Int(string name, string noParamOrder, int fallback, bool? required)
Expand Down
8 changes: 6 additions & 2 deletions Src/Sxc/ToSic.Sxc/Data/TypedInterfaces/ITyped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public partial interface ITyped
/// <param name="noParamOrder">see [](xref:NetCode.Conventions.NamedParameters)</param>
/// <param name="fallback">_optional_ fallback if conversion fails</param>
/// <param name="required">throw error if the `name` doesn't exist, see [](xref:NetCode.Conventions.TypedRequired)</param>
/// <param name="scrubHtml">If true, will remove all HTML tags from the string. This is the same as using `Kit.Scrub.All(...)`. For more detailed scrubbing, use the `Kit.Scrub`</param>
/// <param name="scrubHtml">
/// If `true`, will remove all HTML tags from the string.
/// If `p` will remove all `p` tags, if `div,span` will remove these tags.
/// This is the same as using `Kit.Scrub.All(...)` or `.Only(...). For more detailed scrubbing, use the `Kit.Scrub`
/// </param>
/// <returns>Value as `string`</returns>
string String(string name, string noParamOrder = Protector, string fallback = default, bool? required = default, bool scrubHtml = default);
string String(string name, string noParamOrder = Protector, string fallback = default, bool? required = default, object scrubHtml = default);

#region Numbers

Expand Down
6 changes: 3 additions & 3 deletions Src/Sxc/ToSic.Sxc/Data/Wrapper/CodeDataWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace ToSic.Sxc.Data.Wrapper
[PrivateApi]
public class CodeDataWrapper: ServiceBase
{
private readonly LazySvc<CodeDataFactory> _cdf;
public readonly LazySvc<CodeDataFactory> Cdf;

public CodeDataWrapper(LazySvc<ConvertForCodeService> forCodeConverter, LazySvc<CodeDataFactory> cdf): base("Sxc.DWrpFk")
{
ConnectServices(
_forCodeConverter = forCodeConverter,
_cdf = cdf
Cdf = cdf
);
}

Expand Down Expand Up @@ -54,7 +54,7 @@ public ITyped TypedFromObject(object data, WrapperSettings settings)
public ITypedItem TypedItemFromObject(object data, WrapperSettings settings, ILazyLike<CodeDataFactory> cdf = default)
{
var preWrap = new PreWrapObject(data, settings, this);
return new WrapObjectTypedItem(preWrap, this, cdf ?? _cdf);
return new WrapObjectTypedItem(preWrap, this, cdf ?? Cdf);
}

/// <summary>
Expand Down

0 comments on commit e09bfb7

Please sign in to comment.