github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/htmlrender/dom/writer.go (about)

     1  package dom
     2  
     3  type Writer interface {
     4  	Open(tag string)
     5  	Attr(name, value string)
     6  	Text(text string)
     7  	Close(tag string)
     8  
     9  	Wrap(r RendererExtended) func()
    10  	Render(r Renderer)
    11  	RenderAll(rs ...Renderer)
    12  
    13  	Bytes() []byte
    14  	String() string
    15  }
    16  
    17  type UnsafeWriter interface {
    18  	Writer
    19  	CloseAttributes()
    20  	UnsafeWrite(text string)
    21  	UnsafeContent(text string)
    22  }
    23  
    24  type Renderer interface {
    25  	Render(w Writer)
    26  }
    27  
    28  type RendererExtended interface {
    29  	Renderer
    30  	RenderOpen(w Writer)
    31  	RenderClose(w Writer)
    32  }
    33  
    34  type RenderFunc func(w Writer)
    35  
    36  func (fn RenderFunc) Render(w Writer) { fn(w) }
    37  
    38  func (w *writer) Wrap(r RendererExtended) func() {
    39  	r.RenderOpen(w)
    40  	return func() {
    41  		r.RenderClose(w)
    42  	}
    43  }
    44  
    45  func (w *writer) Render(r Renderer) {
    46  	r.Render(w)
    47  }
    48  
    49  func (w *writer) RenderAll(rs ...Renderer) {
    50  	for _, r := range rs {
    51  		r.Render(w)
    52  	}
    53  }