Skip to content

Commit 76a6c61

Browse files
authored
Merge pull request #19 from 2sic/develop
03.11
2 parents c022cdc + e70835c commit 76a6c61

File tree

9 files changed

+140
-66
lines changed

9 files changed

+140
-66
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
4+
using ToSic.Razor.Internals.Documentation;
5+
6+
namespace ToSic.Razor.Internals
7+
{
8+
/// <summary>
9+
/// Made public to enable consistent use in 2sxc, but can still change at any time so not really public
10+
/// </summary>
11+
[PrivateApi]
12+
public class UriHelpers
13+
{
14+
public static string UriEncode(string url)
15+
{
16+
if (string.IsNullOrEmpty(url)) return url;
17+
18+
// more difficult case: %-character detected
19+
// maybe we want it - like in ?title=I want 25%
20+
// but most likely it's already encoded - like when an umlaut was already converted
21+
// check if it has any existing encode sequences
22+
// if it does, don't encode again
23+
if (url.Contains("%")
24+
&& Regex.IsMatch(url, @"(?:%[0-9A-Fa-f]{2})"))
25+
return url;
26+
27+
// simple use case - no % character in url
28+
// or the % wasn't just used for encoding
29+
// so just perform standard encoding
30+
return Uri.EscapeUriString(url).Replace("'", "%27");
31+
}
32+
33+
public static string UriEncodeSrcSet(string srcset)
34+
{
35+
if (string.IsNullOrWhiteSpace(srcset)) return srcset;
36+
37+
// split items and drop new-lines and leading/trailing spaces
38+
var items = srcset
39+
.Split(',')
40+
.Select(s => Blade.Text.Nl2X(s, "").Trim())
41+
.Select(CleanASrcSetLine);
42+
43+
// If the original had line breaks, add them in again
44+
// This is not 100% identical to the original, but in most cases it will be identical
45+
var nl = Environment.NewLine;
46+
var noNewLine = !srcset.Contains(nl);
47+
var prefix = noNewLine ? "" : srcset.StartsWith(nl) ? nl : "";
48+
var suffix = noNewLine ? "" : srcset.EndsWith(nl) ? nl : "";
49+
var reJoiner = noNewLine ? "," : "," + nl;
50+
51+
return prefix + string.Join(reJoiner, items) + suffix;
52+
53+
string CleanASrcSetLine(string line)
54+
{
55+
var matches = Regex.Matches(line, @"^(.*)(\s\d+[xw])$");
56+
// we expect 3 hits, #0 is everything, #1 the first hit and #2 the resolution
57+
if (matches.Count == 0 || matches[0].Groups.Count != 3) return UriEncode(line);
58+
59+
var url = matches[0].Groups[1].Value;
60+
var ext = matches[0].Groups[2].Value;
61+
return UriEncode(url) + ext;
62+
}
63+
}
64+
65+
}
66+
}
Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,15 @@
1-
using System;
2-
using System.Linq;
3-
using System.Text.RegularExpressions;
1+
using ToSic.Razor.Internals;
2+
using ToSic.Razor.Internals.Documentation;
43

54
namespace ToSic.Razor.Markup
65
{
76
public partial class TagBase
87
{
9-
internal static string UriEncode(string url)
10-
{
11-
if (string.IsNullOrEmpty(url)) return url;
8+
[PrivateApi("Should remove soon, after auto-generated code is updated")]
9+
internal static string UriEncode(string url) => UriHelpers.UriEncode(url);
1210

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

22-
// simple use case - no % character in url
23-
// or the % wasn't just used for encoding
24-
// so just perform standard encoding
25-
return Uri.EscapeUriString(url).Replace("'", "%27");
26-
}
27-
28-
internal static string UriEncodeSrcSet(string srcSet)
29-
{
30-
if (string.IsNullOrWhiteSpace(srcSet)) return srcSet;
31-
32-
// split items and drop new-lines and leading/trailing spaces
33-
var items = srcSet
34-
.Split(',')
35-
.Select(s => Blade.Text.Nl2X(s, "").Trim())
36-
.Select(CleanASrcSetLine);
37-
38-
return string.Join(",", items);
39-
40-
string CleanASrcSetLine(string line)
41-
{
42-
var matches = Regex.Matches(line, @"^(.*)(\s\d+[xw])$");
43-
// we expect 3 hits, #0 is everything, #1 the first hit and #2 the resolution
44-
if (matches.Count == 0 || matches[0].Groups.Count != 3) return UriEncode(line);
45-
46-
var url = matches[0].Groups[1].Value;
47-
var ext = matches[0].Groups[2].Value;
48-
return UriEncode(url) + ext;
49-
}
50-
}
51-
52-
5314
}
5415
}

Razor.Blade/Razor.Blade.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net472;net5.0</TargetFrameworks>
55
<RootNamespace>ToSic.Razor</RootNamespace>
6-
<Version>03.10.00</Version>
6+
<Version>03.11.00</Version>
77
<AssemblyName>ToSic.Razor</AssemblyName>
88
<PackageId>ToSic.Razor</PackageId>
99
<Authors>ToSic.Razor</Authors>

ToSic.Razor.Dnn/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
//
3131
// You can specify all the values or you can default the Revision and Build Numbers
3232
// by using the '*' as shown below:
33-
[assembly: AssemblyVersion("03.10.0.00")]
34-
[assembly: AssemblyFileVersion("03.10.0.00")]
33+
[assembly: AssemblyVersion("03.11.0.00")]
34+
[assembly: AssemblyFileVersion("03.11.0.00")]

ToSic.Razor.Dnn/ToSic_Razor_Blade_Dnn.dnn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<dotnetnuke type="Package" version="5.0">
22
<packages>
3-
<package name="ToSic.RazorBlade" type="Library" version="03.10.00">
3+
<package name="ToSic.RazorBlade" type="Library" version="03.11.00">
44
<friendlyName>2sic RazorBlade</friendlyName>
55
<description>2sic RazorBlade</description>
66
<iconFile>icon.png</iconFile>

ToSic.RazorBladeTests/HtmlTagsTests/ImgTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public void ImgSrcSetUmlautsXorW()
115115
[TestMethod]
116116
public void ImgSrcSetUmlautsMultiline()
117117
{
118-
Is("<img srcset='L%C3%A9onie%20M%C3%BCller.jpg?w=17 2x,L%C3%A9onie%20M%C3%BCller.jpg?w=17 700w'>",
118+
Is(@"<img srcset='
119+
L%C3%A9onie%20M%C3%BCller.jpg?w=17 2x,
120+
L%C3%A9onie%20M%C3%BCller.jpg?w=17 700w'>",
119121
Tag.Img().Srcset(@"
120122
Léonie Müller.jpg?w=17 2x,
121123
Léonie Müller.jpg?w=17 700w"));
Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using ToSic.Razor.Internals;
23
using ToSic.Razor.Markup;
34

45
namespace ToSic.RazorBladeTests.InternalTests
56
{
67
[TestClass]
78
public class UriEncoding
89
{
10+
/// <summary>
11+
/// Test Accessor
12+
/// </summary>
13+
private string EncodeTestAccessor(string url) => UriHelpers.UriEncode(url);
14+
15+
/// <summary>
16+
/// Test Accessor
17+
/// </summary>
18+
private string EncodeSrcSetTestAccessor(string srcset) => UriHelpers.UriEncodeSrcSet(srcset);
19+
920
/// <summary>
1021
/// Verify that uri-encode works and leaves slashes etc. untouched
1122
/// </summary>
1223
[TestMethod]
1324
public void UriEncode()
1425
{
15-
Assert.AreEqual("hello", TagBase.UriEncode("hello"));
26+
Assert.AreEqual("hello", EncodeTestAccessor("hello"));
1627

17-
Assert.AreEqual("hell%C3%B6", TagBase.UriEncode("hellö"),
28+
Assert.AreEqual("hell%C3%B6", EncodeTestAccessor("hellö"),
1829
"check umlauts");
1930

20-
Assert.AreEqual("hello/there.aspx", TagBase.UriEncode("hello/there.aspx"),
31+
Assert.AreEqual("hello/there.aspx", EncodeTestAccessor("hello/there.aspx"),
2132
"check path parts");
2233

23-
Assert.AreEqual("hello?parameter=7", TagBase.UriEncode("hello?parameter=7"),
34+
Assert.AreEqual("hello?parameter=7", EncodeTestAccessor("hello?parameter=7"),
2435
"check params");
2536

26-
Assert.AreEqual("http://azing.org", TagBase.UriEncode("http://azing.org"),
37+
Assert.AreEqual("http://azing.org", EncodeTestAccessor("http://azing.org"),
2738
"check domains");
2839

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

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

35-
Assert.AreEqual("something#hashed", TagBase.UriEncode("something#hashed"),
46+
Assert.AreEqual("something#hashed", EncodeTestAccessor("something#hashed"),
3647
"check hash");
3748

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

4152

@@ -44,30 +55,64 @@ public void UriEncode()
4455
[TestMethod]
4556
public void Nulls()
4657
{
47-
Assert.AreEqual(null, TagBase.UriEncode(null));
58+
Assert.AreEqual(null, EncodeTestAccessor(null));
4859
}
4960

5061
[TestMethod]
5162
public void ResizeUrls()
5263
{
53-
Assert.AreEqual("img.jpg?w=200", TagBase.UriEncode("img.jpg?w=200"));
64+
Assert.AreEqual("img.jpg?w=200", EncodeTestAccessor("img.jpg?w=200"));
5465
}
5566

5667
[TestMethod]
5768
public void DontDoubleEncode()
5869
{
59-
Assert.AreEqual("%20", TagBase.UriEncode("%20"),
70+
Assert.AreEqual("%20", EncodeTestAccessor("%20"),
6071
"check don't re-encode");
6172

62-
Assert.AreEqual("hello%20there", TagBase.UriEncode(TagBase.UriEncode("hello there")));
73+
Assert.AreEqual("hello%20there", EncodeTestAccessor(EncodeTestAccessor("hello there")));
6374
}
6475

6576
[TestMethod]
6677
public void EncodePercentIfNotUsedForEncoding()
6778
{
68-
Assert.AreEqual("I-want-25%25-profit", TagBase.UriEncode("I-want-25%-profit"));
79+
Assert.AreEqual("I-want-25%25-profit", EncodeTestAccessor("I-want-25%-profit"));
6980

70-
Assert.AreEqual("25%25is%20enough", TagBase.UriEncode("25%is enough"));
81+
Assert.AreEqual("25%25is%20enough", EncodeTestAccessor("25%is enough"));
82+
}
83+
84+
[TestMethod]
85+
public void SrcSetUnchanged()
86+
{
87+
var original = "something.jpg 1x";
88+
Assert.AreEqual(original, EncodeSrcSetTestAccessor(original));
89+
}
90+
91+
[TestMethod]
92+
public void SrcSetUnchanged2()
93+
{
94+
var original = "something.jpg?test=true 1x";
95+
Assert.AreEqual(original, EncodeSrcSetTestAccessor(original));
96+
}
97+
98+
[TestMethod]
99+
public void SrcSetAmp()
100+
{
101+
var original = "something.jpg?x=1&y=z 1x";
102+
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
103+
}
104+
[TestMethod]
105+
public void SrcSetOneLine()
106+
{
107+
var original = "something.jpg?x=1&y=z 1x,something.jpg?x=1&y=z 2x";
108+
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
109+
}
110+
111+
[TestMethod]
112+
public void SrcSetMultiline()
113+
{
114+
var original = "something.jpg?x=1&y=z 1x,\nsomething.jpg?x=1&y=z 2x";
115+
Assert.AreEqual(original/*.Replace("&", "&amp;")*/, EncodeSrcSetTestAccessor(original));
71116
}
72117
}
73118
}

docs/api/ToSic.Razor.Markup.TagBase.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ <h3 id="implements">Implements</h3>
13191319
<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>
13201320
</li>
13211321
<li>
1322-
<a href="https://github.com/2sic/razor-blade/blob/develop/Razor.Blade/Markup/Tag/TagBase_UriEncode.cs/#L7" class="contribution-link">View Source</a>
1322+
<a href="https://github.com/2sic/razor-blade/blob/develop/Razor.Blade/Markup/Tag/TagBase_UriEncode.cs/#L6" class="contribution-link">View Source</a>
13231323
</li>
13241324
</ul>
13251325
</div>

docs/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@
16701670
"output": {
16711671
".html": {
16721672
"relative_path": "api/ToSic.Razor.Markup.TagBase.html",
1673-
"hash": "O9nNVY0nW29C30WtKmdcViqZeull9bJxR58aRijBYC8="
1673+
"hash": "o7OqEn6EPStayk1hzlBS+m3Wr3621J5Z9Zc93jGqwHs="
16741674
}
16751675
},
16761676
"is_incremental": false,

0 commit comments

Comments
 (0)