-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomRenderOptionMock.cs
More file actions
92 lines (88 loc) · 4.19 KB
/
CustomRenderOptionMock.cs
File metadata and controls
92 lines (88 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using Contentstack.Utils.Interfaces;
using Contentstack.Utils.Models;
using HtmlAgilityPack;
namespace Contentstack.Utils.Tests.Mocks
{
public class CustomRenderOptionMock : Options
{
public CustomRenderOptionMock(IEntryEmbedable entry) : base(entry)
{
}
public override string RenderNode(string nodeType, Node node, NodeChildrenCallBack callBack)
{
string GetAttrString(string key)
{
if (!node.attrs.ContainsKey(key) || node.attrs[key] == null) return "";
var val = node.attrs[key];
if (val is string s) return s;
if (val is System.Text.Json.JsonElement je && je.ValueKind == System.Text.Json.JsonValueKind.String) return je.GetString();
return val.ToString();
}
switch (nodeType)
{
case "a":
if (node.attrs.ContainsKey("target"))
{
return $"<a href=\"{GetAttrString("url")}\" target=\"{GetAttrString("target")}\">{callBack(node.children)}</a>";
}
return $"<a href=\"{GetAttrString("url")}\">{callBack(node.children)}</a>";
}
return base.RenderNode(nodeType, node, callBack);
}
public override string RenderOption(IEmbeddedObject embeddedObject, Metadata metadata)
{
var attributeStringList = new List<string>();
foreach (var attribute in (HtmlAttributeCollection)metadata.attributes)
{
attributeStringList.Add($" {attribute.Name}=\"{attribute.Value}\"");
}
var attributeString = string.Join(" ", attributeStringList);
switch (metadata.StyleType)
{
case Enums.StyleType.Block:
string renderString = "";
if (embeddedObject is IEmbeddedEntry)
{
renderString += $"<div {attributeString}> <b>{((IEmbeddedEntry)embeddedObject).Title}</b></div>";
}
else
{
renderString += $"<div {attributeString}> <b>{embeddedObject.Uid}</b></div>";
}
return renderString;
case Enums.StyleType.Inline:
if (embeddedObject is IEmbeddedEntry)
{
return $"<span {attributeString}><b>{((IEmbeddedEntry)embeddedObject).Title}</b></span>";
}
else
{
return $"<span {attributeString}><b>{embeddedObject.Uid}</b></span>";
}
case Enums.StyleType.Link:
if (embeddedObject is IEmbeddedEntry)
{
return $"<span> Please find link to: <a {attributeString}><b>{metadata.Text ?? ((IEmbeddedEntry)embeddedObject).Title}</b></a></span>";
}
else
{
return $"<span> Please find link to: <a {attributeString}><b>{metadata.Text ?? embeddedObject.Uid}</b></a></span>";
}
case Enums.StyleType.Display:
if (embeddedObject is IEmbeddedAsset)
{
return $"<b>{((IEmbeddedAsset)embeddedObject).Title}</b><p>{((IEmbeddedAsset)embeddedObject).FileName} image: <img {attributeString} /></p>";
}
return "<img src=\"" + embeddedObject.Uid + "\" alt=\"" + embeddedObject.Uid + "\" />";
case Enums.StyleType.Download:
if (embeddedObject is IEmbeddedAsset)
{
return "<span> Please find link to: <a href=\"" + ((IEmbeddedAsset)embeddedObject).Url + "\">" + (metadata.Text ?? ((IEmbeddedAsset)embeddedObject).Title) + "</a></span>";
}
return "<a href=\"" + embeddedObject.Uid + "\">" + (metadata.Text ?? embeddedObject.Uid) + "</a>";
}
return base.RenderOption(embeddedObject, metadata);
}
}
}