BadScript 2
Loading...
Searching...
No Matches
BadTextNodeTransformer.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3
6
7namespace BadHtml.Transformer;
8
14{
16 protected override bool CanTransform(BadHtmlContext context)
17 {
18 return context.InputNode.Name == "#text";
19 }
20
22 protected override void TransformNode(BadHtmlContext context)
23 {
24 if (context.InputNode.InnerText.All(x => char.IsWhiteSpace(x) || x == '\n' || x == '\r' || x == '\t'))
25 {
26 if (!context.Options.SkipEmptyTextNodes)
27 {
28 context.OutputNode.AppendChild(context.OutputDocument.CreateTextNode(context.InputNode.InnerText));
29 }
30
31 return;
32 }
33
34 //Get Text and Replace all newlines with spaces
35 string text = "$@\"" + context.InputNode.InnerText + "\";";
36
37 //Evaluate Text with BadScript
38 BadObject result = context.ParseAndExecute(text, context.CreateInnerPosition());
39
40 if (result is not IBadString resultStr)
41 {
42 Console.WriteLine("Warning: Text node is not a string");
43 context.OutputNode.AppendChild(context.OutputDocument.CreateTextNode(result.ToString()));
44
45 return;
46 }
47
48 //Append Text to output
49 context.OutputNode.AppendChild(context.OutputDocument.CreateTextNode(resultStr.Value));
50 }
51}
Implements the Html Context for the Transformation Process.
readonly BadHtmlTemplateOptions Options
The Html Template Options.
HtmlDocument OutputDocument
The Output Document.
BadObject ParseAndExecute(string code, BadSourcePosition pos)
Parses and executes the specified code.
BadSourcePosition CreateInnerPosition()
Creates the Source Position of the current Input Nodes Inner Content.
readonly HtmlNode InputNode
The Current Input Node.
readonly HtmlNode OutputNode
The Current Output Node.
Base class of all Node transformers.
bool SkipEmptyTextNodes
If true, empty Html Text Nodes will be omitted from the output.
Implements a BadScript Text Node Transformer Copies the text nodes to the output and evaluates them w...
override void TransformNode(BadHtmlContext context)
override bool CanTransform(BadHtmlContext context)
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:210
Implements the Interface for Native Strings.
Definition IBadString.cs:7
Implementations of Html Node Transformers that are used in the Transformation Process.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10