package html

import "github.com/tdewolff/minify/v2/html"

Package html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html.

Index

Examples

Variables

var ASPTemplateDelims = [2]string{"<%", "%>"}
var EJSTemplateDelims = [2]string{"<%", "%>"}
var EntitiesMap = map[string][]byte{ /* 1092 elements not displayed */

}

EntitiesMap are all named character entities.

var GoTemplateDelims = [2]string{"{{", "}}"}
var HandlebarsTemplateDelims = [2]string{"{{", "}}"}
var MustacheTemplateDelims = [2]string{"{{", "}}"}
var PHPTemplateDelims = [2]string{"<?", "?>"}
var TextRevEntitiesMap = map[byte][]byte{
	'<': []byte("&lt;"),
}

TextRevEntitiesMap is a map of escapes.

Functions

func Minify

func Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error

Minify minifies HTML data, it reads from r and writes to w.

Example

Code:

{
	m := minify.New()
	m.AddFunc("text/html", Minify)
	m.AddFunc("text/css", css.Minify)
	m.AddFunc("image/svg+xml", svg.Minify)
	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
	m.AddFuncRegexp(regexp.MustCompile("[/+]xml$"), xml.Minify)

	// set URL to minify link locations too
	m.URL, _ = url.Parse("https://www.example.com/")
	if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {
		panic(err)
	}
}
Example (Options)

Code:

{
	m := minify.New()
	m.Add("text/html", &Minifier{
		KeepDefaultAttrVals: true,
		KeepWhitespace:      true,
	})

	if err := m.Minify("text/html", os.Stdout, os.Stdin); err != nil {
		panic(err)
	}
}
Example (Reader)

Code:

{
	b := bytes.NewReader([]byte("<html><body><h1>Example</h1></body></html>"))

	m := minify.New()
	m.Add("text/html", &Minifier{})

	r := m.Reader("text/html", b)
	if _, err := io.Copy(os.Stdout, r); err != nil {
		panic(err)
	}
	// Output: <h1>Example</h1>
}

Output:

<h1>Example</h1>
Example (Writer)

Code:

{
	m := minify.New()
	m.Add("text/html", &Minifier{})

	w := m.Writer("text/html", os.Stdout)
	w.Write([]byte("<html><body><h1>Example</h1></body></html>"))
	w.Close()
	// Output: <h1>Example</h1>
}

Output:

<h1>Example</h1>

Types

type Hash

type Hash uint32

Hash defines perfect hashes for a predefined list of strings

const (
	A                        Hash = 0x1     // a
	Abbr                     Hash = 0x40004 // abbr
	About                    Hash = 0x5     // about
	Accept                   Hash = 0xc06   // accept
	Accept_Charset           Hash = 0xc0e   // accept-charset
	Accesskey                Hash = 0x2c09  // accesskey
	Acronym                  Hash = 0x3507  // acronym
	Action                   Hash = 0x26006 // action
	Address                  Hash = 0x6d07  // address
	Allow                    Hash = 0x31f05 // allow
	Allowfullscreen          Hash = 0x31f0f // allowfullscreen
	Amp_Boilerplate          Hash = 0x5e0f  // amp-boilerplate
	Applet                   Hash = 0xee06  // applet
	Area                     Hash = 0x2c304 // area
	Article                  Hash = 0x22507 // article
	As                       Hash = 0x2102  // as
	Aside                    Hash = 0x9205  // aside
	Async                    Hash = 0x8a05  // async
	Audio                    Hash = 0x9d05  // audio
	Autocapitalize           Hash = 0xc30e  // autocapitalize
	Autocomplete             Hash = 0xd10c  // autocomplete
	Autofocus                Hash = 0xe309  // autofocus
	Autoplay                 Hash = 0xfc08  // autoplay
	B                        Hash = 0x101   // b
	Base                     Hash = 0x2004  // base
	Basefont                 Hash = 0x2008  // basefont
	Bb                       Hash = 0x40102 // bb
	Bdi                      Hash = 0x8303  // bdi
	Bdo                      Hash = 0x3dc03 // bdo
	Big                      Hash = 0x12f03 // big
	Blocking                 Hash = 0x13208 // blocking
	Blockquote               Hash = 0x13a0a // blockquote
	Body                     Hash = 0x804   // body
	Br                       Hash = 0x14b02 // br
	Button                   Hash = 0x14406 // button
	Canvas                   Hash = 0x8e06  // canvas
	Caption                  Hash = 0x23707 // caption
	Capture                  Hash = 0x10d07 // capture
	Center                   Hash = 0x24f06 // center
	Charset                  Hash = 0x1307  // charset
	Checked                  Hash = 0x37707 // checked
	Cite                     Hash = 0x14d04 // cite
	Class                    Hash = 0x15a05 // class
	Code                     Hash = 0x17604 // code
	Col                      Hash = 0x17f03 // col
	Colgroup                 Hash = 0x17f08 // colgroup
	Color                    Hash = 0x19e05 // color
	Cols                     Hash = 0x1a304 // cols
	Colspan                  Hash = 0x1a307 // colspan
	Content                  Hash = 0x1b107 // content
	Contenteditable          Hash = 0x1b10f // contenteditable
	Controls                 Hash = 0x1cc08 // controls
	Coords                   Hash = 0x1e306 // coords
	Crossorigin              Hash = 0x2160b // crossorigin
	Data                     Hash = 0xad04  // data
	Datalist                 Hash = 0xad08  // datalist
	Datatype                 Hash = 0x11908 // datatype
	Datetime                 Hash = 0x28508 // datetime
	Dd                       Hash = 0x6e02  // dd
	Decoding                 Hash = 0x9508  // decoding
	Default                  Hash = 0x17807 // default
	Defer                    Hash = 0x4405  // defer
	Del                      Hash = 0x1f203 // del
	Details                  Hash = 0x20b07 // details
	Dfn                      Hash = 0x16a03 // dfn
	Dialog                   Hash = 0x28d06 // dialog
	Dir                      Hash = 0x8403  // dir
	Disabled                 Hash = 0x19208 // disabled
	Div                      Hash = 0x19903 // div
	Dl                       Hash = 0x1c302 // dl
	Draggable                Hash = 0x1da09 // draggable
	Dt                       Hash = 0x40902 // dt
	Em                       Hash = 0xdc02  // em
	Embed                    Hash = 0x16605 // embed
	Enctype                  Hash = 0x26a07 // enctype
	Enterkeyhint             Hash = 0x2500c // enterkeyhint
	Fetchpriority            Hash = 0x1220d // fetchpriority
	Fieldset                 Hash = 0x22c08 // fieldset
	Figcaption               Hash = 0x2340a // figcaption
	Figure                   Hash = 0x24506 // figure
	Font                     Hash = 0x2404  // font
	Footer                   Hash = 0x1a06  // footer
	For                      Hash = 0x25c03 // for
	Form                     Hash = 0x25c04 // form
	Formaction               Hash = 0x25c0a // formaction
	Formenctype              Hash = 0x2660b // formenctype
	Formmethod               Hash = 0x2710a // formmethod
	Formnovalidate           Hash = 0x27b0e // formnovalidate
	Formtarget               Hash = 0x2930a // formtarget
	Frame                    Hash = 0x16e05 // frame
	Frameset                 Hash = 0x16e08 // frameset
	H1                       Hash = 0x2d502 // h1
	H2                       Hash = 0x38602 // h2
	H3                       Hash = 0x39502 // h3
	H4                       Hash = 0x40b02 // h4
	H5                       Hash = 0x29d02 // h5
	H6                       Hash = 0x29f02 // h6
	Head                     Hash = 0x36c04 // head
	Header                   Hash = 0x36c06 // header
	Headers                  Hash = 0x36c07 // headers
	Height                   Hash = 0x2a106 // height
	Hgroup                   Hash = 0x2b506 // hgroup
	Hidden                   Hash = 0x2cc06 // hidden
	High                     Hash = 0x2d204 // high
	Hr                       Hash = 0x2d702 // hr
	Href                     Hash = 0x2d704 // href
	Hreflang                 Hash = 0x2d708 // hreflang
	Html                     Hash = 0x2a504 // html
	Http_Equiv               Hash = 0x2df0a // http-equiv
	I                        Hash = 0x2801  // i
	Id                       Hash = 0x9402  // id
	Iframe                   Hash = 0x2f206 // iframe
	Image                    Hash = 0x30005 // image
	Imagesizes               Hash = 0x3000a // imagesizes
	Imagesrcset              Hash = 0x30d0b // imagesrcset
	Img                      Hash = 0x31803 // img
	Inert                    Hash = 0x10805 // inert
	Inlist                   Hash = 0x21f06 // inlist
	Input                    Hash = 0x3d05  // input
	Inputmode                Hash = 0x3d09  // inputmode
	Ins                      Hash = 0x31b03 // ins
	Is                       Hash = 0xb202  // is
	Ismap                    Hash = 0x32e05 // ismap
	Itemid                   Hash = 0x2fa06 // itemid
	Itemprop                 Hash = 0x14e08 // itemprop
	Itemref                  Hash = 0x34507 // itemref
	Itemscope                Hash = 0x35709 // itemscope
	Itemtype                 Hash = 0x36108 // itemtype
	Kbd                      Hash = 0x8203  // kbd
	Kind                     Hash = 0xaa04  // kind
	Label                    Hash = 0x1c405 // label
	Lang                     Hash = 0x2db04 // lang
	Legend                   Hash = 0x1be06 // legend
	Li                       Hash = 0xb102  // li
	Link                     Hash = 0x1c804 // link
	List                     Hash = 0xb104  // list
	Loading                  Hash = 0x3ad07 // loading
	Loop                     Hash = 0x2a804 // loop
	Low                      Hash = 0x32103 // low
	Main                     Hash = 0x3b04  // main
	Map                      Hash = 0xed03  // map
	Mark                     Hash = 0x7f04  // mark
	Marquee                  Hash = 0x3e407 // marquee
	Math                     Hash = 0x36904 // math
	Max                      Hash = 0x37e03 // max
	Maxlength                Hash = 0x37e09 // maxlength
	Media                    Hash = 0x28b05 // media
	Menu                     Hash = 0x2f604 // menu
	Menuitem                 Hash = 0x2f608 // menuitem
	Meta                     Hash = 0x5004  // meta
	Meter                    Hash = 0x38805 // meter
	Method                   Hash = 0x27506 // method
	Min                      Hash = 0x38d03 // min
	Minlength                Hash = 0x38d09 // minlength
	Multiple                 Hash = 0x39708 // multiple
	Muted                    Hash = 0x39f05 // muted
	Name                     Hash = 0x4e04  // name
	Nav                      Hash = 0xbc03  // nav
	Nobr                     Hash = 0x14904 // nobr
	Noembed                  Hash = 0x16407 // noembed
	Noframes                 Hash = 0x16c08 // noframes
	Nomodule                 Hash = 0x1a908 // nomodule
	Noscript                 Hash = 0x23d08 // noscript
	Novalidate               Hash = 0x27f0a // novalidate
	Object                   Hash = 0xa106  // object
	Ol                       Hash = 0x18002 // ol
	Open                     Hash = 0x35d04 // open
	Optgroup                 Hash = 0x2aa08 // optgroup
	Optimum                  Hash = 0x3de07 // optimum
	Option                   Hash = 0x2ec06 // option
	Output                   Hash = 0x206   // output
	P                        Hash = 0x501   // p
	Param                    Hash = 0x7b05  // param
	Pattern                  Hash = 0xb607  // pattern
	Picture                  Hash = 0x18607 // picture
	Ping                     Hash = 0x2b104 // ping
	Plaintext                Hash = 0x2ba09 // plaintext
	Playsinline              Hash = 0x1000b // playsinline
	Popover                  Hash = 0x33207 // popover
	Popovertarget            Hash = 0x3320d // popovertarget
	Popovertargetaction      Hash = 0x33213 // popovertargetaction
	Portal                   Hash = 0x3f406 // portal
	Poster                   Hash = 0x41006 // poster
	Pre                      Hash = 0x3a403 // pre
	Prefix                   Hash = 0x3a406 // prefix
	Preload                  Hash = 0x3aa07 // preload
	Profile                  Hash = 0x3b407 // profile
	Progress                 Hash = 0x3bb08 // progress
	Property                 Hash = 0x15208 // property
	Q                        Hash = 0x11401 // q
	Rb                       Hash = 0x1f02  // rb
	Readonly                 Hash = 0x2c408 // readonly
	Referrerpolicy           Hash = 0x3490e // referrerpolicy
	Rel                      Hash = 0x3ab03 // rel
	Required                 Hash = 0x11208 // required
	Resource                 Hash = 0x24908 // resource
	Rev                      Hash = 0x18b03 // rev
	Reversed                 Hash = 0x18b08 // reversed
	Rows                     Hash = 0x4804  // rows
	Rowspan                  Hash = 0x4807  // rowspan
	Rp                       Hash = 0x6702  // rp
	Rt                       Hash = 0x10b02 // rt
	Rtc                      Hash = 0x10b03 // rtc
	Ruby                     Hash = 0x8604  // ruby
	S                        Hash = 0x1701  // s
	Samp                     Hash = 0x5d04  // samp
	Sandbox                  Hash = 0x7307  // sandbox
	Scope                    Hash = 0x35b05 // scope
	Script                   Hash = 0x23f06 // script
	Section                  Hash = 0x15e07 // section
	Select                   Hash = 0x1d306 // select
	Selected                 Hash = 0x1d308 // selected
	Shadowrootdelegatesfocus Hash = 0x1e818 // shadowrootdelegatesfocus
	Shadowrootmode           Hash = 0x1ff0e // shadowrootmode
	Shape                    Hash = 0x21105 // shape
	Size                     Hash = 0x30504 // size
	Sizes                    Hash = 0x30505 // sizes
	Slot                     Hash = 0x30904 // slot
	Small                    Hash = 0x31d05 // small
	Source                   Hash = 0x24b06 // source
	Span                     Hash = 0x4b04  // span
	Spellcheck               Hash = 0x3720a // spellcheck
	Src                      Hash = 0x31203 // src
	Srclang                  Hash = 0x3c207 // srclang
	Srcset                   Hash = 0x31206 // srcset
	Start                    Hash = 0x22305 // start
	Step                     Hash = 0xb304  // step
	Strike                   Hash = 0x3c906 // strike
	Strong                   Hash = 0x3cf06 // strong
	Style                    Hash = 0x3d505 // style
	Sub                      Hash = 0x3da03 // sub
	Summary                  Hash = 0x3eb07 // summary
	Sup                      Hash = 0x3f203 // sup
	Svg                      Hash = 0x3fa03 // svg
	Tabindex                 Hash = 0x5208  // tabindex
	Table                    Hash = 0x1bb05 // table
	Target                   Hash = 0x29706 // target
	Tbody                    Hash = 0x705   // tbody
	Td                       Hash = 0x1f102 // td
	Template                 Hash = 0xdb08  // template
	Text                     Hash = 0x2bf04 // text
	Textarea                 Hash = 0x2bf08 // textarea
	Tfoot                    Hash = 0x1905  // tfoot
	Th                       Hash = 0x27702 // th
	Thead                    Hash = 0x36b05 // thead
	Time                     Hash = 0x28904 // time
	Title                    Hash = 0x2705  // title
	Tr                       Hash = 0xa602  // tr
	Track                    Hash = 0xa605  // track
	Translate                Hash = 0xf309  // translate
	Tt                       Hash = 0xb802  // tt
	Type                     Hash = 0x11d04 // type
	Typeof                   Hash = 0x11d06 // typeof
	U                        Hash = 0x301   // u
	Ul                       Hash = 0x17c02 // ul
	Usemap                   Hash = 0xea06  // usemap
	Value                    Hash = 0xbe05  // value
	Var                      Hash = 0x19b03 // var
	Video                    Hash = 0x2e805 // video
	Vocab                    Hash = 0x3fd05 // vocab
	Wbr                      Hash = 0x40403 // wbr
	Width                    Hash = 0x40705 // width
	Wrap                     Hash = 0x40d04 // wrap
	Xmlns                    Hash = 0x5905  // xmlns
	Xmp                      Hash = 0x7903  // xmp
)

Identifiers for the hashes associated with the text in the comments.

func ToHash

func ToHash(s []byte) Hash

ToHash returns a hash Hash for a given []byte. Hash is a uint32 that is associated with the text in []byte. It returns zero if no match found.

func (Hash) Bytes

func (i Hash) Bytes() []byte

Bytes returns the text associated with the hash.

func (Hash) String

func (i Hash) String() string

String returns the text associated with the hash.

type Minifier

type Minifier struct {
	KeepComments            bool
	KeepConditionalComments bool
	KeepSpecialComments     bool
	KeepDefaultAttrVals     bool
	KeepDocumentTags        bool
	KeepEndTags             bool
	KeepQuotes              bool
	KeepWhitespace          bool
	TemplateDelims          [2]string
}

Minifier is an HTML minifier.

func (*Minifier) Minify

func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error

Minify minifies HTML data, it reads from r and writes to w.

type Token

type Token struct {
	html.TokenType
	Hash        Hash
	Data        []byte
	Text        []byte
	AttrVal     []byte
	Traits      traits
	Offset      int
	HasTemplate bool
}

Token is a single token unit with an attribute value (if given) and hash of the data.

type TokenBuffer

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

TokenBuffer is a buffer that allows for token look-ahead.

func NewTokenBuffer

func NewTokenBuffer(r *parse.Input, l *html.Lexer) *TokenBuffer

NewTokenBuffer returns a new TokenBuffer.

func (*TokenBuffer) Attributes

func (z *TokenBuffer) Attributes(hashes ...Hash) []*Token

Attributes extracts the gives attribute hashes from a tag. It returns in the same order pointers to the requested token data or nil.

func (*TokenBuffer) Peek

func (z *TokenBuffer) Peek(pos int) *Token

Peek returns the ith element and possibly does an allocation. Peeking past an error will panic.

func (*TokenBuffer) Shift

func (z *TokenBuffer) Shift() *Token

Shift returns the first element and advances position.

Source Files

buffer.go hash.go html.go table.go

Version
v2.23.11 (latest)
Published
Aug 6, 2025
Platform
linux/amd64
Imports
7 packages
Last checked
8 months ago

Tools for package owners.