Documentation ¶
Overview ¶
The HTML package includes general functions for manipulating html tags, comments and the like. It includes specific functions for manipulating attributes inside of tags, including various special attributes like styles, classes, data-* attributes, etc.
Many of the routines return a boolean to indicate whether the data actually changed. This can be used to prevent needlessly redrawing html after setting values that had no affect on the attribute list.
Index ¶
- func AddClass(classes string, newClasses string) (string, bool)
- func AttributeString(i interface{}) string
- func Comment(s string) string
- func HasClass(classes string, testClass string) (found bool)
- func Indent(s string) string
- func RandomString(n int) string
- func RemoveClass(class string, removeClass string) (string, bool)
- func RemoveClassesWithPrefix(class string, prefix string) (string, bool)
- func RenderImage(src string, alt string, attributes *Attributes) string
- func RenderLabel(labelAttributes *Attributes, label string, ctrlHtml string, ...) string
- func RenderTag(tag string, attr *Attributes, innerHtml string) string
- func RenderTagNoSpace(tag string, attr *Attributes, innerHtml string) string
- func RenderVoidTag(tag string, attr *Attributes) (s string)
- func StyleString(i interface{}) string
- func TextToHtml(in string) (out string)
- func ToDataAttr(s string) (string, error)
- func ToDataJqKey(s string) (string, error)
- type Attributer
- type Attributes
- func (a *Attributes) AddClass(v string) *Attributes
- func (a *Attributes) AddClassChanged(v string) bool
- func (a *Attributes) Class() string
- func (a *Attributes) Clone() *Attributes
- func (a *Attributes) Copy() *Attributes
- func (a *Attributes) DataAttribute(name string) string
- func (a *Attributes) GetStyle(name string) string
- func (a *Attributes) HasClass(c string) bool
- func (a *Attributes) HasDataAttribute(name string) bool
- func (a *Attributes) HasStyle(name string) bool
- func (a *Attributes) ID() string
- func (a *Attributes) IsDisabled() bool
- func (a *Attributes) IsDisplayed() bool
- func (a *Attributes) Override(i maps.StringMapI) *Attributes
- func (a *Attributes) RemoveAttribute(name string) bool
- func (a *Attributes) RemoveClass(v string) bool
- func (a *Attributes) RemoveClassesWithPrefix(v string) bool
- func (a *Attributes) RemoveDataAttribute(name string) bool
- func (a *Attributes) RemoveStyle(name string) (changed bool)
- func (a *Attributes) Set(name string, v string) *Attributes
- func (a *Attributes) SetChanged(name string, v string) (changed bool, err error)
- func (a *Attributes) SetClass(v string) *Attributes
- func (a *Attributes) SetClassChanged(v string) bool
- func (a *Attributes) SetDataAttribute(name string, v string) *Attributes
- func (a *Attributes) SetDataAttributeChanged(name string, v string) (changed bool, err error)
- func (a *Attributes) SetDisabled(d bool) *Attributes
- func (a *Attributes) SetDisplay(d string) *Attributes
- func (a *Attributes) SetID(i string) *Attributes
- func (a *Attributes) SetIDChanged(i string) (changed bool, err error)
- func (a *Attributes) SetStyle(name string, v string) *Attributes
- func (a *Attributes) SetStyleChanged(name string, v string) (changed bool, err error)
- func (a *Attributes) SetStyles(s *Style) *Attributes
- func (a *Attributes) SetStylesTo(s string) *Attributes
- func (a *Attributes) String() string
- func (a *Attributes) StyleMap() *Style
- func (a *Attributes) StyleString() string
- type LabelDrawingMode
- type Style
- type TagBuilder
- func (b *TagBuilder) Class(class string) *TagBuilder
- func (b *TagBuilder) ID(id string) *TagBuilder
- func (b *TagBuilder) InnerHtml(html string) *TagBuilder
- func (b *TagBuilder) InnerText(text string) *TagBuilder
- func (b *TagBuilder) IsVoid() *TagBuilder
- func (b *TagBuilder) Link(href string) *TagBuilder
- func (b *TagBuilder) Set(attribute string, value string) *TagBuilder
- func (b *TagBuilder) String() string
- func (b *TagBuilder) Tag(tag string) *TagBuilder
- type VoidTag
Examples ¶
- AddClass
- Attributes.AddClass
- Attributes.HasClass
- Attributes.HasStyle
- Attributes.Override
- Attributes.RemoveStyle
- Attributes.SetClass
- HasClass
- NewAttributesFrom
- RemoveClass
- RemoveClassesWithPrefix
- RenderTag
- RenderVoidTag
- Style.Get
- Style.RemoveAll
- Style.Set (A)
- Style.Set (B)
- Style.SetTo
- ToDataAttr
- ToDataJqKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddClass ¶
AddClass is a utility function that appends the given class name(s) to the end of the given string, if the names are not already in the string. Returns the new string, and a value indicating whether it changed or not. newClasses can have multiple space separated strings to add multiple classes. The final string returned will have no duplicates. Since the order of a class list in html may make a difference, you should take care in the order of the classes you add if this matters in your situation.
Example ¶
classes, changed := AddClass("myClass1 myClass2", "myClass1 myClass3") fmt.Println(classes + ":" + strconv.FormatBool(changed))
Output: myClass1 myClass2 myClass3:true
func AttributeString ¶
func AttributeString(i interface{}) string
AttributeString is a helper function to convert an interface type to a string that is appropriate for the value in the Set function.
func HasClass ¶
HasClass searches the list of strings for the given class name. testClass can only be a single class.
Example ¶
found := HasClass("myClass31 myClass2", "myClass3") fmt.Println(strconv.FormatBool(found))
Output: false
func Indent ¶
Indent will add space to the front of every line in the string. Since indent is used to format code for reading while we are in development mode, we do not need it to be particularly efficient.
func RandomString ¶
RandomString generates a pseudo random string of the given length Characters are drawn from legal HTML values that do not need encoding. The distribution is not perfect, so its not good for crypto, but works for general purposes This also works for GET variables
func RemoveClass ¶
Use RemoveClass to remove a class from the list of classes given. You can give it more than one class to remove by separating the classes with spaces in the removeClass string.
Example ¶
classes, changed := RemoveClass("myClass1 myClass2", "myClass1 myClass3") fmt.Println(classes + ":" + strconv.FormatBool(changed))
Output: myClass2:true
func RemoveClassesWithPrefix ¶
Many CSS frameworks use families of classes, which are built up from a base family name. For example, Bootstrap uses 'col-lg-6' to represent a table that is 6 units wide on large screens and Foundation uses 'large-6' to do the same thing. This utility removes classes that start with a particular prefix to remove whatever sizing class was specified. Returns the resulting class list, and true if the list actually changed.
Example ¶
classes, changed := RemoveClassesWithPrefix("col-6 col-brk col4-other", "col-") fmt.Println(classes + ":" + strconv.FormatBool(changed))
Output: col4-other:true
func RenderImage ¶
func RenderImage(src string, alt string, attributes *Attributes) string
func RenderLabel ¶
func RenderLabel(labelAttributes *Attributes, label string, ctrlHtml string, mode LabelDrawingMode) string
A utility function to render a label, together with its text. Various CSS frameworks require labels to be rendered a certain way.
func RenderTag ¶
func RenderTag(tag string, attr *Attributes, innerHtml string) string
RenderTag renders a standard html tag with a closing tag innerHtml is html, and must already be escaped if needed The tag will be surrounded with newlines to force general formatting consistency. This will cause the tag to be rendered with a space between it and its neighbors if the tag is not a block tag. In the few situations where you would want to get rid of this space, call RenderTagNoSpace()
Example ¶
fmt.Println(RenderTagNoSpace("div", NewAttributesFromMap(map[string]string{"id": "me", "name": "you"}), "Here I am"))
Output: <div id="me" name="you">Here I am</div>
func RenderTagNoSpace ¶
func RenderTagNoSpace(tag string, attr *Attributes, innerHtml string) string
RenderTagNoSpace is similar to RenderTag, but should be used in situations where the tag is an inline tag that you want to visually be right next to its neighbors with no space.
func RenderVoidTag ¶
func RenderVoidTag(tag string, attr *Attributes) (s string)
Render a void tag that has no closing tag
Example ¶
fmt.Println(RenderVoidTag("img", NewAttributesFromMap(map[string]string{"src": "thisFile"})))
Output: <img src="thisFile" />
func StyleString ¶
func StyleString(i interface{}) string
StyleString converts an interface type that is being used to set a style value to a string that can be fed into the SetStyle* functions
func TextToHtml ¶
Does a variety of transformations to make standard text presentable as HTML.
func ToDataAttr ¶
Helper function to convert a name from camel case to using dashes to separated words. data-* html attributes have special conversion rules. Attribute names should always be lower case. Dashes in the name get converted to camel case javascript variable names by jQuery. For example, if you want to pass the value with key name "testVar" to javascript by printing it in the html, you would use this function to help convert it to "data-test-var", after which you can retrieve in javascript by calling ".data('testVar')". on the object. This will also test for the existence of a camel case string it cannot handle due to a bug in jQuery
Example ¶
s, _ := ToDataAttr("thisIsMyTest") fmt.Println(s)
Output: this-is-my-test
func ToDataJqKey ¶
Helper function to convert a name from data attribute naming convention to camel case. data-* html attributes have special conversion rules. Key names should always be lower case. Dashes in the name get converted to camel case javascript variable names by jQuery. For example, if you want to pass the value with key name "testVar" to javascript by printing it in the html, you would use this function to help convert it to "data-test-var", after which you can retrieve in jQuery by calling ".data('testVar')". on the object.
Example ¶
s, _ := ToDataJqKey("this-is-my-test") fmt.Println(s)
Output: thisIsMyTest
Types ¶
type Attributer ¶
type Attributer interface {
Attributes(...interface{}) *Attributes
}
Attributer is a general purpose interface for objects that return attributes based on information given.
type Attributes ¶
type Attributes struct {
maps.StringSliceMap // Use an ordered string map so that each time we draw the attributes, they do not change order
}
An html attribute manager. Use SetAttribute to set specific attribute values, and then convert it to a string to get the attributes in a version embeddable in an html tag.
func NewAttributes ¶
func NewAttributes() *Attributes
NewAttributes initializes a group of html attributes.
func NewAttributesFrom ¶
func NewAttributesFrom(i maps.StringMapI) *Attributes
Example ¶
a := NewAttributesFromMap(map[string]string{"id": "1", "name": "test"}) fmt.Println(a.Get("id"))
Output: 1
func NewAttributesFromMap ¶
func NewAttributesFromMap(i map[string]string) *Attributes
func (*Attributes) AddClass ¶
func (a *Attributes) AddClass(v string) *Attributes
Example ¶
a := NewAttributes() a.AddClass("this") a.AddClass("that") fmt.Println(a)
Output: class="this that"
func (*Attributes) AddClassChanged ¶
func (a *Attributes) AddClassChanged(v string) bool
Use AddClass to add a class or classes. Multiple classes can be separated by spaces. If a class is not present, the class will be added to the end of the class list If a class is present, it will not be added, and the position of the current class in the list will not change
func (*Attributes) Class ¶
func (a *Attributes) Class() string
Return the value of the class attribute.
func (*Attributes) Clone ¶
func (a *Attributes) Clone() *Attributes
Clone returns a copy of the attributes
func (*Attributes) Copy ¶
func (a *Attributes) Copy() *Attributes
func (*Attributes) DataAttribute ¶
func (a *Attributes) DataAttribute(name string) string
DataAttribute gets the data-* attribute value that was set previously. Does NOT call into javascript to return a value that was set on the browser side. You need to use another mechanism to retrieve that.
func (*Attributes) GetStyle ¶
func (a *Attributes) GetStyle(name string) string
Style gives you the value of a single style attribute value. If you want all the attributes as a style string, use Attribute("style").
func (*Attributes) HasClass ¶
func (a *Attributes) HasClass(c string) bool
HasClass return true if the given class is in the class list in the class attribute.
Example ¶
a := NewAttributes() if !a.HasClass("that") { fmt.Println("Not found") } a.SetClass("this that other") if a.HasClass("that") { fmt.Println("found") }
Output: Not found found
func (*Attributes) HasDataAttribute ¶
func (a *Attributes) HasDataAttribute(name string) bool
HasDataAttribute returns true if the data attribute is set.
func (*Attributes) HasStyle ¶
func (a *Attributes) HasStyle(name string) bool
Example ¶
a := NewAttributes() b := []bool{} var found bool found = a.HasStyle("height") b = append(b, found) a.SetStyle("height", strconv.Itoa(10)) found = a.HasStyle("height") b = append(b, found) fmt.Println(b)
Output: [false true]
func (*Attributes) IsDisabled ¶
func (a *Attributes) IsDisabled() bool
func (*Attributes) IsDisplayed ¶
func (a *Attributes) IsDisplayed() bool
func (*Attributes) Override ¶
func (a *Attributes) Override(i maps.StringMapI) *Attributes
Override returns a new Attributes structure with the current attributes merged with the given attributes. Conflicts are won by the given overrides
Example ¶
a := NewAttributes() a.SetClass("this") a.SetStyle("height", "4em") b := NewAttributes() b.Set("class", "that") b.SetStyle("width", strconv.Itoa(6)) a = a.Override(b) fmt.Println(a)
Output:
func (*Attributes) RemoveAttribute ¶
func (a *Attributes) RemoveAttribute(name string) bool
RemoveAttribute removes the named attribute. Returns true if the attribute existed.
func (*Attributes) RemoveClass ¶
func (a *Attributes) RemoveClass(v string) bool
Use RemoveClass to remove the named class from the list of classes in the class attribute.
func (*Attributes) RemoveClassesWithPrefix ¶
func (a *Attributes) RemoveClassesWithPrefix(v string) bool
Use RemoveClasses to remove classes with the given prefix. Many CSS frameworks use families of classes, which are built up from a base family name. For example, Bootstrap uses 'col-lg-6' to represent a table that is 6 units wide on large screens and Foundation uses 'large-6' to do the same thing. This utility removes classes that start with a particular prefix to remove whatever sizing class was specified. Returns true if the list actually changed.
func (*Attributes) RemoveDataAttribute ¶
func (a *Attributes) RemoveDataAttribute(name string) bool
RemoveDataAttribute removes the named data attribute. Returns true if the data attribute existed.
func (*Attributes) RemoveStyle ¶
func (a *Attributes) RemoveStyle(name string) (changed bool)
RemoveStyle removes the style from the style list. Returns true if there was a changed.
Example ¶
a := NewAttributes() a.SetStyle("height", "10") a.SetStyle("width", strconv.Itoa(5)) a.RemoveStyle("height") fmt.Println(a)
Output: style="width:5px"
func (*Attributes) Set ¶
func (a *Attributes) Set(name string, v string) *Attributes
Set is similar to SetChanged, but instead returns an attribute pointer so it can be chained. Will panic on errors. Use this when you are setting attributes using implicit strings. Set v to an empty string to create a boolean attribute.
func (*Attributes) SetChanged ¶
func (a *Attributes) SetChanged(name string, v string) (changed bool, err error)
SetChanged sets the value of an attribute. Looks for special attributes like "class" and "style" to do some error checking on them. Returns changed if something in the attribute structure changed, which is useful to determine whether to send the changed control to the browser. Returns err if the given attribute name or value is not valid.
func (*Attributes) SetClass ¶
func (a *Attributes) SetClass(v string) *Attributes
Example ¶
a := NewAttributes() a.SetClass("this") a.SetClass("+ that") s := a.Class() fmt.Println(s)
Output: this that
func (*Attributes) SetClassChanged ¶
func (a *Attributes) SetClassChanged(v string) bool
SetClass sets the class attribute to the value given. If you prefix the value with "+ " the given value will be appended to the end of the current class list. If you prefix the value with "- " the given value will be removed from an class list. Otherwise the current class value is replaced. Returns whether something actually changed or not. v can be multiple classes separated by a space
func (*Attributes) SetDataAttribute ¶
func (a *Attributes) SetDataAttribute(name string, v string) *Attributes
SetDataAttribute sets the given data attribute. Note that data attribute keys must be in camelCase notation and connot be hyphenated. camelCase will get converted to kebab-case in html, and converted back to camelCase when referring to the data attribute using jQuery.data.
func (*Attributes) SetDataAttributeChanged ¶
func (a *Attributes) SetDataAttributeChanged(name string, v string) (changed bool, err error)
SetDataAttribute sets the given value as an html "data-*" attribute. The named value will be retrievable in jQuery by using
$obj.data("name");
Note: Data name cases are handled specially in jQuery. data-* attribute names are supposed to be online lower case. jQuery converts dashed notation to camelCase. In other words, we give it a camelCase name here, it shows up in the html as a dashed name, and then you retrieve it using javascript as camelCase again.
For example, if your html looks like this:
<div id='test1' data-test-case="my test"></div>
You would get that value in jQuery by doing:
$j('#test1').data('testCase');
Conversion to special html data-* name formatting is handled here automatically. So if you SetDataAttribute('testCase') here, you can get it using .data('testCase') in jQuery
func (*Attributes) SetDisabled ¶
func (a *Attributes) SetDisabled(d bool) *Attributes
func (*Attributes) SetDisplay ¶
func (a *Attributes) SetDisplay(d string) *Attributes
func (*Attributes) SetID ¶
func (a *Attributes) SetID(i string) *Attributes
func (*Attributes) SetIDChanged ¶
func (a *Attributes) SetIDChanged(i string) (changed bool, err error)
Set the id to the given value. Returns true if something changed.
func (*Attributes) SetStyle ¶
func (a *Attributes) SetStyle(name string, v string) *Attributes
func (*Attributes) SetStyleChanged ¶
func (a *Attributes) SetStyleChanged(name string, v string) (changed bool, err error)
SetStyle sets the given style to the given value. If the value is prefixed with a plus, minus, multiply or divide, and then a space, it assumes that a number will follow, and the specified operation will be performed in place on the current value. For example, SetStyle ("height", "* 2") will double the height value without changing the unit specifier. When referring to a value that can be a length, you can use numeric values. In this case, "0" will be passed unchanged, but any other number will automatically get a "px" suffix.
func (*Attributes) SetStyles ¶
func (a *Attributes) SetStyles(s *Style) *Attributes
SetStyle merges the given styles with the current styles. The given style wins on collision.
func (*Attributes) SetStylesTo ¶
func (a *Attributes) SetStylesTo(s string) *Attributes
SetStylesTo sets the styles using a traditional css style string with colon and semicolon separatators
func (*Attributes) String ¶
func (a *Attributes) String() string
String returns the attributes escaped and encoded, ready to be placed in an html tag For consistency, it will output the following attributes in the following order if it finds them. Remaining tags will be output in random order: id, name, class
func (*Attributes) StyleMap ¶
func (a *Attributes) StyleMap() *Style
Returns a special Style structure which lets you refer to the styles as a string map
func (*Attributes) StyleString ¶
func (a *Attributes) StyleString() string
Returns the css style string, or a blank string if there is none
type LabelDrawingMode ¶
type LabelDrawingMode int
const ( LabelDefault LabelDrawingMode = iota // Label mode is defined elsewhere, like in a config setting LabelBefore // Label tag is before the control's tag, and terminates before the control LabelAfter // Label tag is after the control's tag, and starts after the control LabelWrapBefore // Label tag is before the control's tag, and wraps the control tag LabelWrapAfter // Label tag is after the control's tag, and wraps the control tag )
The label drawing mode describes how to draw a label when it is drawn. Various CSS frameworks expect it a certain way. Many are not very forgiving when you don't do it the way they expect.
type Style ¶
Style makes it easy to add and manipulate individual properties in a generated style sheet Its main use is for generating a style attribute in an HTML tag It implements the String interface to get the style properties as an HTML embeddable string
func (Style) Get ¶
Example ¶
s := NewStyle() s.SetTo("height: 9em; width: 100%; position:absolute") fmt.Print(s.Get("width"))
Output: 100%
func (*Style) RemoveAll ¶
func (s *Style) RemoveAll()
RemoveAll resets the style to contain no styles
Example ¶
s := NewStyle() s.SetTo("height: 9em; width: 100%; position:absolute") s.RemoveAll() fmt.Print(s)
Output:
func (Style) Set ¶
Example (A) ¶
s := NewStyle() s.Set("height", "9") fmt.Print(s)
Output: height:9px
Example (B) ¶
s := NewStyle() s.SetTo("height:9px") s.Set("height", "+ 10") fmt.Print(s)
Output: height:19px
type TagBuilder ¶
type TagBuilder struct {
// contains filtered or unexported fields
}
Use a TagBuilder to create a tag using a builder pattern, starting out with the tag name and slowly adding parts to it, describing it, until you are ready to print out the entire html tag. The zero value is usable.
func NewTagBuilder ¶
func NewTagBuilder() *TagBuilder
NewTagBuilder starts a tag build, though you can use a tag builder from its zero value too.
func (*TagBuilder) Class ¶
func (b *TagBuilder) Class(class string) *TagBuilder
Class sets the class attribute to the value given. If you prefix the value with "+ " the given value will be appended to the end of the current class list. If you prefix the value with "- " the given value will be removed from the class list. Otherwise the current class value is replaced. The given class can be multiple classes separated by a space.
func (*TagBuilder) InnerHtml ¶
func (b *TagBuilder) InnerHtml(html string) *TagBuilder
InnerHtml sets the inner html of the tag. Remember this is HTML, and will not be escaped.
func (*TagBuilder) InnerText ¶
func (b *TagBuilder) InnerText(text string) *TagBuilder
InnerText sets the inner part of the tag to the given text. The text will be escaped if needed.
func (*TagBuilder) IsVoid ¶
func (b *TagBuilder) IsVoid() *TagBuilder
By default, tags have inner html. This will make the builder output a void tag instead.
func (*TagBuilder) Link ¶
func (b *TagBuilder) Link(href string) *TagBuilder
Link is a shortcut that will set the tag to "a" and the "href" to the given destination. This is not the same as an actual "link" tag, which points to resources from the header.
func (*TagBuilder) Set ¶
func (b *TagBuilder) Set(attribute string, value string) *TagBuilder
Set sets the attribute to the given value
func (*TagBuilder) String ¶
func (b *TagBuilder) String() string
String ends the builder and returns the html