html

package
v0.0.0-...-994bc1c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

Usage

In order to change to update the DOM, you need to create DOM representation. DOM representation can be shared across the app:

template := []byte("<html>...</html>")
dom, err := html.New(template)
// handle error

You can specify some options while creating DOM:

  • BufferSize - initial buffer size for each DOM session, int wrapper
  • *Filters - represents allowed tags and attributes
bufferSize := option.BufferSize(1024)
filter := option.NewFilters(
	option.NewFilter("div", "class"), 
	option.NewFilter("img", "src"),
	)
dom, err := option.New(template, bufferSize, filter)
// handle error

Then you need to create a Document:

document := dom.Document()
documentWithBuffer := dom.Document(option.NewBuffer(1024))

If you don't provide a Buffer, will be created one with BufferSize specified while creating DOM

Now you can get/set Attribute, get/set InnerHTML by using selectors.

Selectors order: Tag -> Attribute -> Attribute value. Selectors are optional, it means if you don't specify Attribute only tag will be checked.

Usage:

	template := `
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Index</title>
</head>
<body>
	<p class="[class]">Classes</p>
	<img src="[src]" alt="alt"/>
	<div hidden="[hidden]">This is div inner</div>
</body>
</html>`

dom, err := New(template)
if err != nil {
    fmt.Println(err)
    return
}

filter := option.NewFilters(
    option.NewFilter("div", "hidden"),
    option.NewFilter("img", "src"),
)

bufferSize := option.BufferSize(1024)
document := dom.Document(filter, bufferSize)

elemIt := document.Select("div", "hidden")
for elemIt.Has() {
    elem, _ := elemIt.Next()
    fmt.Println(elem.InnerHTML())
    _ = elem.SetInnerHTML("This will be new InnerHTML")
    attribute, ok := elem.MatchAttribute("hidden", "[hidden]")
    if ok {
        attribute.Set("true")
        fmt.Println(attribute.Value())
    }
}

attributeIt := document.SelectAttributes("img", "src", "[src]")
for attributeIt.Has() {
    attribute, _ := attributeIt.Next()
    attribute.Set("abcdef.jpg")
    fmt.Println(attribute.Value())
}

fmt.Println(document.Render())

// Output:
//This is div inner
//true
//abcdef.jpg
//
//<!DOCTYPE html>
//<html lang="en">
//<head>
//	<title>Index</title>
//</head>
//<body>
//	<p class="[class]">Classes</p>
//	<img src="abcdef.jpg" alt="alt"/>
//	<div hidden="true">This will be new InnerHTML</div>
//</body>
//</html>

Options

Options supported:

  • *Buffer - you can reuse Buffers by passing one while creating the DOM using VirutalDOM.DOM() manually, or by using Pool created via NewPool(size int, dom *VirtualDOM)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	// contains filtered or unexported fields
}

Attribute represents HTML Element attribute

func (*Attribute) Name

func (a *Attribute) Name() string

Name returns Attribute Key

func (*Attribute) Parent

func (a *Attribute) Parent() *Element

Parent returns Attribute parent Element

func (*Attribute) Set

func (a *Attribute) Set(newValue string)

Set updates Attribute value

func (*Attribute) Value

func (a *Attribute) Value() string

Value returns Attribute Value

type AttributeIterator

type AttributeIterator struct {
	// contains filtered or unexported fields
}

AttributeIterator iterates over matching attributes

func (*AttributeIterator) Has

func (at *AttributeIterator) Has() bool

Has returns true if there are more attributes matching given selectors

func (*AttributeIterator) Next

func (at *AttributeIterator) Next() (*Attribute, error)

Next returns Attribute matching given selectors Note: it is needed to call Has before calling Next

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer hold the current DOM value

func NewBuffer

func NewBuffer(size int) *Buffer

NewBuffer creates new buffer of given size.

type DOM

type DOM struct {
	// contains filtered or unexported fields
}

DOM represents DOM structure

func New

func New(template string, options ...option.Option) (*DOM, error)

New parses template and creates new DOM. Filters can be specified to index some tags and attributes.

Example
template := `
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Index</title>
</head>
<body>
	<p class="[class]">Classes</p>
	<img src="[src]" alt="alt"/>
	<div hidden="[hidden]">This is div inner</div>
</body>
</html>`

dom, err := New(template)
if err != nil {
	fmt.Println(err)
	return
}

filter := option.NewFilters(
	option.NewFilter("div", "hidden"),
	option.NewFilter("img", "src"),
)

bufferSize := option.BufferSize(1024)
document := dom.Document(filter, bufferSize)

elemIt := document.Select("div", "hidden")
for elemIt.Has() {
	elem, _ := elemIt.Next()
	fmt.Println(elem.InnerHTML())
	_ = elem.SetInnerHTML("This will be new InnerHTML")
	attribute, ok := elem.MatchAttribute("hidden", "[hidden]")
	if ok {
		attribute.Set("true")
		fmt.Println(attribute.Value())
	}
}

attributeIt := document.SelectAttributes("img", "src", "[src]")
for attributeIt.Has() {
	attribute, _ := attributeIt.Next()
	attribute.Set("abcdef.jpg")
	fmt.Println(attribute.Value())
}

fmt.Println(document.Render())
Output:

This is div inner
true
abcdef.jpg

<!DOCTYPE html>
<html lang="en">
<head>
	<title>Index</title>
</head>
<body>
	<p class="[class]">Classes</p>
	<img src="abcdef.jpg" alt="alt"/>
	<div hidden="true">This will be new InnerHTML</div>
</body>
</html>

func (*DOM) Document

func (d *DOM) Document(options ...option.Option) *Document

Document creates new Document

type Document

type Document struct {
	// contains filtered or unexported fields
}

Document modifies the DOM

func (*Document) Render

func (d *Document) Render() string

Render returns template after DOM changes

func (*Document) Select

func (d *Document) Select(selectors ...string) *ElementIterator

Select returns ElementIterator to iterate over HTML Elements

func (*Document) SelectAttributes

func (d *Document) SelectAttributes(selectors ...string) *AttributeIterator

SelectAttributes returns AttributeIterator to iterate over HTML Attributes

func (*Document) SelectFirst

func (d *Document) SelectFirst(selectors ...string) (*Element, bool)

type Element

type Element struct {
	// contains filtered or unexported fields
}

Element represents HTML Element

func (*Element) AddAttribute

func (e *Element) AddAttribute(key, value string)

func (*Element) Attribute

func (e *Element) Attribute(name string) (*Attribute, bool)

Attribute returns matched attribute, true or nil, false

func (*Element) AttributeByIndex

func (e *Element) AttributeByIndex(i int) *Attribute

AttributeByIndex returns n-th Attribute

func (*Element) AttributesLen

func (e *Element) AttributesLen() int

AttributesLen returns number of Element Attributes

func (*Element) HasAttribute

func (e *Element) HasAttribute(name string) bool

func (*Element) InnerHTML

func (e *Element) InnerHTML() string

InnerHTML returns InnerHTML of Element

func (*Element) MatchAttribute

func (e *Element) MatchAttribute(name, value string) (*Attribute, bool)

MatchAttribute returns an attribute that matches the supplied attribute name and value

func (*Element) SetInnerHTML

func (e *Element) SetInnerHTML(innerHTML string) error

SetInnerHTML updates InnerHTML of Element

type ElementIterator

type ElementIterator struct {
	// contains filtered or unexported fields
}

ElementIterator iterates over matching tags

func (*ElementIterator) Has

func (it *ElementIterator) Has() bool

Has returns true if there are more tags matching given selectors

func (*ElementIterator) Next

func (it *ElementIterator) Next() (*Element, error)

Next returns Element matching given selectors Note: it is needed to call Has before calling Next

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool represents sharable Document instances

func NewPool

func NewPool(size int, dom *DOM) *Pool

NewPool creates Document pool

func (*Pool) New

func (p *Pool) New() *Document

New creates or reuse recent Document instance

func (*Pool) Put

func (p *Pool) Put(dom *Document)

Put returns Document to the pool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL