Skip to content

Commit

Permalink
Class name support.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Dec 10, 2023
1 parent 80dae90 commit 79e79ba
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions text/Squidex.Text.Tests/RichText/RichTextInlineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ public void Should_render_code()
<code>Text1</code>");
}

[Fact]
public void Should_render_class_name()
{
var source = new Node
{
Type = NodeType.Text,
Text = "Text1",
Marks =
[
new Mark
{
Type = MarkType.ClassName,
Attributes = new Attributes
{
["className"] = "text-left"
}
},
]
};

RenderUtils.AssertNode(source,
expectedMarkdown: @"
Text1",
expectedFormattedHtml: @"
<span class=""__editor_text-left"">Text1</span>",
expectedCompressedHtml: @"
<span class=""__editor_text-left"">Text1</span>");
}

[Fact]
public void Should_render_nested()
{
Expand Down
8 changes: 8 additions & 0 deletions text/Squidex.Text/RichText/HtmlWriterVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ protected override void VisitParagraph(INode node)
static s => s.self.VisitChildren(s.node));
}

protected override void VisitClassName(IMark mark, Action inner, string className)
{
attributes.Add(("class", $"__editor_{className}"));

RenderInline(inner, "span",
static a => a());
}

protected override void VisitBold(IMark mark, Action inner)
{
RenderInline(inner, "strong",
Expand Down
1 change: 1 addition & 0 deletions text/Squidex.Text/RichText/Model/MarkType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum MarkType
{
Undefined,
Bold,
ClassName,
Code,
Italic,
Link,
Expand Down
9 changes: 9 additions & 0 deletions text/Squidex.Text/RichText/Visitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ private void VisitMark(IMark mark)
mark.GetStringAttr("target"),
"noopener noreferrer nofollow");
break;
case MarkType.ClassName:
VisitClassName(mark, visitInner,
mark.GetStringAttr("className"));
break;
case MarkType.Bold:
VisitBold(mark, visitInner);
break;
Expand All @@ -88,6 +92,11 @@ protected virtual void VisitBold(IMark mark, Action inner)
inner();
}

protected virtual void VisitClassName(IMark mark, Action inner, string className)
{
inner();
}

protected virtual void VisitCode(IMark mark, Action inner)
{
inner();
Expand Down

0 comments on commit 79e79ba

Please sign in to comment.