Skip to content

Commit

Permalink
Conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Dec 9, 2023
1 parent 3a4234b commit 80dae90
Show file tree
Hide file tree
Showing 21 changed files with 654 additions and 580 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/squidex/squidex</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>6.2.0</Version>
<Version>6.3.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
8 changes: 5 additions & 3 deletions text/Squidex.Text.Tests/RichText/ComplexText.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<h1>Header</h1>
<p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u> and <code spellcheck="false">code</code>.</p>
<p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u> and <code>code</code>.</p>
<blockquote>
<p>Quote</p>
</blockquote>
<pre spellcheck="false" class="language-javascript"><code data-code-block-language="javascript">Code Block in Javascript</code></pre>
<pre class="language-javascript">
<code data-code-block-language="javascript">Code Block in Javascript</code>
</pre>
<p>Just another paragraph</p>
<hr>
<ul>
Expand All @@ -23,4 +25,4 @@ <h1>Header</h1>
</li>
</ol>
<p><a href="Link Content" rel="noopener noreferrer nofollow">A link</a></p>
<p><img alt="" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Adams_The_Tetons_and_the_Snake_River.jpg/1280px-Adams_The_Tetons_and_the_Snake_River.jpg" title="My Image" resizable="false"> </p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Adams_The_Tetons_and_the_Snake_River.jpg/1280px-Adams_The_Tetons_and_the_Snake_River.jpg" title="My Image"></p>
12 changes: 4 additions & 8 deletions text/Squidex.Text.Tests/RichText/ComplexText.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ Just another paragraph

---

* Item 1

* Item 2


1. Item A

2. Item B
* Item 1
* Item 2

1. Item A
2. Item B

[A link](Link Content)

Expand Down
1 change: 1 addition & 0 deletions text/Squidex.Text.Tests/RichText/ComplexText.min.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Header</h1><p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u> and <code>code</code>.</p><blockquote><p>Quote</p></blockquote><pre class="language-javascript"><code data-code-block-language="javascript">Code Block in Javascript</code></pre><p>Just another paragraph</p><hr><ul><li><p>Item 1</p></li><li><p>Item 2</p></li></ul><ol><li><p>Item A</p></li><li><p>Item B</p></li></ol><p><a href="Link Content" rel="noopener noreferrer nofollow">A link</a></p><p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Adams_The_Tetons_and_the_Snake_River.jpg/1280px-Adams_The_Tetons_and_the_Snake_River.jpg" title="My Image"></p>
10 changes: 5 additions & 5 deletions text/Squidex.Text.Tests/RichText/Json/JsonNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ public string GetStringAttr(string name, string defaultValue = "")

public void IterateContent<T>(T state, Action<INode, T, bool, bool> action)
{
if (currentState.Content == null)
var prevState = currentState;

if (prevState.Content == null)
{
return;
}

var prevState = currentState;

var i = 0;
foreach (var item in currentState.Content)
foreach (var item in prevState.Content)
{
var isFirst = i == 0;
var isLast = i == currentState.Content.Count - 1;
var isLast = i == prevState.Content.Count - 1;

TryUse((JsonObject)item, false);
action(this, state, isFirst, isLast);
Expand Down
33 changes: 24 additions & 9 deletions text/Squidex.Text.Tests/RichText/RenderUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using System.Text;
using Squidex.Text.RichText.Model;
using Xunit;

namespace Squidex.Text.RichText;

internal static class RenderUtils
{
public static (string Markdown, string Html) Render(INode node)
public static void AssertNode(INode node, string expectedMarkdown, string expectedFormattedHtml, string? expectedCompressedHtml)
{
return (RenderMarkdown(node), RenderHtml(node));
var (markdown, htmlFormatted, htmlCompressed) = Render(node);

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);
Assert.Equal(expectedFormattedHtml.TrimExpected(), htmlFormatted);

if (expectedCompressedHtml != null)
{
Assert.Equal(expectedCompressedHtml.TrimExpected(), htmlCompressed);
}
}

public static (string Markdown, string Html, string CompressedHtml) Render(INode node)
{
return (RenderMarkdown(node), RenderHtml(node), RenderHtml(node, 0));
}

public static string TrimExpected(this string result)
Expand All @@ -25,21 +40,21 @@ public static string TrimExpected(this string result)
return result;
}

public static string RenderHtml(INode node)
public static string RenderHtml(INode node, int indentation = 4)
{
var htmlString = new StringWriter();
var sb = new StringBuilder();

HtmlWriterVisitor.Render(node, htmlString);
HtmlWriterVisitor.Render(node, sb, new HtmlWriterOptions { Indentation = indentation });

return htmlString.ToString().TrimExpected();
return sb.ToString().TrimExpected();
}

public static string RenderMarkdown(INode node)
{
var markdownString = new StringWriter();
var sb = new StringBuilder();

MarkdownVisitor.Render(node, markdownString);
MarkdownVisitor.Render(node, sb);

return markdownString.ToString().TrimExpected();
return sb.ToString().TrimExpected();
}
}
93 changes: 32 additions & 61 deletions text/Squidex.Text.Tests/RichText/RichTextComplexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Squidex.RichText.Json;
using Squidex.Text.RichText.Model;
using Xunit;
using static Google.Rpc.Context.AttributeContext.Types;

namespace Squidex.Text.RichText;

Expand Down Expand Up @@ -99,37 +100,26 @@ public void Should_render_complex_state()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
RenderUtils.AssertNode(source,
expectedMarkdown: @"
Paragraph1
Paragraph2
* **Item1**
* Item2
* Item3";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<p>
Paragraph1
</p>
* Item3",
expectedFormattedHtml: @"
<p>Paragraph1</p>
<p>
Paragraph2<ul>
<li>
<strong>Item1</strong>
</li>
<li>
Item2
</li>
<li>
Item3
</li>
Paragraph2
<ul>
<li><strong>Item1</strong></li>
<li>Item2</li>
<li>Item3</li>
</ul>
</p>";

Assert.Equal(expectedHtml.TrimExpected(), html);
</p>",
expectedCompressedHtml: @"
<p>Paragraph1</p><p>Paragraph2<ul><li><strong>Item1</strong></li><li>Item2</li><li>Item3</li></ul></p>");
}

[Fact]
Expand Down Expand Up @@ -218,42 +208,28 @@ public void Should_render_complex_state_from_json()
};

var node = new JsonNode();
node.TryUse(source, false);

var expectedMarkdown = @"
RenderUtils.AssertNode(node,
expectedMarkdown: @"
Paragraph1
Paragraph2
* **Item1**
* Item2
* Item3";

node.TryUse(source, false);
var markdown = RenderUtils.RenderMarkdown(node);

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
* Item3",
expectedFormattedHtml: @"
<p>Paragraph1</p>
<p>
Paragraph1
</p>
<p>
Paragraph2<ul>
<li>
<strong>Item1</strong>
</li>
<li>
Item2
</li>
<li>
Item3
</li>
Paragraph2
<ul>
<li><strong>Item1</strong></li>
<li>Item2</li>
<li>Item3</li>
</ul>
</p>";

node.TryUse(source, false);
var html = RenderUtils.RenderHtml(node);

Assert.Equal(expectedHtml.TrimExpected(), html);
</p>",
expectedCompressedHtml: @"
<p>Paragraph1</p><p>Paragraph2<ul><li><strong>Item1</strong></li><li>Item2</li><li>Item3</li></ul></p>");
}

[Fact]
Expand All @@ -262,14 +238,9 @@ public void Should_render_from_files()
var inputJson = File.ReadAllText("RichText/ComplexText.json");
var inputNode = JsonSerializer.Deserialize<Node>(inputJson)!;

var (markdown, html) = RenderUtils.Render(inputNode);

Assert.Equal(
File.ReadAllText("RichText/ComplexText.md").TrimExpected(),
markdown);

Assert.Equal(
File.ReadAllText("RichText/ComplexText.html").TrimExpected(),
html);
RenderUtils.AssertNode(inputNode,
File.ReadAllText("RichText/ComplexText.md"),
File.ReadAllText("RichText/ComplexText.html"),
File.ReadAllText("RichText/ComplexText.min.html"));
}
}
90 changes: 35 additions & 55 deletions text/Squidex.Text.Tests/RichText/RichTextInlineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public void Should_render_bold()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
**Text1**";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<strong>Text1</strong>";

Assert.Equal(expectedHtml.TrimExpected(), html);
RenderUtils.AssertNode(source,
expectedMarkdown: @"
**Text1**",
expectedFormattedHtml: @"
<strong>Text1</strong>",
expectedCompressedHtml: @"
<strong>Text1</strong>");
}

[Fact]
Expand All @@ -57,17 +53,13 @@ public void Should_render_italic()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
*Text1*";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<em>Text1</em>";

Assert.Equal(expectedHtml.TrimExpected(), html);
RenderUtils.AssertNode(source,
expectedMarkdown: @"
*Text1*",
expectedFormattedHtml: @"
<em>Text1</em>",
expectedCompressedHtml: @"
<em>Text1</em>");
}

[Fact]
Expand All @@ -86,17 +78,13 @@ public void Should_render_underline()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
Text1";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<u>Text1</u>";

Assert.Equal(expectedHtml.TrimExpected(), html);
RenderUtils.AssertNode(source,
expectedMarkdown: @"
Text1",
expectedFormattedHtml: @"
<u>Text1</u>",
expectedCompressedHtml: @"
<u>Text1</u>");
}

[Fact]
Expand All @@ -115,17 +103,13 @@ public void Should_render_code()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
`Text1`";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<code>Text1</code>";

Assert.Equal(expectedHtml.TrimExpected(), html);
RenderUtils.AssertNode(source,
expectedMarkdown: @"
`Text1`",
expectedFormattedHtml: @"
<code>Text1</code>",
expectedCompressedHtml: @"
<code>Text1</code>");
}

[Fact]
Expand Down Expand Up @@ -156,16 +140,12 @@ public void Should_render_nested()
]
};

var (markdown, html) = RenderUtils.Render(source);

var expectedMarkdown = @"
***`Text1`***";

Assert.Equal(expectedMarkdown.TrimExpected(), markdown);

var expectedHtml = @"
<strong><u><em><code>Text1</code></em></u></strong>";

Assert.Equal(expectedHtml.TrimExpected(), html);
RenderUtils.AssertNode(source,
expectedMarkdown: @"
***`Text1`***",
expectedFormattedHtml: @"
<strong><u><em><code>Text1</code></em></u></strong>",
expectedCompressedHtml: @"
<strong><u><em><code>Text1</code></em></u></strong>");
}
}
Loading

0 comments on commit 80dae90

Please sign in to comment.