All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kreuzberg.FlatHtml.scala Maven / Gradle / Ivy

There is a newer version: 0.10.3
Show newest version
package kreuzberg

import scala.collection.mutable

sealed trait FlatHtmlElement

object FlatHtmlElement {
  case class Part(s: String)             extends FlatHtmlElement
  case class PlaceHolder(id: Identifier) extends FlatHtmlElement
}

/** Flattened HTML generated from a component for faster rerendering. */
class FlatHtml(parts: Array[FlatHtmlElement]) {

  /** Rerender Flat HTML. */
  def render(builder: StringBuilder, nodeRender: (Identifier, StringBuilder) => Unit): Unit = {
    parts.foreach {
      case FlatHtmlElement.Part(s)        => builder.append(s)
      case FlatHtmlElement.PlaceHolder(n) => nodeRender(n, builder)
    }
  }

  def renderWithoutPlaceholders(sb: StringBuilder): Unit = {
    val nodeRender: (Identifier, StringBuilder) => Unit = { (id, sb) =>
      sb ++= s""
    }
    render(sb, nodeRender)
  }

  def renderWithoutPlaceholders(): String = {
    val sb = StringBuilder()
    renderWithoutPlaceholders(sb)
    sb.toString()
  }

  override def toString(): String = {
    renderWithoutPlaceholders()
  }
}

object FlatHtml {
  def apply(parts: Seq[FlatHtmlElement]): FlatHtml = new FlatHtml(parts.toArray)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy