BadScript 2
Loading...
Searching...
No Matches
BadForEachNodeTransformer.cs
Go to the documentation of this file.
1using System.Collections;
2
7
8using HtmlAgilityPack;
9
10namespace BadHtml.Transformer;
11
16{
18 protected override bool CanTransform(BadHtmlContext context)
19 {
20 return context.InputNode.Name == "bs:each";
21 }
22
31 private static void RunIteration(BadHtmlContext context, string name, BadObject obj, string? indexName, int index)
32 {
33 using BadExecutionContext loopContext = new BadExecutionContext(
35 "bs:while",
36 context.ExecutionContext.Scope,
37 null,
38 BadScopeFlags.Breakable | BadScopeFlags.Continuable
39 )
40 );
41 loopContext.Scope.DefineVariable(name, obj, context.ExecutionContext.Scope);
42 if (!string.IsNullOrEmpty(indexName))
43 {
44 loopContext.Scope.DefineVariable(indexName!, index, context.ExecutionContext.Scope);
45 }
46
47 foreach (HtmlNode? child in context.InputNode.ChildNodes)
48 {
49 BadHtmlContext childContext = context.CreateChild(child, context.OutputNode, loopContext);
50
51 Transform(childContext);
52 }
53 }
54
56 protected override void TransformNode(BadHtmlContext context)
57 {
58 HtmlAttribute? enumerationAttribute = context.InputNode.Attributes["on"];
59 HtmlAttribute? itemAttribute = context.InputNode.Attributes["as"];
60 HtmlAttribute? indexNameAttribute = context.InputNode.Attributes["index"];
61
62 if (enumerationAttribute == null)
63 {
65 context.ExecutionContext.Scope,
66 "Missing 'on' attribute in 'bs:each' node",
67 context.CreateOuterPosition()
68 );
69 }
70
71 if (itemAttribute == null)
72 {
74 context.ExecutionContext.Scope,
75 "Missing 'as' attribute in 'bs:each' node",
76 context.CreateOuterPosition()
77 );
78 }
79
80 if (string.IsNullOrEmpty(enumerationAttribute.Value))
81 {
83 context.ExecutionContext.Scope,
84 "Empty 'on' attribute in 'bs:each' node",
85 context.CreateAttributePosition(enumerationAttribute)
86 );
87 }
88
89 if (string.IsNullOrEmpty(itemAttribute.Value))
90 {
92 context.ExecutionContext.Scope,
93 "Empty 'as' attribute in 'bs:each' node",
94 context.CreateAttributePosition(itemAttribute)
95 );
96 }
97
98 string? indexName = null;
99 if (indexNameAttribute != null)
100 {
101 indexName = indexNameAttribute.Value;
102 }
103
104 BadObject enumeration = context.ParseAndExecuteSingle(
105 enumerationAttribute.Value,
106 context.CreateAttributePosition(enumerationAttribute)
107 );
108
109 switch (enumeration)
110 {
111 case IBadEnumerable badEnumerable:
112 {
113 int i = 0;
114 foreach (BadObject o in badEnumerable)
115 {
116 RunIteration(context, itemAttribute.Value, o, indexName, i);
117 i++;
118 }
119
120 break;
121 }
122 case IBadEnumerator badEnumerator:
123 {
124 int i = 0;
125 while (badEnumerator.MoveNext())
126 {
128 context,
129 itemAttribute.Value,
130 badEnumerator.Current ??
132 context.ExecutionContext.Scope,
133 "Enumerator returned null",
134 context.CreateAttributePosition(enumerationAttribute)
135 ),
136 indexName,
137 i
138 );
139 i++;
140 }
141
142 break;
143 }
144 case IEnumerable enumerable:
145 {
146 int i = 0;
147 foreach (object? o in enumerable)
148 {
149 RunIteration(context, itemAttribute.Value, BadObject.Wrap(o), indexName, i);
150 i++;
151 }
152
153 break;
154 }
155 case IEnumerator enumerator:
156 {
157 int i = 0;
158 while (enumerator.MoveNext())
159 {
161 context,
162 itemAttribute.Value,
163 BadObject.Wrap(enumerator.Current),
164 indexName,
165 i
166 );
167 i++;
168 }
169
170 break;
171 }
172 default:
174 context.ExecutionContext.Scope,
175 "Result of 'on' attribute in 'bs:each' node is not enumerable",
176 context.CreateAttributePosition(enumerationAttribute)
177 );
178 }
179 }
180}
Implements the Html Context for the Transformation Process.
BadSourcePosition CreateAttributePosition(HtmlAttribute attribute)
Creates the Source Position of the specified Attribute.
BadSourcePosition CreateOuterPosition()
Creates the Source Position of the current Input Nodes Outer Content.
BadHtmlContext CreateChild(HtmlNode inputNode, HtmlNode outputNode, BadExecutionContext? executionContext=null)
Creates a child context with the specified input, output node and optional execution context.
readonly HtmlNode InputNode
The Current Input Node.
readonly BadExecutionContext ExecutionContext
The Execution Context that is used to evaluate badscript code.
BadObject ParseAndExecuteSingle(string code, BadSourcePosition pos)
Parses and executes the specified code and returns the result of the last expression.
Base class of all Node transformers.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Implements the 'bs:each' node transformer.
override bool CanTransform(BadHtmlContext context)
override void TransformNode(BadHtmlContext context)
static void RunIteration(BadHtmlContext context, string name, BadObject obj, string? indexName, int index)
Runs an iteration of the 'bs:each' node.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
BadScope CreateChild(string name, BadScope? caller, bool? useVisibility, BadScopeFlags flags=BadScopeFlags.RootScope)
Creates a subscope of the current scope.
Definition BadScope.cs:597
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
Defines a BadScript Enumerable.
Defines a BadScript Enumerator.
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Abstractions and Implementations for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.
BadScopeFlags
Defines Different Behaviours for the Current Scope.