forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageFragment.cs
More file actions
261 lines (217 loc) · 9.63 KB
/
PageFragment.cs
File metadata and controls
261 lines (217 loc) · 9.63 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public abstract class PageFragment {}
public class PageVariableFragment : PageFragment
{
public ReadOnlyMemory<char> OriginalText { get; set; }
private ReadOnlyMemory<byte> originalTextUtf8;
public ReadOnlyMemory<byte> OriginalTextUtf8 => originalTextUtf8.IsEmpty ? (originalTextUtf8 = OriginalText.ToUtf8()) : OriginalTextUtf8;
public JsToken Expression { get; }
public string Binding { get; }
public object InitialValue { get; }
public JsCallExpression InitialExpression { get; }
public JsCallExpression[] FilterExpressions { get; }
internal string LastFilterName { get; }
public PageVariableFragment(ReadOnlyMemory<char> originalText, JsToken expr, List<JsCallExpression> filterCommands)
{
OriginalText = originalText;
Expression = expr;
FilterExpressions = filterCommands?.ToArray() ?? TypeConstants<JsCallExpression>.EmptyArray;
if (expr is JsLiteral initialValue)
{
InitialValue = initialValue.Value;
}
else if (ReferenceEquals(expr, JsNull.Value))
{
InitialValue = expr;
}
else if (expr is JsCallExpression initialExpr)
{
InitialExpression = initialExpr;
}
else if (expr is JsIdentifier initialBinding)
{
Binding = initialBinding.Name;
}
if (FilterExpressions.Length > 0)
{
LastFilterName = FilterExpressions[FilterExpressions.Length - 1].Name;
}
}
public object Evaluate(ScriptScopeContext scope)
{
if (ReferenceEquals(Expression, JsNull.Value))
return Expression;
return Expression.Evaluate(scope);
}
protected bool Equals(PageVariableFragment other)
{
return OriginalText.Span.SequenceEqual(other.OriginalText.Span)
&& Equals(Expression, other.Expression)
&& FilterExpressions.EquivalentTo(other.FilterExpressions);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PageVariableFragment) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = OriginalText.GetHashCode();
hashCode = (hashCode * 397) ^ (Expression != null ? Expression.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (FilterExpressions != null ? FilterExpressions.GetHashCode() : 0);
return hashCode;
}
}
}
public class PageStringFragment : PageFragment
{
public ReadOnlyMemory<char> Value { get; set; }
private string valueString;
public string ValueString => valueString ?? (valueString = Value.ToString());
private ReadOnlyMemory<byte> valueUtf8;
public ReadOnlyMemory<byte> ValueUtf8 => valueUtf8.IsEmpty ? (valueUtf8 = Value.ToUtf8()) : valueUtf8;
public PageStringFragment(string value) : this(value.AsMemory()) {}
public PageStringFragment(ReadOnlyMemory<char> value) => Value = value;
protected bool Equals(PageStringFragment other)
{
return Value.Span.SequenceEqual(other.Value.Span);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PageStringFragment) obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
public class PageBlockFragment : PageFragment
{
public ReadOnlyMemory<char> OriginalText { get; internal set; }
public string Name { get; }
public ReadOnlyMemory<char> Argument { get; }
private string argumentString;
public string ArgumentString => argumentString ?? (argumentString = Argument.ToString());
public PageFragment[] Body { get; }
public PageElseBlock[] ElseBlocks { get; }
public PageBlockFragment(string originalText, string name, string argument,
JsStatement body, IEnumerable<PageElseBlock> elseStatements=null)
: this (originalText.AsMemory(), name, argument.AsMemory(), new PageFragment[]{ new PageJsBlockStatementFragment(new JsBlockStatement(body)) }, elseStatements) {}
public PageBlockFragment(string originalText, string name, string argument,
JsBlockStatement body, IEnumerable<PageElseBlock> elseStatements=null)
: this (originalText.AsMemory(), name, argument.AsMemory(), new PageFragment[]{ new PageJsBlockStatementFragment(body) }, elseStatements) {}
public PageBlockFragment(string originalText, string name, string argument,
List<PageFragment> body, List<PageElseBlock> elseStatements=null)
: this (originalText.AsMemory(), name, argument.AsMemory(), body, elseStatements) {}
public PageBlockFragment(string name, ReadOnlyMemory<char> argument,
List<PageFragment> body, List<PageElseBlock> elseStatements=null)
: this(default, name, argument, body, elseStatements) {}
public PageBlockFragment(ReadOnlyMemory<char> originalText, string name, ReadOnlyMemory<char> argument,
IEnumerable<PageFragment> body, IEnumerable<PageElseBlock> elseStatements=null)
{
OriginalText = originalText;
Name = name;
Argument = argument;
Body = body.ToArray();
ElseBlocks = elseStatements?.ToArray() ?? TypeConstants<PageElseBlock>.EmptyArray;
}
protected bool Equals(PageBlockFragment other)
{
return Name == other.Name &&
Argument.Span.SequenceEqual(other.Argument.Span) &&
Body.EquivalentTo(other.Body) &&
ElseBlocks.EquivalentTo(other.ElseBlocks);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PageBlockFragment) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (Name != null ? Name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Argument.GetHashCode();
hashCode = (hashCode * 397) ^ (Body != null ? Body.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (ElseBlocks != null ? ElseBlocks.GetHashCode() : 0);
return hashCode;
}
}
}
public class PageElseBlock : PageFragment
{
public ReadOnlyMemory<char> Argument { get; }
public PageFragment[] Body { get; }
public PageElseBlock(string argument, List<PageFragment> body) : this(argument.AsMemory(), body) {}
public PageElseBlock(string argument, JsStatement statement)
: this(argument, new JsBlockStatement(statement)) {}
public PageElseBlock(string argument, JsBlockStatement block)
: this(argument.AsMemory(), new PageFragment[]{ new PageJsBlockStatementFragment(block) }) {}
public PageElseBlock(ReadOnlyMemory<char> argument, IEnumerable<PageFragment> body) : this(argument, body.ToArray()) {}
public PageElseBlock(ReadOnlyMemory<char> argument, PageFragment[] body)
{
Argument = argument;
Body = body;
}
protected bool Equals(PageElseBlock other)
{
return Argument.Span.SequenceEqual(other.Argument.Span) &&
Body.EquivalentTo(other.Body);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PageElseBlock) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Argument.GetHashCode();
hashCode = (hashCode * 397) ^ (Body != null ? Body.GetHashCode() : 0);
return hashCode;
}
}
}
public class PageJsBlockStatementFragment : PageFragment
{
public JsBlockStatement Block { get; }
public bool Quiet { get; set; }
public PageJsBlockStatementFragment(JsBlockStatement statement)
{
Block = statement;
}
protected bool Equals(PageJsBlockStatementFragment other)
{
return Equals(Block, other.Block);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((PageJsBlockStatementFragment) obj);
}
public override int GetHashCode()
{
return (Block != null ? Block.GetHashCode() : 0);
}
}
}