76 lines
1.9 KiB
C#
76 lines
1.9 KiB
C#
// /**
|
|
// * File: ElementReader.cs
|
|
// * Author: haraldwolff
|
|
// *
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
// *
|
|
// *
|
|
// **/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
namespace ln.templates.html
|
|
{
|
|
public class ElementReader : HtmlReader
|
|
{
|
|
Stack<Element> elements = new Stack<Element>();
|
|
|
|
public Element CurrentElement => elements.Peek();
|
|
public virtual DocumentElement Document { get; }
|
|
|
|
public ElementReader()
|
|
{
|
|
Document = CreateDocument();
|
|
elements.Push(Document);
|
|
}
|
|
|
|
public virtual DocumentElement CreateDocument() => new DocumentElement();
|
|
|
|
public virtual Element CreateElement(string tagName)
|
|
{
|
|
switch (tagName.ToUpper())
|
|
{
|
|
default:
|
|
return new Element(tagName);
|
|
}
|
|
}
|
|
public virtual Element CreateTextElement(string text) => new TextElement(text);
|
|
|
|
void Push(Element element)
|
|
{
|
|
if (CurrentElement.IsVoid)
|
|
elements.Pop();
|
|
|
|
CurrentElement.AppendChild(element);
|
|
elements.Push(element);
|
|
}
|
|
|
|
void Pop()
|
|
{
|
|
if (CurrentElement.IsVoid)
|
|
elements.Pop();
|
|
|
|
elements.Pop();
|
|
}
|
|
|
|
public override void OpenTag(string tagName)
|
|
{
|
|
Push(CreateElement(tagName));
|
|
}
|
|
public override void CloseTag(string tagName)
|
|
{
|
|
Pop();
|
|
}
|
|
public override void Attribute(string attributeName, string attributeValue)
|
|
{
|
|
CurrentElement.SetAttribute(attributeName, attributeValue);
|
|
}
|
|
public override void Text(string text)
|
|
{
|
|
CurrentElement.AppendChild(
|
|
CreateTextElement(text)
|
|
);
|
|
}
|
|
}
|
|
}
|