fshtml

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// NL 换行: \n
	NL = Bytes("\n")

	// BR HTML 换行 br
	BR = Bytes("<br/>")

	// HR HTML 分割符 hr
	HR = Bytes("<hr/>")
)
View Source
var ErrEmptyTagName = errors.New("empty tag name")

ErrEmptyTagName tag 值为空的错误

Functions

func Add

func Add(to HasChildren, add ...Element)

Add 给指定的对象添加子元素

func AddClass

func AddClass(w AttrsMapper, class ...string)

AddClass 添加 class 属性

func DeleteAttr

func DeleteAttr(w AttrsMapper, key string, values ...string)

DeleteAttr 删除指定的属性值

func DeleteClass

func DeleteClass(w AttrsMapper, class ...string)

DeleteClass 删除 class 属性

func SetAccept

func SetAccept(w AttrsMapper, accept string)

SetAccept 设置 accept 属性

func SetAction

func SetAction(w AttrsMapper, action string)

SetAction 设置 action 属性

func SetAsync

func SetAsync(w AttrsMapper)

SetAsync 设置 async 属性

func SetAttr

func SetAttr(w AttrsMapper, key string, value ...string)

SetAttr 设置属性值

func SetAttrNoValue

func SetAttrNoValue(w AttrsMapper, key string)

SetAttrNoValue 设置只有 key,不需要 value 的属性

func SetAutoComplete

func SetAutoComplete(w AttrsMapper, on bool)

SetAutoComplete 设置 autocomplete 属性

func SetChecked

func SetChecked(w AttrsMapper, checked bool)

SetChecked 设置 form 属性

func SetClass

func SetClass(w AttrsMapper, class ...string)

SetClass 设置 class 属性

func SetDisabled

func SetDisabled(w AttrsMapper, disabled bool)

SetDisabled 设置 disabled 属性

func SetForm

func SetForm(w AttrsMapper, form string)

SetForm 设置 form 属性

func SetFormAction

func SetFormAction(w AttrsMapper, formAction string)

SetFormAction 设置 formaction 属性

func SetHeight

func SetHeight(w AttrsMapper, height string)

SetHeight 设置元素的 height

func SetID

func SetID(w AttrsMapper, id string)

SetID 设置元素的 id

func SetLang

func SetLang(w AttrsMapper, lang string)

SetLang 设置元素的 lang 属性

如 en-US、zh-CN

func SetList

func SetList(w AttrsMapper, list string)

SetList 设置 list 属性

func SetMax

func SetMax(w AttrsMapper, max string)

SetMax 设置 max 属性

func SetMaxLength

func SetMaxLength(w AttrsMapper, maxLen int)

SetMaxLength 设置 maxlength 属性

func SetMethod

func SetMethod(w AttrsMapper, method string)

SetMethod 设置 method 属性

func SetMin

func SetMin(w AttrsMapper, min string)

SetMin 设置 min 属性

func SetMinLength

func SetMinLength(w AttrsMapper, minLen int)

SetMinLength 设置 minlength 属性

func SetName

func SetName(w AttrsMapper, name string)

SetName 设置元素的 name

func SetOnBlur

func SetOnBlur(w AttrsMapper, script string)

SetOnBlur 设置 onblur 属性

func SetOnChange

func SetOnChange(w AttrsMapper, script string)

SetOnChange 设置 onchange 属性

func SetOnClick

func SetOnClick(w AttrsMapper, script string)

SetOnClick 设置 onclick 属性

func SetOnFocus

func SetOnFocus(w AttrsMapper, script string)

SetOnFocus 设置 onfocus 属性

func SetOnFormChange

func SetOnFormChange(w AttrsMapper, script string)

SetOnFormChange 设置 onformchange 属性

func SetOnFormInput

func SetOnFormInput(w AttrsMapper, script string)

SetOnFormInput 设置 onforminput 属性

func SetOnInput

func SetOnInput(w AttrsMapper, script string)

SetOnInput 设置 oninput 属性

func SetOnInvalid

func SetOnInvalid(w AttrsMapper, script string)

SetOnInvalid 设置 oninvalid 属性

func SetOnKeyDown

func SetOnKeyDown(w AttrsMapper, script string)

SetOnKeyDown 设置 onkeydown 属性

func SetOnKeyPress

func SetOnKeyPress(w AttrsMapper, script string)

SetOnKeyPress 设置 onkeypress 属性

func SetOnKeyUp

func SetOnKeyUp(w AttrsMapper, script string)

SetOnKeyUp 设置 onkeyup 属性

func SetOnReset

func SetOnReset(w AttrsMapper, script string)

SetOnReset 设置 onreset 属性

func SetOnSelect

func SetOnSelect(w AttrsMapper, script string)

SetOnSelect 设置 onselect 属性

func SetOnSubmit

func SetOnSubmit(w AttrsMapper, script string)

SetOnSubmit 设置 onsubmit 属性

func SetReadOnly

func SetReadOnly(w AttrsMapper, readonly bool)

SetReadOnly 设置 readonly 属性

func SetRequired

func SetRequired(w AttrsMapper, required bool)

SetRequired 设置 required 属性

func SetSelected

func SetSelected(w AttrsMapper, checked bool)

SetSelected 设置 selected 属性

func SetSize

func SetSize(w AttrsMapper, size int)

SetSize 设置元素的 size

func SetSrc

func SetSrc(w AttrsMapper, src string)

SetSrc 设置 src 属性

func SetTarget

func SetTarget(w AttrsMapper, target string)

SetTarget 设置 target 属性

func SetTitle

func SetTitle(w AttrsMapper, title string)

SetTitle 设置 title 属性

func SetType

func SetType(w AttrsMapper, tp string)

SetType 设置 type 属性

func SetValue

func SetValue(w AttrsMapper, value string)

SetValue 设置 value 属性

func SetWidth

func SetWidth(w AttrsMapper, width string)

SetWidth 设置元素的 width

Types

type A

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

A 地址标签 <a>

func NewA

func NewA(href string) *A

NewA 创建一个 <a>

func (*A) HTML

func (a *A) HTML() ([]byte, error)

HTML 转换为 html

func (*A) Href

func (a *A) Href(href string) *A

Href 设置 href 属性

func (*A) HrefLang

func (a *A) HrefLang(hrefLang string) *A

HrefLang 设置 hreflang 属性

func (*A) Ping

func (a *A) Ping(ping string) *A

Ping 设置 ping 属性

func (*A) Rel

func (a *A) Rel(rel string) *A

Rel 设置 rel 属性

func (*A) Target

func (a *A) Target(target string) *A

Target 设置 target 属性

func (*A) Title

func (a *A) Title(title string) *A

Title 设置 Title 属性

func (*A) Type

func (a *A) Type(tp string) *A

Type 设置 type 属性

type Any

type Any struct {
	// Tag 标签名称,必填,如 div
	Tag string

	// WithAttrs 可选,属性信息
	WithAttrs

	// Body 内容,可选
	Body Elements

	// SelfClose 当前标签是否自关闭,默认为 false
	// 如 img 标签就是自关闭的:<img src="/a.jpg"/>
	SelfClose bool
}

Any 一块 HTML 内容

func NewAny

func NewAny(tag string) *Any

NewAny 创建任意的 tag

func NewArticle

func NewArticle() *Any

NewArticle 创建一个 <article>

func NewBody

func NewBody() *Any

NewBody 创建一个 <body>

func NewButton

func NewButton(value string) *Any

NewButton 创建一个 button 标签

func NewCode

func NewCode() *Any

NewCode 创建一个 <code>

func NewDL

func NewDL() *Any

NewDL 创建一个 <dl>

func NewDT

func NewDT() *Any

NewDT 创建一个 <dt>

func NewDiv

func NewDiv() *Any

NewDiv 创建一个 <div>

func NewFieldSet

func NewFieldSet() *Any

NewFieldSet 创建一个 fieldset

func NewFigCaption

func NewFigCaption() *Any

NewFigCaption 创建一个 <figcaption>

func NewFigure

func NewFigure() *Any

NewFigure 创建一个 <figure>

func NewForm

func NewForm(method string, action string) *Any

NewForm 创建一个 form

func NewHTML

func NewHTML() *Any

NewHTML html 标签

Example
package main

import (
	"fmt"

	"github.com/fsgo/fsgo/fshtml"
)

func main() {
	h := fshtml.NewHTML()
	fshtml.Add(h,
		fshtml.WithAny(fshtml.NewHead(), func(a *fshtml.Any) {
			fshtml.Add(a, fshtml.NewTitle(fshtml.Text("hello")))
		}),
		fshtml.WithAny(fshtml.NewBody(), func(a *fshtml.Any) {
			fshtml.Add(a, fshtml.Text("Hello World"))
		}),
	)
	got, _ := h.HTML()
	fmt.Println(string(got))
}
Output:

<html><head><title>hello</title></head><body>Hello World</body></html>

func NewHead

func NewHead() *Any

NewHead 创建一个 <head>

func NewInput

func NewInput(tp string, name string) *Any

NewInput 创建一个 input 标签

func NewLabel

func NewLabel(e Element) *Any

NewLabel 创建一个 label

func NewLegend

func NewLegend(e Element) *Any

NewLegend 创建一个 legend

func NewNav

func NewNav() *Any

NewNav 创建一个 <nav>

func NewOl

func NewOl(values StringSlice) *Any

NewOl 转换为 ol

Example
package main

import (
	"fmt"

	"github.com/fsgo/fsgo/fshtml"
)

func main() {
	values := []string{"1", "2", "3"}
	ul := fshtml.NewOl(values)
	style := &fshtml.StyleAttr{}
	_ = style.Width("180px").Height("20px").SetTo(ul)

	got, _ := ul.HTML()
	fmt.Println(string(got))
}
Output:

<ol style="width:180px; height:20px"><li>1</li><li>2</li><li>3</li></ol>

func NewOption

func NewOption(value string, b Element) *Any

NewOption 创建一个 option

func NewP

func NewP() *Any

NewP 创建一个 <p>

func NewPre

func NewPre() *Any

NewPre 创建一个 <pre>

func NewScript

func NewScript() *Any

NewScript script 标签

func NewSelect

func NewSelect(opts ...Element) *Any

NewSelect 创建一个 select

func NewStyle

func NewStyle() *Any

NewStyle style 标签

func NewSubmit

func NewSubmit(value string) *Any

NewSubmit 创建一个 submit 标签

func NewTd

func NewTd(val ...Element) *Any

NewTd 创建一个新的 td

func NewTh

func NewTh(val ...Element) *Any

NewTh 创建一个新的 th

func NewTitle

func NewTitle(c Element) *Any

NewTitle 创建一个 <title>

func NewUl

func NewUl(values StringSlice) *Any

NewUl 转换为 ul

Example
package main

import (
	"fmt"

	"github.com/fsgo/fsgo/fshtml"
)

func main() {
	values := []string{"1", "2", "3"}
	ul := fshtml.NewUl(values)
	got, _ := ul.HTML()
	fmt.Println(string(got))
}
Output:

<ul><li>1</li><li>2</li><li>3</li></ul>

func WithAny

func WithAny(a *Any, fn func(*Any)) *Any

WithAny 对 any 元素进行处理

func (*Any) Add

func (c *Any) Add(values ...Element)

Add 添加子元素

func (*Any) HTML

func (c *Any) HTML() ([]byte, error)

HTML 实现 Element 接口

type Attr

type Attr struct {
	// Key 属性的名字
	Key string

	// Values 属性值,可以有多个
	Values []string
}

Attr 一个属性

func (*Attr) Add

func (a *Attr) Add(value ...string)

Add 添加新的属性值

func (*Attr) Delete

func (a *Attr) Delete(value ...string)

Delete 删除属性值

func (*Attr) First

func (a *Attr) First() string

First 返回首个属性值

func (*Attr) Set

func (a *Attr) Set(value ...string)

Set 设置属性值

type Attrs

type Attrs struct {
	// Sep 多个属性间的连接符,当为空时,使用默认值 " " (一个空格)
	Sep string

	// KVSep key 和 value 之间的连接符,当为空时,使用默认值 =
	KVSep string
	// contains filtered or unexported fields
}

Attrs 多个属性

func (*Attrs) Attr

func (a *Attrs) Attr(key string) *Attr

Attr 返回一个指定的属性,若不存在,返回 nil

func (*Attrs) Delete

func (a *Attrs) Delete(keys ...string)

Delete 删除指定 key 的属性

func (*Attrs) GetKVSep

func (a *Attrs) GetKVSep() string

GetKVSep key 和 value 之间的连接符,当为空时, 返回默认值 =

func (*Attrs) GetSep

func (a *Attrs) GetSep() string

GetSep 多个属性间的连接符,当为空时,返回默认值 " " (一个空格)

func (*Attrs) HTML

func (a *Attrs) HTML() ([]byte, error)

HTML 转换为 HTML

func (*Attrs) Keys

func (a *Attrs) Keys() []string

Keys 返回所有属性的 key

func (*Attrs) MustAttr

func (a *Attrs) MustAttr(key string) *Attr

MustAttr 返回一个指定的属性,若不存在,返回 nil

func (*Attrs) Set

func (a *Attrs) Set(attr ...*Attr)

Set 设置属性值

type AttrsMapper

type AttrsMapper interface {
	MustAttrs() *Attrs
	FindAttrs() *Attrs
}

AttrsMapper 具有 AttrsMapper 方法

type Bytes

type Bytes []byte

Bytes 将 []byte 转换为 Element 类型,原样输出 HTML

func (Bytes) HTML

func (b Bytes) HTML() ([]byte, error)

HTML 实现 Element 接口

type Comment

type Comment string

Comment 注释

func (Comment) HTML

func (c Comment) HTML() ([]byte, error)

HTML 转换为 HTML

type Element

type Element interface {
	HTML() ([]byte, error)
}

Element 所有 HTML 组件的基础定义

type Elements

type Elements []Element

Elements alias of []Element

func ToElements

func ToElements(es ...Element) Elements

ToElements 转换为 Elements 类型

func (*Elements) Add

func (hs *Elements) Add(values ...Element)

Add append to the end

func (Elements) HTML

func (hs Elements) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*Elements) InsertFront

func (hs *Elements) InsertFront(values ...Element)

InsertFront to the frontend

func (*Elements) Set

func (hs *Elements) Set(values ...Element)

Set 设置内容

type HasChildren

type HasChildren interface {
	Add(values ...Element)
	Element
}

HasChildren 允许添加子元素

type IMG

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

IMG 图片 img 标签

func NewIMG

func NewIMG(src string) *IMG

NewIMG 创建一个 img 标签

func (*IMG) ALT

func (m *IMG) ALT(alt string) *IMG

ALT 设置 alt 属性

func (*IMG) HTML

func (m *IMG) HTML() ([]byte, error)

HTML 转换为 html

func (*IMG) SRC

func (m *IMG) SRC(src string) *IMG

SRC 设置 src 属性

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

Link 页面元素 link 标签

func NewLink() *Link

NewLink 创建一个新的 link 标签

func (*Link) HTML

func (a *Link) HTML() ([]byte, error)

HTML 转换为 html

func (*Link) Href

func (a *Link) Href(href string) *Link

Href 设置 href 属性

func (*Link) Rel

func (a *Link) Rel(rel string) *Link

Rel 设置 rel 属性

func (*Link) Type

func (a *Link) Type(tp string) *Link

Type 设置 tp 属性

type Meta

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

Meta 页面元信息标签 meth

func NewMeta

func NewMeta() *Meta

NewMeta 创建一个新的 <meta>

func (*Meta) Charset

func (a *Meta) Charset(charset string) *Meta

Charset 设置 charset 属性

func (*Meta) Content

func (a *Meta) Content(content string) *Meta

Content 设置 content 属性

func (*Meta) HTML

func (a *Meta) HTML() ([]byte, error)

HTML 转换为 html

func (*Meta) HTTPEquiv

func (a *Meta) HTTPEquiv(equiv string) *Meta

HTTPEquiv 设置 http-equiv 属性

func (*Meta) Media

func (a *Meta) Media(media string) *Meta

Media 设置 media 属性

func (*Meta) Name

func (a *Meta) Name(name string) *Meta

Name 设置 name 属性

type Pre added in v0.0.6

type Pre string

Pre 输出 HTML 时添加 pre 标签

func (Pre) HTML added in v0.0.6

func (p Pre) HTML() ([]byte, error)

type PreByte added in v0.0.6

type PreByte []byte

PreByte 输出 HTML 时添加 pre 标签

func (PreByte) HTML added in v0.0.6

func (p PreByte) HTML() ([]byte, error)

type String

type String string

String 将 String 转换为 Element 类型,原样输出 HTML

func (String) HTML

func (s String) HTML() ([]byte, error)

HTML 实现 Element 接口

type StringSlice

type StringSlice []string

StringSlice 将 []string 转换为 Element 类型

func (StringSlice) HTML added in v0.0.6

func (ss StringSlice) HTML() ([]byte, error)

func (StringSlice) ToElements

func (ss StringSlice) ToElements(tag string, fn func(b *Any)) Elements

ToElements 转换为 字段 tag 的 []Element

type StyleAttr

type StyleAttr struct {
	WithAttrs
}

StyleAttr style 属性

func (*StyleAttr) BackgroundColor

func (s *StyleAttr) BackgroundColor(color string) *StyleAttr

BackgroundColor 设置背景演示

func (*StyleAttr) Border

func (s *StyleAttr) Border(val string) *StyleAttr

Border 设置边框属性

func (*StyleAttr) Color

func (s *StyleAttr) Color(color string) *StyleAttr

Color 设置前景/字体颜色

func (*StyleAttr) Font

func (s *StyleAttr) Font(val string) *StyleAttr

Font 设置字体

func (*StyleAttr) FontFamily

func (s *StyleAttr) FontFamily(val string) *StyleAttr

FontFamily 设置字体系列(字体族)

func (*StyleAttr) FontSize

func (s *StyleAttr) FontSize(val string) *StyleAttr

FontSize 设置字体大小

func (*StyleAttr) FontWeight

func (s *StyleAttr) FontWeight(val string) *StyleAttr

FontWeight 设置字体粗细

func (*StyleAttr) Get

func (s *StyleAttr) Get(key string) string

Get 获取属性值

func (*StyleAttr) HTML

func (s *StyleAttr) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*StyleAttr) Height

func (s *StyleAttr) Height(h string) *StyleAttr

Height 设置高度

func (*StyleAttr) Margin

func (s *StyleAttr) Margin(val string) *StyleAttr

Margin 设置外边距

func (*StyleAttr) MaxHeight

func (s *StyleAttr) MaxHeight(h string) *StyleAttr

MaxHeight 设置最大高度

func (*StyleAttr) MaxWidth

func (s *StyleAttr) MaxWidth(w string) *StyleAttr

MaxWidth 设置最大新宽度

func (*StyleAttr) MinHeight

func (s *StyleAttr) MinHeight(h string) *StyleAttr

MinHeight 设置最小高度

func (*StyleAttr) MinWidth

func (s *StyleAttr) MinWidth(w string) *StyleAttr

MinWidth 设置最小宽度

func (*StyleAttr) Padding

func (s *StyleAttr) Padding(val string) *StyleAttr

Padding 设置内边距

func (*StyleAttr) SetTo

func (s *StyleAttr) SetTo(a AttrsMapper) error

SetTo 将样式信息设置到指定的属性集合

func (*StyleAttr) TextAlign

func (s *StyleAttr) TextAlign(align string) *StyleAttr

TextAlign 设置内容对齐方式

func (*StyleAttr) Width

func (s *StyleAttr) Width(w string) *StyleAttr

Width 设置宽度

type Table1

type Table1 struct {
	WithAttrs
	// contains filtered or unexported fields
}

Table1 一个简单的表格

func (*Table1) AddRow

func (t *Table1) AddRow(cells ...Element)

AddRow 添加一行内容

func (*Table1) AddRows

func (t *Table1) AddRows(rows ...[]Element)

AddRows 添加多行内容

func (*Table1) HTML

func (t *Table1) HTML() ([]byte, error)

HTML 实现 Element 接口

func (*Table1) SetFooter

func (t *Table1) SetFooter(cells ...Element)

SetFooter 设置表格的页脚

func (*Table1) SetHeader

func (t *Table1) SetHeader(cells ...Element)

SetHeader 设置表头

type Text

type Text String

Text 文本,输出的时候会自动调用 html.EscapeString

func (Text) HTML

func (b Text) HTML() ([]byte, error)

HTML 实现 Element 接口

type WithAttrs

type WithAttrs struct {
	Attrs *Attrs
}

WithAttrs 具有 attrs 属性

func (*WithAttrs) FindAttrs

func (w *WithAttrs) FindAttrs() *Attrs

FindAttrs 返回当前的 Attrs

func (*WithAttrs) MustAttrs

func (w *WithAttrs) MustAttrs() *Attrs

MustAttrs 若 attrs 不存在,则创建 并返回 attrs

Jump to

Keyboard shortcuts

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