element

package
v0.0.0-...-4102953 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ElementToFile

func ElementToFile(e Element, out *os.File) (err error)

Saves a textual markup representation of an Element and all of it's child elements to a file TODO this is recursive (careful of stack overflows)

Types

type AttributeValidator

type AttributeValidator struct {
	*BaseValidator
	Key, Val string
}
Attribute Validator checks if an element contains a matching

attribute value pair

func NewAttributeValidator

func NewAttributeValidator(key, text string) (*AttributeValidator, error)

Construct a AttributeValidator, text is the search term which can be a literal : Example: find all items of the "mytextwidget" class NewAttributeValidator("mytextwidget")

or a standard go regular expression: Example: perhaps we want to find all elements whose class begins with "mytext" NewAttributeValidator("mytext*")

func (AttributeValidator) Validate

func (t AttributeValidator) Validate(e Element) bool

type BaseElement

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

Common base HTML Element implementation

func NewBaseElement

func NewBaseElement() *BaseElement

func (*BaseElement) AddChild

func (e *BaseElement) AddChild(child Element)

Append a child to a node and remove it from its old parent if it has one

func (*BaseElement) AllByAttribute

func (e *BaseElement) AllByAttribute(name, value string) []Element

Return all elements found with the given attribute matching the given value.

Example: find all links where the url points to submit elem.ByAttribute("href", "/submit")

func (*BaseElement) AllByClass

func (e *BaseElement) AllByClass(class string) []Element

Return the all element found with the given class.

Example: elem := elem.AllByClass("info-result")

func (*BaseElement) AllById

func (e *BaseElement) AllById(id string) []Element

Return the all element found with the given id.

Example: find all links where the url points to submit elem := elem.ById("search-result")

func (*BaseElement) AllByTag

func (e *BaseElement) AllByTag(tag string) []Element

Return the first element with a matching tag Example: find all forms in the document forms := elem.ByTag("form")

func (*BaseElement) ByAttribute

func (e *BaseElement) ByAttribute(name, value string) Element

Return the first element found with a given attribute matching the given value. Usually used when there is only one of these elements on a page or any one of them will do.

Example: Return the first link with a url pointing to /search link := elem.ByAttribute("href", "/search")

func (*BaseElement) ByClass

func (e *BaseElement) ByClass(class string) Element

Return the first element found with the given class name.

Example: Find the first (usually only) search box div box := elem.ByClass("info-container")

func (*BaseElement) ById

func (e *BaseElement) ById(id string) Element

Return the first element found with the given id.

Example: Find the first (usually only) search box div box := elem.ById("search-box")

func (*BaseElement) ByTag

func (e *BaseElement) ByTag(tag string) Element

Return the first element with a matching tag Example: find the first form in the document form := elem.ByTag("form")

func (*BaseElement) GetAttribute

func (e *BaseElement) GetAttribute(key string) string

Return an Elements tag attribute Example: Read the css style attribute if present styleStr := elem.GetAttribute("style")

func (*BaseElement) GetAttributes

func (e *BaseElement) GetAttributes() map[string]string

Get all tag attributes belonging to an element

func (*BaseElement) GetChildren

func (e *BaseElement) GetChildren() []Element

Return all children directly below this element

func (*BaseElement) GetContent

func (e *BaseElement) GetContent() string

Returns text enclosed within this elements start and end tags excluding any child tags.

func (*BaseElement) GetKind

func (e *BaseElement) GetKind() ElemKind

func (*BaseElement) GetMutex

func (e *BaseElement) GetMutex() *sync.RWMutex

func (*BaseElement) GetParent

func (e *BaseElement) GetParent() Element

Return this childs current parent element

func (*BaseElement) GetTagName

func (e *BaseElement) GetTagName() string

Return the tag name for an element such as "img" or "div"

func (*BaseElement) Next

func (e *BaseElement) Next() Element

func (*BaseElement) Prev

func (e *BaseElement) Prev() Element

func (*BaseElement) RemoveAttribute

func (e *BaseElement) RemoveAttribute(key string)

Completely remove an attribute from an element Example: Remove all local formatting from an element elem.RemoveAttribute("style")

func (*BaseElement) RemoveChild

func (e *BaseElement) RemoveChild(child Element)

Remove a child and all its children from a given elements tree

func (*BaseElement) SetAttribute

func (e *BaseElement) SetAttribute(key, value string)

Set an Elements tag attribute Example: Use the style attribute to hide the element elem.SetAttribute("style","display:none")

func (*BaseElement) SetContent

func (e *BaseElement) SetContent(content string)

Sets the text enclosed within this elements start and end tags excluding any child tags.

func (*BaseElement) SetKind

func (e *BaseElement) SetKind(elemKind ElemKind)

func (*BaseElement) SetTagName

func (e *BaseElement) SetTagName(name string)

Set the tag name of this Element to the given name

func (*BaseElement) String

func (e *BaseElement) String() string

Return a textual representation of this element

type BaseValidator

type BaseValidator struct {
	Text string
	// contains filtered or unexported fields
}

Base Validator provides a basic default implementation of the Validator interface

func NewBaseValidator

func NewBaseValidator(text string) (b *BaseValidator, err error)

Construct a Validator, text is the search term which can be a literal : Example: NewBaseValidator("my-content-div-01")

or a standard go regular expression: Example: NewBaseValidator("my-content-div*")

func (*BaseValidator) FirstOnly

func (t *BaseValidator) FirstOnly() bool

func (*BaseValidator) GetRegex

func (t *BaseValidator) GetRegex() *regexp.Regexp

func (*BaseValidator) SetFirstOnly

func (t *BaseValidator) SetFirstOnly(first bool)

func (*BaseValidator) SetRegex

func (t *BaseValidator) SetRegex(reg *regexp.Regexp)

func (*BaseValidator) Validate

func (t *BaseValidator) Validate(e Element) bool

type ElemKind

type ElemKind int
const (
	ELEM_VOID ElemKind = 1 + iota
	ELEM_RAW
	ELEM_ESC_RAW
	ELEM_FOREIGN
	ELEM_NORMAL
)

func GetElemKind

func GetElemKind(tagName string) ElemKind

type Element

type Element interface {
	AddChild(child Element)
	RemoveChild(child Element)
	GetParent() Element

	GetAttribute(key string) string
	SetAttribute(key, value string)
	RemoveAttribute(key string)
	GetTagName() string
	SetTagName(name string)
	GetChildren() []Element
	GetContent() string
	SetContent(string)
	String() string
	GetAttributes() map[string]string
	ByAttribute(name, value string) Element
	AllByAttribute(name, value string) []Element
	ById(id string) Element
	AllById(id string) []Element
	ByClass(class string) Element
	AllByClass(class string) []Element
	ByTag(tag string) Element
	AllByTag(tag string) []Element
	GetMutex() *sync.RWMutex
	GetKind() ElemKind
	SetKind(ElemKind)
	Next() Element

	Prev() Element
	// contains filtered or unexported methods
}

Define common methods for HTML elements

func BFS

func BFS(current Element, v Validator) []Element

Perform an iterative Breadth first search to find all matching elements. Searching begins at the given element and all subchildren of this Element will be

func BFSFirst

func BFSFirst(current Element, v Validator) Element

Perform an iterative Breadth first search to find a matching element

type Form

type Form struct {
	BaseElement
	Inputs *url.Values
}

HTML <form> object

func NewForm

func NewForm() *Form

func (*Form) Action

func (e *Form) Action() string

func (*Form) ClearFields

func (e *Form) ClearFields()

func (*Form) GetField

func (e *Form) GetField(name string) string

func (*Form) GetFields

func (e *Form) GetFields() []Element

func (*Form) GetInputs

func (e *Form) GetInputs() []Element

func (*Form) GetSelects

func (e *Form) GetSelects() []Element

func (*Form) Method

func (e *Form) Method() string

func (*Form) Name

func (e *Form) Name() string

func (*Form) SetField

func (e *Form) SetField(name, value string)

Todo consider letting users add fields not present

type Input

type Input struct {
	BaseElement
}

HTML <input> object

func NewInput

func NewInput() *Input

type Page

type Page struct {
	Document *http.Response
	// contains filtered or unexported fields
}

func NewPage

func NewPage() *Page

func ParseBody

func ParseBody(r io.Reader) *Page

func ParseResp

func ParseResp(resp *http.Response) *Page

Build a Page from a http response

func (*Page) Absolutify

func (p *Page) Absolutify()

Convert all relative links on this page to absolute links (useful when saving a file to disk for later viewing)

func (*Page) AllByAttribute

func (p *Page) AllByAttribute(name, value string) []Element

Find all elements matching a given attribute Example: result := page.AllAttribute("class","search-result-div")

func (*Page) AllByClass

func (p *Page) AllByClass(class string) []Element

Find all elements with this class Example: result := page.AllById("news-result-div")

func (*Page) AllById

func (p *Page) AllById(id string) []Element

Find all elements with this id Example: result := page.AllById("search-result-div")

func (*Page) ByAttribute

func (p *Page) ByAttribute(name, value string) Element

Find the first element matching a given attribute Example: form := page.ByAttribute("id","login-form")

func (*Page) ByClass

func (p *Page) ByClass(class string) Element

Find the first element with this class Example: form := page.ByClass("container-div")

func (*Page) ById

func (p *Page) ById(id string) Element

Find the first element with this id Example: form := page.ById("login-form")

func (*Page) GetUrl

func (p *Page) GetUrl() string

func (*Page) SaveToFile

func (p *Page) SaveToFile(fileName string)

Blocking call to save this pages html content to a file

func (*Page) SetUrl

func (p *Page) SetUrl(url string)

type Parser

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

func NewParser

func NewParser() *Parser

func (*Parser) ParsePage

func (p *Parser) ParsePage(r io.Reader) *Page

Create a page from a http body

type TagValidator

type TagValidator struct {
	*BaseValidator
	// contains filtered or unexported fields
}

TagValidator compares based on the value of the tag name

func NewTagValidator

func NewTagValidator(tagName string) (*TagValidator, error)

Construct a TagValidator, text is the search term which can be a literal : Example: find all menu tags NewTagValidator("menu")

or a standard go regular expression: Example: perhaps we want to find all <menu> and <menuitem> tags NewTagValidator("menu*")

func (TagValidator) Validate

func (t TagValidator) Validate(e Element) bool

type Validator

type Validator interface {
	Validate(e Element) bool
	GetRegex() *regexp.Regexp
	SetRegex(*regexp.Regexp)
	FirstOnly() bool
	SetFirstOnly(bool)
}

Validators : a generic way to compare html elements in document Searches

Jump to

Keyboard shortcuts

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