Skip to content

Commit

Permalink
Merge pull request #19 from 2sic/develop
Browse files Browse the repository at this point in the history
03.11
  • Loading branch information
iJungleboy authored Mar 10, 2022
2 parents c022cdc + e70835c commit 76a6c61
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 66 deletions.
66 changes: 66 additions & 0 deletions Razor.Blade/Internals/UriHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using ToSic.Razor.Internals.Documentation;

namespace ToSic.Razor.Internals
{
/// <summary>
/// Made public to enable consistent use in 2sxc, but can still change at any time so not really public
/// </summary>
[PrivateApi]
public class UriHelpers
{
public static string UriEncode(string url)
{
if (string.IsNullOrEmpty(url)) return url;

// more difficult case: %-character detected
// maybe we want it - like in ?title=I want 25%
// but most likely it's already encoded - like when an umlaut was already converted
// check if it has any existing encode sequences
// if it does, don't encode again
if (url.Contains("%")
&& Regex.IsMatch(url, @"(?:%[0-9A-Fa-f]{2})"))
return url;

// simple use case - no % character in url
// or the % wasn't just used for encoding
// so just perform standard encoding
return Uri.EscapeUriString(url).Replace("'", "%27");
}

public static string UriEncodeSrcSet(string srcset)
{
if (string.IsNullOrWhiteSpace(srcset)) return srcset;

// split items and drop new-lines and leading/trailing spaces
var items = srcset
.Split(',')
.Select(s => Blade.Text.Nl2X(s, "").Trim())
.Select(CleanASrcSetLine);

// If the original had line breaks, add them in again
// This is not 100% identical to the original, but in most cases it will be identical
var nl = Environment.NewLine;
var noNewLine = !srcset.Contains(nl);
var prefix = noNewLine ? "" : srcset.StartsWith(nl) ? nl : "";
var suffix = noNewLine ? "" : srcset.EndsWith(nl) ? nl : "";
var reJoiner = noNewLine ? "," : "," + nl;

return prefix + string.Join(reJoiner, items) + suffix;

string CleanASrcSetLine(string line)
{
var matches = Regex.Matches(line, @"^(.*)(\s\d+[xw])$");
// we expect 3 hits, #0 is everything, #1 the first hit and #2 the resolution
if (matches.Count == 0 || matches[0].Groups.Count != 3) return UriEncode(line);

var url = matches[0].Groups[1].Value;
var ext = matches[0].Groups[2].Value;
return UriEncode(url) + ext;
}
}

}
}
51 changes: 6 additions & 45 deletions Razor.Blade/Markup/Tag/TagBase_UriEncode.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,15 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using ToSic.Razor.Internals;
using ToSic.Razor.Internals.Documentation;

namespace ToSic.Razor.Markup
{
public partial class TagBase
{
internal static string UriEncode(string url)
{
if (string.IsNullOrEmpty(url)) return url;
[PrivateApi("Should remove soon, after auto-generated code is updated")]
internal static string UriEncode(string url) => UriHelpers.UriEncode(url);

// more difficult case: %-character detected
// maybe we want it - like in ?title=I want 25%
// but most likely it's already encoded - like when an umlaut was already converted
// check if it has any existing encode sequences
// if it does, don't encode again
if (url.Contains("%")
&& Regex.IsMatch(url, @"(?:%[0-9A-Fa-f]{2})"))
return url;
[PrivateApi("Should remove soon, after auto-generated code is updated")]
internal static string UriEncodeSrcSet(string srcset) => UriHelpers.UriEncodeSrcSet(srcset);

// simple use case - no % character in url
// or the % wasn't just used for encoding
// so just perform standard encoding
return Uri.EscapeUriString(url).Replace("'", "%27");
}

internal static string UriEncodeSrcSet(string srcSet)
{
if (string.IsNullOrWhiteSpace(srcSet)) return srcSet;

// split items and drop new-lines and leading/trailing spaces
var items = srcSet
.Split(',')
.Select(s => Blade.Text.Nl2X(s, "").Trim())
.Select(CleanASrcSetLine);

return string.Join(",", items);

string CleanASrcSetLine(string line)
{
var matches = Regex.Matches(line, @"^(.*)(\s\d+[xw])$");
// we expect 3 hits, #0 is everything, #1 the first hit and #2 the resolution
if (matches.Count == 0 || matches[0].Groups.Count != 3) return UriEncode(line);

var url = matches[0].Groups[1].Value;
var ext = matches[0].Groups[2].Value;
return UriEncode(url) + ext;
}
}


}
}
2 changes: 1 addition & 1 deletion Razor.Blade/Razor.Blade.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net472;net5.0</TargetFrameworks>
<RootNamespace>ToSic.Razor</RootNamespace>
<Version>03.10.00</Version>
<Version>03.11.00</Version>
<AssemblyName>ToSic.Razor</AssemblyName>
<PackageId>ToSic.Razor</PackageId>
<Authors>ToSic.Razor</Authors>
Expand Down
4 changes: 2 additions & 2 deletions ToSic.Razor.Dnn/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("03.10.0.00")]
[assembly: AssemblyFileVersion("03.10.0.00")]
[assembly: AssemblyVersion("03.11.0.00")]
[assembly: AssemblyFileVersion("03.11.0.00")]
2 changes: 1 addition & 1 deletion ToSic.Razor.Dnn/ToSic_Razor_Blade_Dnn.dnn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<dotnetnuke type="Package" version="5.0">
<packages>
<package name="ToSic.RazorBlade" type="Library" version="03.10.00">
<package name="ToSic.RazorBlade" type="Library" version="03.11.00">
<friendlyName>2sic RazorBlade</friendlyName>
<description>2sic RazorBlade</description>
<iconFile>icon.png</iconFile>
Expand Down
4 changes: 3 additions & 1 deletion ToSic.RazorBladeTests/HtmlTagsTests/ImgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ public void ImgSrcSetUmlautsXorW()
[TestMethod]
public void ImgSrcSetUmlautsMultiline()
{
Is("<img srcset='L%C3%A9onie%20M%C3%BCller.jpg?w=17 2x,L%C3%A9onie%20M%C3%BCller.jpg?w=17 700w'>",
Is(@"<img srcset='
L%C3%A9onie%20M%C3%BCller.jpg?w=17 2x,
L%C3%A9onie%20M%C3%BCller.jpg?w=17 700w'>",
Tag.Img().Srcset(@"
Léonie Müller.jpg?w=17 2x,
Léonie Müller.jpg?w=17 700w"));
Expand Down
73 changes: 59 additions & 14 deletions ToSic.RazorBladeTests/InternalTests/UriEncoding.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ToSic.Razor.Internals;
using ToSic.Razor.Markup;

namespace ToSic.RazorBladeTests.InternalTests
{
[TestClass]
public class UriEncoding
{
/// <summary>
/// Test Accessor
/// </summary>
private string EncodeTestAccessor(string url) => UriHelpers.UriEncode(url);

/// <summary>
/// Test Accessor
/// </summary>
private string EncodeSrcSetTestAccessor(string srcset) => UriHelpers.UriEncodeSrcSet(srcset);

/// <summary>
/// Verify that uri-encode works and leaves slashes etc. untouched
/// </summary>
[TestMethod]
public void UriEncode()
{
Assert.AreEqual("hello", TagBase.UriEncode("hello"));
Assert.AreEqual("hello", EncodeTestAccessor("hello"));

Assert.AreEqual("hell%C3%B6", TagBase.UriEncode("hellö"),
Assert.AreEqual("hell%C3%B6", EncodeTestAccessor("hellö"),
"check umlauts");

Assert.AreEqual("hello/there.aspx", TagBase.UriEncode("hello/there.aspx"),
Assert.AreEqual("hello/there.aspx", EncodeTestAccessor("hello/there.aspx"),
"check path parts");

Assert.AreEqual("hello?parameter=7", TagBase.UriEncode("hello?parameter=7"),
Assert.AreEqual("hello?parameter=7", EncodeTestAccessor("hello?parameter=7"),
"check params");

Assert.AreEqual("http://azing.org", TagBase.UriEncode("http://azing.org"),
Assert.AreEqual("http://azing.org", EncodeTestAccessor("http://azing.org"),
"check domains");

Assert.AreEqual("http://azing.org/homepage?name=m", "http://azing.org/homepage?name=m",
"check full url");

Assert.AreEqual("http://azing.org/home%20page?name=m", TagBase.UriEncode("http://azing.org/home page?name=m"),
Assert.AreEqual("http://azing.org/home%20page?name=m", EncodeTestAccessor("http://azing.org/home page?name=m"),
"check spaces");

Assert.AreEqual("something#hashed", TagBase.UriEncode("something#hashed"),
Assert.AreEqual("something#hashed", EncodeTestAccessor("something#hashed"),
"check hash");

Assert.AreEqual("url+plus", TagBase.UriEncode("url+plus"),
Assert.AreEqual("url+plus", EncodeTestAccessor("url+plus"),
"check plus in url = it's a space, and shouldn't be re-encode");


Expand All @@ -44,30 +55,64 @@ public void UriEncode()
[TestMethod]
public void Nulls()
{
Assert.AreEqual(null, TagBase.UriEncode(null));
Assert.AreEqual(null, EncodeTestAccessor(null));
}

[TestMethod]
public void ResizeUrls()
{
Assert.AreEqual("img.jpg?w=200", TagBase.UriEncode("img.jpg?w=200"));
Assert.AreEqual("img.jpg?w=200", EncodeTestAccessor("img.jpg?w=200"));
}

[TestMethod]
public void DontDoubleEncode()
{
Assert.AreEqual("%20", TagBase.UriEncode("%20"),
Assert.AreEqual("%20", EncodeTestAccessor("%20"),
"check don't re-encode");

Assert.AreEqual("hello%20there", TagBase.UriEncode(TagBase.UriEncode("hello there")));
Assert.AreEqual("hello%20there", EncodeTestAccessor(EncodeTestAccessor("hello there")));
}

[TestMethod]
public void EncodePercentIfNotUsedForEncoding()
{
Assert.AreEqual("I-want-25%25-profit", TagBase.UriEncode("I-want-25%-profit"));
Assert.AreEqual("I-want-25%25-profit", EncodeTestAccessor("I-want-25%-profit"));

Assert.AreEqual("25%25is%20enough", TagBase.UriEncode("25%is enough"));
Assert.AreEqual("25%25is%20enough", EncodeTestAccessor("25%is enough"));
}

[TestMethod]
public void SrcSetUnchanged()
{
var original = "something.jpg 1x";
Assert.AreEqual(original, EncodeSrcSetTestAccessor(original));
}

[TestMethod]
public void SrcSetUnchanged2()
{
var original = "something.jpg?test=true 1x";
Assert.AreEqual(original, EncodeSrcSetTestAccessor(original));
}

[TestMethod]
public void SrcSetAmp()
{
var original = "something.jpg?x=1&y=z 1x";
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
}
[TestMethod]
public void SrcSetOneLine()
{
var original = "something.jpg?x=1&y=z 1x,something.jpg?x=1&y=z 2x";
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
}

[TestMethod]
public void SrcSetMultiline()
{
var original = "something.jpg?x=1&y=z 1x,\nsomething.jpg?x=1&y=z 2x";
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
}
}
}
2 changes: 1 addition & 1 deletion docs/api/ToSic.Razor.Markup.TagBase.html
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ <h3 id="implements">Implements</h3>
<a href="https://github.com/2sic/razor-blade/new/develop/apiSpec/new?filename=ToSic_Razor_Markup_TagBase.md&amp;value=---%0Auid%3A%20ToSic.Razor.Markup.TagBase%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A" class="contribution-link">Improve this Doc</a>
</li>
<li>
<a href="https://github.com/2sic/razor-blade/blob/develop/Razor.Blade/Markup/Tag/TagBase_UriEncode.cs/#L7" class="contribution-link">View Source</a>
<a href="https://github.com/2sic/razor-blade/blob/develop/Razor.Blade/Markup/Tag/TagBase_UriEncode.cs/#L6" class="contribution-link">View Source</a>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,7 @@
"output": {
".html": {
"relative_path": "api/ToSic.Razor.Markup.TagBase.html",
"hash": "O9nNVY0nW29C30WtKmdcViqZeull9bJxR58aRijBYC8="
"hash": "o7OqEn6EPStayk1hzlBS+m3Wr3621J5Z9Zc93jGqwHs="
}
},
"is_incremental": false,
Expand Down

0 comments on commit 76a6c61

Please sign in to comment.