BadScript 2
Loading...
Searching...
No Matches
BadInsertNodeTransformer.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2
5
6using HtmlAgilityPack;
7
8namespace BadHtml.Transformer;
9
14{
16 protected override bool CanTransform(BadHtmlContext context)
17 {
18 return context.InputNode.Name == "bs:insert";
19 }
20
21
29 private static IEnumerable<HtmlNode> GetNodes(BadHtmlContext context, string path, bool global)
30 {
31 if (path.StartsWith("#"))
32 {
33 yield return context.OutputDocument.GetElementbyId(path.Remove(0, 1));
34 }
35 else
36 {
37 HtmlNode? root = global ? context.OutputDocument.DocumentNode : context.OutputNode;
38
39 foreach (HtmlNode node in root.SelectNodes(path))
40 {
41 yield return node;
42 }
43 }
44 }
45
47 protected override void TransformNode(BadHtmlContext context)
48 {
49 HtmlAttribute? pathAttribute = context.InputNode.Attributes["into"];
50 bool isGlobal = context.InputNode.Attributes["global"]?.Value != "false";
51
52 if (pathAttribute == null)
53 {
55 context.ExecutionContext.Scope,
56 "Missing 'into' attribute in 'bs:insert' node",
57 context.CreateOuterPosition()
58 );
59 }
60
61 string? path = pathAttribute.Value;
62
63 if (string.IsNullOrEmpty(path))
64 {
66 context.ExecutionContext.Scope,
67 "Empty 'into' attribute in 'bs:insert' node",
68 context.CreateAttributePosition(pathAttribute)
69 );
70 }
71
72
73 foreach (HtmlNode outputNode in GetNodes(context, path, isGlobal))
74 {
75 if (outputNode == null)
76 {
77 continue;
78 }
79
80 foreach (HtmlNode node in context.InputNode.ChildNodes)
81 {
82 using BadExecutionContext exCtx = new BadExecutionContext(context.ExecutionContext.Scope.CreateChild("bs:insert", context.ExecutionContext.Scope, null));
83 BadHtmlContext ctx = context.CreateChild(
84 node,
85 outputNode,
86 exCtx
87 );
88 Transform(ctx);
89 }
90 }
91 }
92}
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.
HtmlDocument OutputDocument
The Output Document.
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.
readonly HtmlNode OutputNode
The Current Output Node.
Base class of all Node transformers.
static void Transform(BadHtmlContext context)
Transforms the input node with one of the registered transformers.
Implements the bs:insert node transformer. That inserts the inner content of the node into the specif...
static IEnumerable< HtmlNode > GetNodes(BadHtmlContext context, string path, bool global)
Returns all nodes that match the specified path.
override bool CanTransform(BadHtmlContext context)
override void TransformNode(BadHtmlContext context)
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Implementation.