BadScript 2
Loading...
Searching...
No Matches
BadComponentNodeTransformer.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
9
10using HtmlAgilityPack;
11
13
20{
22 protected override bool CanTransform(BadHtmlContext context)
23 {
24 string? nodeName = context.InputNode.OriginalName;
25
26 if (context.ExecutionContext.Scope.HasVariable(nodeName, context.ExecutionContext.Scope))
27 {
28 BadObject componentDefinition = context.ExecutionContext.Scope.GetVariable(nodeName)
29 .Dereference(context.CreateOuterPosition());
30
31 if (componentDefinition is BadFunction function)
32 {
33 return true;
34 }
35 }
36
37 return false;
38 }
39
41 protected override void TransformNode(BadHtmlContext context)
42 {
43 string? nodeName = context.InputNode.OriginalName;
44
45 BadObject componentDefinition = context.ExecutionContext.Scope.GetVariable(nodeName)
46 .Dereference(context.CreateOuterPosition());
47 BadFunction function = (BadFunction)componentDefinition;
48
49 HtmlAttribute[] attributes = context.InputNode.Attributes.ToArray();
50
51 string[] indexMap = function.Parameters.Select(x => x.Name)
52 .ToArray();
53 Dictionary<string, BadObject> arguments = new Dictionary<string, BadObject>();
54
55 foreach (HtmlAttribute attribute in attributes)
56 {
57 int index = Array.IndexOf(indexMap, attribute.OriginalName);
58
59 if (index != -1)
60 {
61 BadObject arg = context.ParseAndExecuteSingle(attribute.Value,
62 context.CreateAttributePosition(attribute)
63 );
64 arguments[attribute.OriginalName] = arg;
65 }
66 }
67
68 List<BadObject> args = new List<BadObject>();
69
70 foreach (string argName in indexMap)
71 {
72 args.Add(arguments.TryGetValue(argName, out BadObject? argument) ? argument : BadObject.Null);
73 }
74
75 BadObject result = BadObject.Null;
76
77 foreach (BadObject o in function.Invoke(args.ToArray(), context.ExecutionContext))
78 {
79 result = o;
80 }
81
82 result = result.Dereference(context.CreateOuterPosition());
83
84 if (result is not IBadString str)
85 {
86 throw BadRuntimeException.Create(context.ExecutionContext.Scope,
87 "Component must return a string",
88 context.CreateOuterPosition()
89 );
90 }
91
92 context.OutputNode.AppendChild(HtmlNode.CreateNode(str.Value));
93 }
94}
Implements the Html Context for the Transformation Process.
BadSourcePosition CreateOuterPosition()
Creates the Source Position of the current Input Nodes Outer Content.
readonly HtmlNode InputNode
The Current Input Node.
readonly BadExecutionContext ExecutionContext
The Execution Context that is used to evaluate badscript code.
Base class of all Node transformers.
A transformer that allows the user to invoke a function by specifying the function namd and its argum...
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements a function that can be called from the script.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Error Objects for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10