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
29 private static void RunIteration(BadHtmlContext context, string name, BadObject obj)
30 {
31 using BadExecutionContext loopContext = new BadExecutionContext(
33 "bs:while",
34 context.ExecutionContext.Scope,
35 null,
36 BadScopeFlags.Breakable | BadScopeFlags.Continuable
37 )
38 );
39 loopContext.Scope.DefineVariable(name, obj, context.ExecutionContext.Scope);
40
41 foreach (HtmlNode? child in context.InputNode.ChildNodes)
42 {
43 BadHtmlContext childContext = context.CreateChild(child, context.OutputNode, loopContext);
44
45 Transform(childContext);
46 }
47 }
48
50 protected override void TransformNode(BadHtmlContext context)
51 {
52 HtmlAttribute? enumerationAttribute = context.InputNode.Attributes["on"];
53 HtmlAttribute? itemAttribute = context.InputNode.Attributes["as"];
54
55 if (enumerationAttribute == null)
56 {
58 context.ExecutionContext.Scope,
59 "Missing 'on' attribute in 'bs:each' node",
60 context.CreateOuterPosition()
61 );
62 }
63
64 if (itemAttribute == null)
65 {
67 context.ExecutionContext.Scope,
68 "Missing 'as' attribute in 'bs:each' node",
69 context.CreateOuterPosition()
70 );
71 }
72
73 if (string.IsNullOrEmpty(enumerationAttribute.Value))
74 {
76 context.ExecutionContext.Scope,
77 "Empty 'on' attribute in 'bs:each' node",
78 context.CreateAttributePosition(enumerationAttribute)
79 );
80 }
81
82 if (string.IsNullOrEmpty(itemAttribute.Value))
83 {
85 context.ExecutionContext.Scope,
86 "Empty 'as' attribute in 'bs:each' node",
87 context.CreateAttributePosition(itemAttribute)
88 );
89 }
90
91 BadObject enumeration = context.ParseAndExecuteSingle(
92 enumerationAttribute.Value,
93 context.CreateAttributePosition(enumerationAttribute)
94 );
95
96 switch (enumeration)
97 {
98 case IBadEnumerable badEnumerable:
99 {
100 foreach (BadObject o in badEnumerable)
101 {
102 RunIteration(context, itemAttribute.Value, o);
103 }
104
105 break;
106 }
107 case IBadEnumerator badEnumerator:
108 {
109 while (badEnumerator.MoveNext())
110 {
112 context,
113 itemAttribute.Value,
114 badEnumerator.Current ??
116 context.ExecutionContext.Scope,
117 "Enumerator returned null",
118 context.CreateAttributePosition(enumerationAttribute)
119 )
120 );
121 }
122
123 break;
124 }
125 case IEnumerable enumerable:
126 {
127 foreach (object? o in enumerable)
128 {
129 RunIteration(context, itemAttribute.Value, BadObject.Wrap(o));
130 }
131
132 break;
133 }
134 case IEnumerator enumerator:
135 {
136 while (enumerator.MoveNext())
137 {
139 context,
140 itemAttribute.Value,
141 BadObject.Wrap(enumerator.Current)
142 );
143 }
144
145 break;
146 }
147 default:
149 context.ExecutionContext.Scope,
150 "Result of 'on' attribute in 'bs:each' node is not enumerable",
151 context.CreateAttributePosition(enumerationAttribute)
152 );
153 }
154 }
155}
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)
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:792
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.