Documentation
¶
Overview ¶
It is a binding for Rust crate lol_html. https://github.com/cloudflare/lol-html
Index ¶
- Variables
- func RewriteString(s string, h *Handlers, config ...Config) (string, error)
- type Attribute
- type AttributeIterator
- type Comment
- func (c *Comment) InsertAfterAsHtml(content string) error
- func (c *Comment) InsertAfterAsText(content string) error
- func (c *Comment) InsertBeforeAsHtml(content string) error
- func (c *Comment) InsertBeforeAsText(content string) error
- func (c *Comment) IsRemoved() bool
- func (c *Comment) Remove()
- func (c *Comment) ReplaceAsHtml(content string) error
- func (c *Comment) ReplaceAsText(content string) error
- func (c *Comment) SetText(text string) error
- func (c *Comment) Text() string
- type CommentHandlerFunc
- type Config
- type Doctype
- type DoctypeHandlerFunc
- type DocumentContentHandler
- type DocumentEnd
- type DocumentEndHandlerFunc
- type Element
- func (e *Element) AttributeIterator() *AttributeIterator
- func (e *Element) AttributeValue(name string) (string, error)
- func (e *Element) HasAttribute(name string) (bool, error)
- func (e *Element) InsertAfterEndTagAsHtml(content string) error
- func (e *Element) InsertAfterEndTagAsText(content string) error
- func (e *Element) InsertAfterStartTagAsHtml(content string) error
- func (e *Element) InsertAfterStartTagAsText(content string) error
- func (e *Element) InsertBeforeEndTagAsHtml(content string) error
- func (e *Element) InsertBeforeEndTagAsText(content string) error
- func (e *Element) InsertBeforeStartTagAsHtml(content string) error
- func (e *Element) InsertBeforeStartTagAsText(content string) error
- func (e *Element) IsRemoved() bool
- func (e *Element) NamespaceUri() string
- func (e *Element) Remove()
- func (e *Element) RemoveAndKeepContent()
- func (e *Element) RemoveAttribute(name string) error
- func (e *Element) ReplaceAsHtml(content string) error
- func (e *Element) ReplaceAsText(content string) error
- func (e *Element) SetAttribute(name string, value string) error
- func (e *Element) SetInnerContentAsHtml(content string) error
- func (e *Element) SetInnerContentAsText(content string) error
- func (e *Element) SetTagName(name string) error
- func (e *Element) TagName() string
- type ElementContentHandler
- type ElementHandlerFunc
- type Handlers
- type MemorySettings
- type OutputSink
- type RewriterDirective
- type TextChunk
- func (t *TextChunk) Content() string
- func (t *TextChunk) InsertAfterAsHtml(content string) error
- func (t *TextChunk) InsertAfterAsText(content string) error
- func (t *TextChunk) InsertBeforeAsHtml(content string) error
- func (t *TextChunk) InsertBeforeAsText(content string) error
- func (t *TextChunk) IsLastInTextNode() bool
- func (t *TextChunk) IsRemoved() bool
- func (t *TextChunk) Remove()
- func (t *TextChunk) ReplaceAsHtml(content string) error
- func (t *TextChunk) ReplaceAsText(content string) error
- type TextChunkHandlerFunc
- type Writer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrCannotGetErrorMessage = errors.New("cannot get error message from underlying lol-html lib")
Functions ¶
func RewriteString ¶ added in v0.2.2
RewriteString rewrites the given string with the provided Handlers and Config.
Example ¶
output, err := lolhtml.RewriteString( `<div><a href="http://example.com"></a></div>`, &lolhtml.Handlers{ ElementContentHandler: []lolhtml.ElementContentHandler{ { Selector: "a[href]", ElementHandler: func(e *lolhtml.Element) lolhtml.RewriterDirective { href, err := e.AttributeValue("href") if err != nil { log.Fatal(err) } href = strings.ReplaceAll(href, "http:", "https:") err = e.SetAttribute("href", href) if err != nil { log.Fatal(err) } return lolhtml.Continue }, }, }, }, ) if err != nil { log.Fatal(err) } fmt.Println(output)
Output: <div><a href="https://example.com"></a></div>
Types ¶
type Attribute ¶
type Attribute C.lol_html_attribute_t
type AttributeIterator ¶
type AttributeIterator C.lol_html_attributes_iterator_t
AttributeIterator cannot be iterated by "range" syntax. You should use AttributeIterator.Next() instead.
func (*AttributeIterator) Free ¶
func (ai *AttributeIterator) Free()
func (*AttributeIterator) Next ¶
func (ai *AttributeIterator) Next() *Attribute
type Comment ¶
type Comment C.lol_html_comment_t
func (*Comment) InsertAfterAsHtml ¶
func (*Comment) InsertAfterAsText ¶ added in v0.2.2
func (*Comment) InsertBeforeAsHtml ¶
func (*Comment) InsertBeforeAsText ¶ added in v0.2.2
func (*Comment) ReplaceAsHtml ¶
func (*Comment) ReplaceAsText ¶ added in v0.2.2
type CommentHandlerFunc ¶ added in v0.2.1
type CommentHandlerFunc func(*Comment) RewriterDirective
type Config ¶
type Config struct { // defaults to "utf-8". Encoding string // defaults to PreallocatedParsingBufferSize: 1024, MaxAllowedMemoryUsage: 1<<63 - 1. Memory *MemorySettings // defaults to func([]byte) {}. In other words, totally discard output. Sink OutputSink // defaults to true. If true, bail out for security reasons when ambiguous. Strict bool }
Config defines settings for the rewriter.
type Doctype ¶
type Doctype C.lol_html_doctype_t
type DoctypeHandlerFunc ¶ added in v0.2.1
type DoctypeHandlerFunc func(*Doctype) RewriterDirective
type DocumentContentHandler ¶ added in v0.2.1
type DocumentContentHandler struct { DoctypeHandler DoctypeHandlerFunc CommentHandler CommentHandlerFunc TextChunkHandler TextChunkHandlerFunc DocumentEndHandler DocumentEndHandlerFunc }
type DocumentEnd ¶ added in v0.2.1
type DocumentEnd C.lol_html_doc_end_t
func (*DocumentEnd) AppendAsHtml ¶ added in v0.2.1
func (d *DocumentEnd) AppendAsHtml(content string) error
func (*DocumentEnd) AppendAsText ¶ added in v0.2.2
func (d *DocumentEnd) AppendAsText(content string) error
type DocumentEndHandlerFunc ¶ added in v0.2.1
type DocumentEndHandlerFunc func(*DocumentEnd) RewriterDirective
type Element ¶
type Element C.lol_html_element_t
func (*Element) AttributeIterator ¶ added in v0.2.1
func (e *Element) AttributeIterator() *AttributeIterator
func (*Element) AttributeValue ¶ added in v0.2.1
func (*Element) InsertAfterEndTagAsHtml ¶
func (*Element) InsertAfterEndTagAsText ¶ added in v0.2.2
func (*Element) InsertAfterStartTagAsHtml ¶
func (*Element) InsertAfterStartTagAsText ¶ added in v0.2.2
func (*Element) InsertBeforeEndTagAsHtml ¶
func (*Element) InsertBeforeEndTagAsText ¶ added in v0.2.2
func (*Element) InsertBeforeStartTagAsHtml ¶
func (*Element) InsertBeforeStartTagAsText ¶ added in v0.2.2
func (*Element) NamespaceUri ¶ added in v0.2.1
func (*Element) RemoveAndKeepContent ¶
func (e *Element) RemoveAndKeepContent()
func (*Element) RemoveAttribute ¶
func (*Element) ReplaceAsHtml ¶
func (*Element) ReplaceAsText ¶ added in v0.2.2
func (*Element) SetInnerContentAsHtml ¶
func (*Element) SetInnerContentAsText ¶ added in v0.2.2
func (*Element) SetTagName ¶
type ElementContentHandler ¶ added in v0.2.1
type ElementContentHandler struct { Selector string ElementHandler ElementHandlerFunc CommentHandler CommentHandlerFunc TextChunkHandler TextChunkHandlerFunc }
type ElementHandlerFunc ¶ added in v0.2.1
type ElementHandlerFunc func(*Element) RewriterDirective
type Handlers ¶ added in v0.2.1
type Handlers struct { DocumentContentHandler []DocumentContentHandler ElementContentHandler []ElementContentHandler }
type MemorySettings ¶
type OutputSink ¶
type OutputSink func([]byte)
OutputSink takes each chunked output as a byte slice.
type RewriterDirective ¶
type RewriterDirective int
RewriterDirective should returned by callback handlers, to inform the rewriter to continue or stop parsing.
const ( // Let the normal parsing process continue. Continue RewriterDirective = iota // Stop the rewriter immediately. Content currently buffered is discarded, and an error is returned. Stop )
type TextChunk ¶
type TextChunk C.lol_html_text_chunk_t
func (*TextChunk) InsertAfterAsHtml ¶
func (*TextChunk) InsertAfterAsText ¶ added in v0.2.2
func (*TextChunk) InsertBeforeAsHtml ¶
func (*TextChunk) InsertBeforeAsText ¶ added in v0.2.2
func (*TextChunk) IsLastInTextNode ¶
func (*TextChunk) ReplaceAsHtml ¶
func (*TextChunk) ReplaceAsText ¶ added in v0.2.2
type TextChunkHandlerFunc ¶ added in v0.2.1
type TextChunkHandlerFunc func(*TextChunk) RewriterDirective
type Writer ¶ added in v0.2.1
type Writer struct {
// contains filtered or unexported fields
}
func NewWriter ¶ added in v0.2.1
NewWriter returns a new Writer with Handlers and Config configured, writing to w.
Example ¶
chunk := []byte("Hello, <span>World</span>!") r := bytes.NewReader(chunk) w, err := lolhtml.NewWriter( os.Stdout, &lolhtml.Handlers{ ElementContentHandler: []lolhtml.ElementContentHandler{ { Selector: "span", ElementHandler: func(e *lolhtml.Element) lolhtml.RewriterDirective { err := e.SetInnerContentAsText("LOL-HTML") if err != nil { log.Fatal(err) } return lolhtml.Continue }, }, }, }, ) if err != nil { log.Fatal(err) } defer w.Free() _, err = io.Copy(w, r) if err != nil { log.Fatal(err) } err = w.End() if err != nil { log.Fatal(err) }
Output: Hello, <span>LOL-HTML</span>!
Click to show internal directories.
Click to hide internal directories.