Skip to content

Commit

Permalink
T
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Dec 8, 2023
1 parent 6dbea8c commit 3a4234b
Show file tree
Hide file tree
Showing 23 changed files with 713 additions and 211 deletions.
26 changes: 26 additions & 0 deletions text/Squidex.Text.Tests/RichText/ComplexText.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Header</h1>
<p>Content with <strong>bold</strong>, <em>italic</em>, <u>underline</u> and <code spellcheck="false">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>
<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 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>
240 changes: 240 additions & 0 deletions text/Squidex.Text.Tests/RichText/ComplexText.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
{
"type": "doc",
"content": [
{
"type": "heading",
"attrs": {
"level": 1
},
"content": [
{
"type": "text",
"text": "Header"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Content with "
},
{
"type": "text",
"marks": [
{
"type": "bold"
}
],
"text": "bold"
},
{
"type": "text",
"text": ", "
},
{
"type": "text",
"marks": [
{
"type": "italic"
}
],
"text": "italic"
},
{
"type": "text",
"text": ", "
},
{
"type": "text",
"marks": [
{
"type": "underline"
}
],
"text": "underline"
},
{
"type": "text",
"text": " and "
},
{
"type": "text",
"marks": [
{
"type": "code"
}
],
"text": "code"
},
{
"type": "text",
"text": "."
}
]
},
{
"type": "blockquote",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Quote"
}
]
}
]
},
{
"type": "codeBlock",
"attrs": {
"language": "javascript",
"wrap": false
},
"content": [
{
"type": "text",
"text": "Code Block in Javascript"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Just another paragraph"
}
]
},
{
"type": "horizontalRule"
},
{
"type": "bulletList",
"content": [
{
"type": "listItem",
"attrs": {
"closed": false,
"nested": false
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Item 1"
}
]
}
]
},
{
"type": "listItem",
"attrs": {
"closed": false,
"nested": false
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Item 2"
}
]
}
]
}
]
},
{
"type": "orderedList",
"attrs": {
"order": 1
},
"content": [
{
"type": "listItem",
"attrs": {
"closed": false,
"nested": false
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Item A"
}
]
}
]
},
{
"type": "listItem",
"attrs": {
"closed": false,
"nested": false
},
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "Item B"
}
]
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"marks": [
{
"type": "link",
"attrs": {
"href": "Link Content",
"target": null,
"auto": false
}
}
],
"text": "A link"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "image",
"attrs": {
"alt": "",
"crop": null,
"height": null,
"width": null,
"rotate": null,
"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",
"fileName": null,
"resizable": false
}
}
]
}
]
}
27 changes: 27 additions & 0 deletions text/Squidex.Text.Tests/RichText/ComplexText.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Header

Content with **bold**, *italic*, underline and `code`.

> Quote
```javascript
Code Block in Javascript
```

Just another paragraph

---

* Item 1

* Item 2


1. Item A

2. Item B


[A link](Link Content)

![](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 "My Image")
20 changes: 20 additions & 0 deletions text/Squidex.Text.Tests/RichText/Json/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ public static bool TryGetEnum<T>(this object value, out T enumValue) where T : s
return value is string text && Enum.TryParse(text, true, out enumValue);
}

public static int GetIntAttr(this JsonObject? attrs, string name, int defaultValue = 0)
{
if (attrs?.TryGetValue(name, out var value) == true && value is int attr)
{
return attr;
}

return defaultValue;
}

public static string GetStringAttr(this JsonObject? attrs, string name, string defaultValue = "")
{
if (attrs?.TryGetValue(name, out var value) == true && value is string attr)
{
return attr;
}

return defaultValue;
}

public static bool TryGetArrayOfObject(this object value, out JsonArray array)
{
array = default!;
Expand Down
30 changes: 8 additions & 22 deletions text/Squidex.Text.Tests/RichText/Json/JsonMark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

namespace Squidex.RichText.Json;

internal sealed class JsonMark : MarkBase
internal sealed class JsonMark : IMark
{
private JsonObject? attrs;
private MarkType type;

public MarkType Type { get; private set; }

public bool TryUse(JsonObject source)
{
Expand All @@ -24,7 +25,7 @@ public bool TryUse(JsonObject source)
switch (key)
{
case "type" when value.TryGetEnum<MarkType>(out var type):
this.type = type;
Type = type;
break;
case "attrs" when value is JsonObject attrs:
this.attrs = attrs;
Expand All @@ -38,28 +39,13 @@ public bool TryUse(JsonObject source)
return isValid;
}

public override MarkType GetMarkType()
public int GetIntAttr(string name, int defaultValue = 0)
{
return type;
}

public override int GetIntAttr(string name, int defaultValue = 0)
{
if (attrs?.TryGetValue(name, out var value) == true && value is int attr)
{
return attr;
}

return defaultValue;
return attrs.GetIntAttr(name, defaultValue);
}

public override string GetStringAttr(string name, string defaultValue = "")
public string GetStringAttr(string name, string defaultValue = "")
{
if (attrs?.TryGetValue(name, out var value) == true && value is string attr)
{
return attr;
}

return defaultValue;
return attrs.GetStringAttr(name, defaultValue);
}
}
Loading

0 comments on commit 3a4234b

Please sign in to comment.