Documentation ¶
Overview ¶
Package trees is a work inspired by a experiemental package by now authors of Vecty(github.com/gopherjs/vecty), although the provide similar API, the underline nature of how gutrees behave and function is heavily different from its original. It provides a markup style that is seperate from the browser DOM yet capable of being generated into one and spots a quick virtual diffing system that makes it easy to upgrade existing markup with less cycles.
Index ¶
- Variables
- func AttrContains(e Attributes, f, val string) bool
- func Augment(root *Markup, m ...*Markup)
- func EqualAttributes(e, em Attributes) bool
- func EqualStyles(e, em Styles) bool
- func ParseTemplateInto(root *Markup, markup string, binding interface{})
- func ParseToRoot(root *Markup, markup string)
- func ParseTreeToText(markup string, withReturns bool) (io.WriterTo, error)
- func RandString(n int) string
- func ReplaceAttribute(m Attributes, name string, val string)
- func ReplaceORAddAttribute(m Properties, name string, val string)
- func ReplaceORAddStyle(m Properties, name string, val string)
- func ReplaceStyle(m Styles, name string, val string)
- func SetMode(ms Mode)
- func StyleContains(e Styles, f, val string) bool
- func Templated(tml string, bind interface{}, processor func(string) string) (string, error)
- type Appliable
- type AppliableTarget
- type AttrPrinter
- type AttrWriter
- type Attribute
- type Attributes
- type CSSStyle
- type ClassList
- type Clonable
- type DeferredApply
- type ElementWriter
- type Event
- func (e *Event) Apply(ex *Markup)
- func (e *Event) Clone() *Event
- func (e *Event) EventJSON() EventJSON
- func (e *Event) EventName() string
- func (e *Event) EventSelector() string
- func (e *Event) ID() string
- func (e *Event) ParentEventSelector() string
- func (e *Event) String() string
- func (e *Event) Target() string
- type EventJSON
- type EventOptions
- type Events
- type HideMorpher
- type Markup
- func CSSStylesheet(styles interface{}, bind interface{}, ext *css.Rule, plain bool) *Markup
- func ElementsUsingStyle(root *Markup, key string, val string) []*Markup
- func ElementsWithAttr(root *Markup, key string, val string) []*Markup
- func ElementsWithTag(root *Markup, tag string) []*Markup
- func If(state bool, m func() *Markup) *Markup
- func MarkdownTemplate(tml string, bind interface{}) *Markup
- func NewMarkup(tag string, autoClose bool) *Markup
- func NewText(txt string, dl ...interface{}) *Markup
- func ParseAndFirst(markup string) *Markup
- func ParseAsRoot(root string, markup string) *Markup
- func ParseFirstOrMakeRoot(markup string) *Markup
- func ParseTemplate(markup string, binding interface{}) []*Markup
- func ParseTree(markup string) []*Markup
- func When(state bool, first, other *Markup) *Markup
- func (e *Markup) AddAttribute(p Property)
- func (e *Markup) AddChild(child ...*Markup)
- func (e *Markup) AddEvent(ev Event)
- func (e *Markup) AddMorpher(m ...Morpher)
- func (e *Markup) AddStyle(p Property)
- func (e *Markup) Apply(em *Markup)
- func (e *Markup) ApplyChildren(em *Markup)
- func (e *Markup) ApplyMorphers() *Markup
- func (e *Markup) Attributes() []Property
- func (e *Markup) AutoClosed() bool
- func (e *Markup) Children() []*Markup
- func (e *Markup) Clean()
- func (e *Markup) Clone() *Markup
- func (e *Markup) CopyTo(co *Markup)
- func (e *Markup) EHTML() template.HTML
- func (e *Markup) EachChild(fn func(*Markup))
- func (e *Markup) EachEvent(fn func(*Event, *Markup))
- func (e *Markup) Empty()
- func (e *Markup) EventID() string
- func (e *Markup) Events() []Event
- func (e *Markup) FirstChild() *Markup
- func (e *Markup) HTML() string
- func (e *Markup) Hash() string
- func (e *Markup) IDSelector(useParent bool) string
- func (e *Markup) LastChild() *Markup
- func (e *Markup) MarshalJSON() ([]byte, error)
- func (e *Markup) Morphers() []Morpher
- func (e *Markup) Name() string
- func (e *Markup) NthChild(index int) *Markup
- func (e *Markup) Reconcile(em *Markup) bool
- func (e *Markup) Remove()
- func (e *Markup) Removed() bool
- func (e *Markup) Styles() []Property
- func (e *Markup) SwapHash(hash string)
- func (e *Markup) SwapUID(uid string)
- func (e *Markup) TextContent() string
- func (e *Markup) TreeJSON() MarkupJSON
- func (e *Markup) UID() string
- func (e *Markup) UnRemove()
- func (e *Markup) UnmarshalJSON(data []byte) error
- func (e *Markup) UpdateHash()
- type MarkupJSON
- type Mode
- type Morpher
- type Morphers
- type Properties
- type Property
- func GetAttr(e Attributes, f string) (Property, error)
- func GetAttrs(e Attributes, f, val string) []Property
- func GetStyle(e Styles, f string) (Property, error)
- func GetStyles(e Styles, f, val string) []Property
- func IfProperty(state bool, m func() Property) Property
- func WhenProperty(state bool, first Property, other Property) Property
- type RemoveMorpher
- type Selector
- type StylePrinter
- type StyleWriter
- type Styles
- type SwitchMorpher
- type TextMarkup
- type TextPrinter
- type TextWriter
Constants ¶
This section is empty.
Variables ¶
var ( // Hide will set any markup top level root into a style of display none. Hide hideMarkup // Show will set any markup top level root into a style of display block. Show showMarkup )
var ErrNotAttr = errors.New("Value type is not n Attribute type")
ErrNotAttr relating to the attribute types
var ErrNotElem = errors.New("*Markup is not a *Markup type")
ErrNotElem is returned when the markup type does not match the *Markup type
var ErrNotFound = errors.New("Item not found")
ErrNotFound relating to the attribute types
var ErrNotMarkup = errors.New("Value does not match *Markup interface types")
ErrNotMarkup is returned when the value/pointer type does not match the *Markup interface type
var ErrNotStyle = errors.New("Value type is not a Style type")
ErrNotStyle relating to the style types
var ErrNotText = errors.New("*Markup is not a *Text type")
ErrNotText is returned when the markup type is not a text markup
var Query queryCtrl
Query defines a package level variable for access the query interface which handles running css queries on markup structures.
var SimpleElementWriter = NewElementWriter(SimpleAttrWriter, SimpleStyleWriter, SimpleTextWriter)
SimpleElementWriter provides a default writer using the basic attribute and style writers
Functions ¶
func AttrContains ¶
func AttrContains(e Attributes, f, val string) bool
AttrContains returns the attributes that have the specified text within the naming convention and if it also contains the set val if not an empty "", NOTE: string.Contains is used
func EqualAttributes ¶
func EqualAttributes(e, em Attributes) bool
EqualAttributes returns true/false if the elements and the giving markup have equal attribute
func EqualStyles ¶
EqualStyles returns true/false if the style values are all equal attribute.
func ParseTemplateInto ¶
ParseTemplateInto parses the provided string has a template which is processed with the provided binding and passed into the root.
func ParseToRoot ¶
ParseToRoot passes the markup generated from the markup added to the provided root.
func ParseTreeToText ¶
ParseTreeToText takes a string markup and returns a *Markup which contains the full structure transpiled into the gutrees markup block structure.
func RandString ¶
RandString generates a set of random numbers of a set length
func ReplaceAttribute ¶
func ReplaceAttribute(m Attributes, name string, val string)
ReplaceAttribute replaces a specific attribute with the given name with the supplied value.
func ReplaceORAddAttribute ¶
func ReplaceORAddAttribute(m Properties, name string, val string)
ReplaceORAddAttribute replaces a specific attribute with the given name with the supplied value if not found it adds a new one if found and if the type does not match a *CSSStyle then it stops.
func ReplaceORAddStyle ¶
func ReplaceORAddStyle(m Properties, name string, val string)
ReplaceORAddStyle replaces a specific style with the given name with the supplied value if not found it adds a new one if found and if the type does not match a *CSSStyle then it stops.
func ReplaceStyle ¶
ReplaceStyle replaces a specific style with the given name with the supplied value.
func StyleContains ¶
StyleContains returns the styles that contain the specified name and if the val is not empty then that contains the specified value also, note that strings NOTE: string.Contains is used
Types ¶
type Appliable ¶
type Appliable interface {
Apply(*Markup)
}
Appliable define the interface specification for applying changes to elements elements in tree
type AppliableTarget ¶
AppliableTarget defines a struct which takes a giving appliable and target attempting to add the Appliable to the roots target child.
func ApplyIn ¶
func ApplyIn(child Appliable, target string, multiple bool) AppliableTarget
ApplyIn adds the giving Appliable to the giving target/targets if multiple is set to true and many are found and returns the a new Appliable which will perform the action.
func (AppliableTarget) Apply ¶
func (e AppliableTarget) Apply(em *Markup)
Apply will attempt to search for the target within the root and append to that giving child else ignoring the call.
type AttrPrinter ¶
AttrPrinter defines a printer interface for writing out a Attribute objects into a string form
type AttrWriter ¶
type AttrWriter struct{}
AttrWriter provides a concrete struct that meets the AttrPrinter interface
var SimpleAttrWriter AttrWriter
SimpleAttrWriter provides a basic attribute writer
func (AttrWriter) Print ¶
func (m AttrWriter) Print(a []Property) string
Print returns a stringed repesentation of the attribute object
type Attribute ¶
Attribute define the struct for attributes
func NewAttrWith ¶
NewAttrWith returns a new attribute instance with a provided function to call to provide a after effect to the markup.
type Attributes ¶
type Attributes interface {
Attributes() []Property
}
Attributes defines an interface that returns a list of attributes.
type CSSStyle ¶
CSSStyle define the style specification for element styles
func NewCSSStyle ¶
NewCSSStyle returns a new style instance
type ClassList ¶
type ClassList struct {
// contains filtered or unexported fields
}
ClassList defines the list type for class lists.
func NewClassList ¶
NewClassList returns a new ClassList instance.
type DeferredApply ¶
DeferredApply will apply a giving set of values to a giving root.
func ApplyTo ¶
func ApplyTo(child Appliable, children ...Appliable) DeferredApply
ApplyTo adds the giving Appliables to the target during calls to Apply.
func (DeferredApply) Apply ¶
func (d DeferredApply) Apply(em *Markup)
Apply will attempt to search for the target within the root and append to that giving child else ignoring the call.
type ElementWriter ¶
type ElementWriter struct {
// contains filtered or unexported fields
}
ElementWriter writes out the element out as a string matching the html tag rules
func NewElementWriter ¶
func NewElementWriter(aw AttrPrinter, sw StylePrinter, tw TextPrinter) *ElementWriter
NewElementWriter returns a new writer for Element objects
func (*ElementWriter) Print ¶
func (m *ElementWriter) Print(e *Markup) string
Print returns the string representation of the element
type Event ¶
type Event struct { Type string PreventDefault bool StopPropagation bool UseCapture bool StopImmediatePropagation bool Tree *Markup Remove common.Remover // contains filtered or unexported fields }
Event provide a meta registry for helps in registering events for dom markups which is translated to the nodes themselves
func NewEvent ¶
func NewEvent(options ...EventOptions) *Event
NewEvent returns a event object that allows registering events to eventlisteners.
func (*Event) EventJSON ¶
EventJSON returns the event json structure which represent the giving event.
func (*Event) EventSelector ¶
EventSelector returns the selector for this events tree.
func (*Event) ParentEventSelector ¶
ParentEventSelector returns the parent selector for this events markup tree.
type EventJSON ¶
type EventJSON struct { ParentSelector string `json:"ParentSelector"` EventSelector string `json:"EventSelector"` EventName string `json:"EventName"` Event string `json:"Event"` PreventDefault bool `json:"PreventDefault"` StopPropagation bool `json:"StopPropagation"` UseCapture bool `json:"UseCapture"` StopImmediatePropagation bool `json:"StopImmediatePropagation"` }
EventJSON defines a struct which contains the giving events and and tree of the giving tree.
type EventOptions ¶
type EventOptions func(*Event)
EventOptions defines a function type used to apply specific operations to a Event object.
func PreventDefault ¶
func PreventDefault(state bool) EventOptions
PreventDefault sets the type of event.
func StopImmediatePropagation ¶
func StopImmediatePropagation(state bool) EventOptions
StopImmediatePropagation sets the type of event.
func StopPropagation ¶
func StopPropagation(state bool) EventOptions
StopPropagation sets the type of event.
type Events ¶
type Events interface {
Events() []Event
}
Events defines an interface that returns a list of events.
type HideMorpher ¶
type HideMorpher struct {
// contains filtered or unexported fields
}
HideMorpher defines a morpher which sets the giving markup as removed.
func (*HideMorpher) Morph ¶
func (r *HideMorpher) Morph(m *Markup) *Markup
Morph sets the markup received as removed.
func (*HideMorpher) Off ¶
func (r *HideMorpher) Off(m interface{})
Off switches the state of the morpher to set hidden state to be true.
func (*HideMorpher) On ¶
func (r *HideMorpher) On(m interface{})
On switches the state of the morpher out of a hidden state to be true.
type Markup ¶
type Markup struct { ID string // contains filtered or unexported fields }
Markup represent a concrete implementation of a element node.
func CSSStylesheet ¶
CSSStylesheet provides a function that takes style rules which returns a stylesheet embeded into the provided element parent and is built on the gu/css package which collects necessary details from its parent to only target where it gets mounted.
func ElementsUsingStyle ¶
ElementsUsingStyle returns the children within the element matching the stlye restrictions passed. NOTE: is uses StyleContains
func ElementsWithAttr ¶
ElementsWithAttr returns the children within the element matching the stlye restrictions passed. NOTE: is uses AttrContains
func ElementsWithTag ¶
ElementsWithTag returns elements matching the tag type in the parent markup children list only without going deeper into children's children lists.
func MarkdownTemplate ¶
MarkdownTemplate returns a markup generated from a markup down string which is built into a markup. If an error occured, it will be turned into an error tag with the contents of the error.
func NewMarkup ¶
NewMarkup returns a new element instance giving the specified name which is used as a tag name.
func ParseAndFirst ¶
ParseAndFirst expects the markup provided to only have one root element which will be returned.
func ParseAsRoot ¶
ParseAsRoot returns the markup generated from the provided markup, returning them as children of the provided root.
func ParseFirstOrMakeRoot ¶
ParseFirstOrMakeRoot attempts to parse the giving markup and returns the element if only one else creates a div and adds all children as part of div.
func ParseTemplate ¶
ParseTemplate parses the provided string has a template which is processed with the provided binding.
func ParseTree ¶
ParseTree takes a string markup and returns a *Markup which contains the full structure transpiled into the gutrees markup block structure.
func (*Markup) AddAttribute ¶
AddAttribute adds a property to the attribute property list.
func (*Markup) AddMorpher ¶
AddMorpher adds the provided morphers into the elements lists.
func (*Markup) ApplyChildren ¶
ApplyChildren applies the internal children of the markup to the provided parent.
func (*Markup) ApplyMorphers ¶
ApplyMorphers calls all elemental morphers sequentially applying them to the element and passes the result as the input of the next morpher unless. If any morpher returns nil, then the element is reused again until all morphers are called.
func (*Markup) Attributes ¶
Attributes return the internal attribute list of the element
func (*Markup) AutoClosed ¶
AutoClosed returns true/false if this element uses a </> or a <></> tag convention
func (*Markup) Clean ¶
func (e *Markup) Clean()
Clean cleans out all internal markup marked as removable.
func (*Markup) EHTML ¶
EHTML returns the html string wrapped by a template.HTML type to avoid getting escaped by go templates. The returned html is rendered using the default SimpleElementWriter and represents the DOM of the giving element.
func (*Markup) EachChild ¶
EachChild iterates all children from this giving root down with all childrens allowing the callback to process the child has needed.
func (*Markup) EachEvent ¶
EachEvent iterates all events from this giving root down with all childrens allowing the callback to process the events has needed.
func (*Markup) FirstChild ¶
FirstChild returns the first child in the markup children list.
func (*Markup) HTML ¶
HTML returns the html string representing the DOM of the giving element. The returned html is rendered using the default SimpleElementWriter.
func (*Markup) IDSelector ¶
IDSelector returns the unique selector for the giving markup.
func (*Markup) MarshalJSON ¶
MarshalJSON returns the html representation of the giving markup.
func (*Markup) Reconcile ¶
Reconcile takes a old markup and reconciles its uid and its children with these information,it returns a true/false telling the parent if the children swapped hashes. The reconcilation uses the order in which elements are added, if the order and element types are same then the uid are swapped, else it firsts checks the element type, but if not the same adds the old one into the new list as removed then continues the check. The system takes position of elements in the old and new as very important and I cant stress this enough, "Element Positioning" in the markup are very important, If a Anchor was the first element in the old render and the next pass returns a Div in the position for that Anchor in the new render, the old Anchor will be marked as removed and will be removed from the dom and ignored by the writers. When two elements position are same and their types are the same then a checkup process is done using the elements attributes, this is done to determine if the hash value of the new should be swapped with the old. We cant use style properties here because they are the most volatile of the set and will periodically be either changed and returned to normal values eg display: none to display: block and vise-versa, so only attributes are used in the check process.
func (*Markup) Remove ¶
func (e *Markup) Remove()
Remove sets the markup as removable and adds a 'NodeRemoved' attribute to it.
func (*Markup) TextContent ¶
TextContent returns the elements text value by either running a text content function if provided else defaulting to the textContent field.
func (*Markup) TreeJSON ¶
func (e *Markup) TreeJSON() MarkupJSON
TreeJSON returns the giving MarkupJSON for this giving markup and the events related to this markup.
func (*Markup) UnRemove ¶
func (e *Markup) UnRemove()
UnRemove sets the markup as not to be removable.
func (*Markup) UnmarshalJSON ¶
UnmarshalJSON the provided data and adds the giving children into the giving root.
type MarkupJSON ¶
type MarkupJSON struct { TreeID string `json:"TreeID"` Events []EventJSON `json:"Events"` Markup string `json:"Markup"` }
MarkupJSON defines a struct which contains the giving events and and tree of the giving tree.
type Mode ¶
type Mode int
Mode defines the behaviour of the printer setup. Where each increasing mode affects the behaviour and final form of the print outs.
type Morpher ¶
Morpher defines an interface which morphs the giving markup based on its current internal state based on some internal condition.
type Properties ¶
type Properties interface { AddAttribute(Property) Attributes() []Property AddStyle(Property) Styles() []Property AddEvent(Event) Events() []Event }
Properties interface defines a type that has Attributes
type Property ¶
Property defines the interface for attributes in trees. It provides a apply and RenderAttribute which returns the key and value for that attribute.
func GetAttr ¶
func GetAttr(e Attributes, f string) (Property, error)
GetAttr returns the attribute with the specified tag name
func GetAttrs ¶
func GetAttrs(e Attributes, f, val string) []Property
GetAttrs returns the attributes that have the specified text within the naming convention and if it also contains the set val if not an empty "", NOTE: string.Contains is used
func GetStyles ¶
GetStyles returns the styles that contain the specified name and if not empty that contains the specified value also, note that strings NOTE: string.Contains is used when checking value parameter if present
func IfProperty ¶
IfProperty returns the property when the giving state is true.
type RemoveMorpher ¶
type RemoveMorpher struct {
// contains filtered or unexported fields
}
RemoveMorpher defines a morpher which sets the giving markup as removed.
func (*RemoveMorpher) Morph ¶
func (r *RemoveMorpher) Morph(m *Markup) *Markup
Morph sets the markup received as removed.
func (*RemoveMorpher) Off ¶
func (r *RemoveMorpher) Off(m interface{})
Off switches the state of the morpher to set remove to be true.
func (*RemoveMorpher) On ¶
func (r *RemoveMorpher) On(m interface{})
On switches the state of the morpher out of a remove to be true.
type Selector ¶
type Selector struct { Tag string ID string Psuedo string AttrOp string AttrName string AttrValue string Classes []string Children []*Selector Order map[string]string }
Selector defines a structure which defines the requirements for a given matching to be processed.
func (*Selector) GetSelector ¶
GetSelector returns the selector received for the given selector.
type StylePrinter ¶
StylePrinter defines a printer interface for writing out a style objects into a string form
type StyleWriter ¶
type StyleWriter struct{}
StyleWriter provides a concrete struct that meets the AttrPrinter interface
var SimpleStyleWriter StyleWriter
SimpleStyleWriter provides a basic style writer
func (StyleWriter) Print ¶
func (m StyleWriter) Print(s []Property) string
Print returns a stringed repesentation of the style object
type Styles ¶
type Styles interface {
Styles() []Property
}
Styles defines an interface that returns a list of styles.
type SwitchMorpher ¶
type SwitchMorpher interface { Morpher On(interface{}) Off(interface{}) }
SwitchMorpher defines an interface which allows switching the behaviour of which determines what the markup provided gets morphed into. This allows a nice simple binary morph of input based on certain conditions.
type TextMarkup ¶
type TextMarkup interface {
TextContent() string
}
TextMarkup defines a interface for text based markup.
type TextPrinter ¶
TextPrinter defines a printer interface for writing out a text type markup into a string form
type TextWriter ¶
type TextWriter struct{}
TextWriter writes out the text element/node for the vdom into a string
var SimpleTextWriter TextWriter
SimpleTextWriter provides a basic text writer
func (TextWriter) Print ¶
func (m TextWriter) Print(t *Markup) string
Print returns the string representation of the text object