public HTMLElement(HTMLBuilder builder, string elementName, bool canBeEmptyElement) { this.builder = builder; this.elementName = elementName; this.canBeEmptyElement = canBeEmptyElement; this.CurrentState = State.Start; builder.Text($"<{elementName}"); }
private void SetCurrentState(State newState) { switch (newState) { case State.Attributes: switch (CurrentState) { case State.ChildElements: throw new Exception("Can't add HTML element attributes after a child element has been added."); case State.ChildText: throw new Exception("Can't add HTML element attributes after element text has been added."); case State.End: throw new Exception("Can't add HTML element attributes after the element has been closed."); } break; case State.ChildElements: switch (CurrentState) { case State.Start: case State.Attributes: builder.Line(">"); break; case State.End: throw new Exception("Can't add HTML child elements after the element has been closed."); } break; case State.ChildText: switch (CurrentState) { case State.Start: case State.Attributes: builder.Text(">"); break; case State.End: throw new Exception("Can't add HTML child elements after the element has been closed."); } break; case State.End: switch (CurrentState) { case State.Start: case State.Attributes: if (canBeEmptyElement) { builder.Line("/>"); } else { builder.Line($"></{elementName}>"); } break; case State.ChildElements: case State.ChildText: builder.Line($"</{elementName}>"); break; } break; } CurrentState = newState; }