Skip to content

Commit 4e1f66f

Browse files
committed
#15 引入 html 包
html 包的代码来自 golang.org/x/net/html ,去掉了对 fmt 的依赖以减小 JS 端包体积
1 parent 34aebc8 commit 4e1f66f

23 files changed

+8044
-2157
lines changed

.header.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Includes": [
55
"*.go"
66
],
7-
"Excludes": [],
7+
"Excludes": ["html/**"],
88
"UseDefaultExcludes": true,
99
"Properties": {
1010
"Year": "2019-present",

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ require (
2424
gitlab.com/golang-commonmark/mdurl v0.0.0-20180912090424-e5bce34c34f2 // indirect
2525
gitlab.com/golang-commonmark/puny v0.0.0-20180912090636-2cd490539afe // indirect
2626
gitlab.com/opennota/wd v0.0.0-20180912061657-c5d65f63c638 // indirect
27-
golang.org/x/sys v0.0.0-20190904005037-43c01164e931 // indirect
27+
golang.org/x/sys v0.0.0-20190909082730-f460065e899a // indirect
2828
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2929
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73r
9393
golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9494
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
9595
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
96-
golang.org/x/sys v0.0.0-20190904005037-43c01164e931 h1:+WYfosiOJzB4BjsISl1Rv4ZLUy+VYcF+u+0Y9jcerv8=
97-
golang.org/x/sys v0.0.0-20190904005037-43c01164e931/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
96+
golang.org/x/sys v0.0.0-20190909082730-f460065e899a h1:mIzbOulag9/gXacgxKlFVwpCOWSfBT3/pDyyCwGA9as=
97+
golang.org/x/sys v0.0.0-20190909082730-f460065e899a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9898
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
9999
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
100100
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e h1:FDhOuMEY4JVRztM/gsbk+IKUQ8kj74bxZrgw87eMMVc=

html/atom/atom.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2012 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package atom provides integer codes (also known as atoms) for a fixed set of
6+
// frequently occurring HTML strings: tag names and attribute keys such as "p"
7+
// and "id".
8+
//
9+
// Sharing an atom's name between all elements with the same tag can result in
10+
// fewer string allocations when tokenizing and parsing HTML. Integer
11+
// comparisons are also generally faster than string comparisons.
12+
//
13+
// The value of an atom's particular code is not guaranteed to stay the same
14+
// between versions of this package. Neither is any ordering guaranteed:
15+
// whether atom.H1 < atom.H2 may also change. The codes are not guaranteed to
16+
// be dense. The only guarantees are that e.g. looking up "div" will yield
17+
// atom.Div, calling atom.Div.String will return "div", and atom.Div != 0.
18+
package atom
19+
20+
// Atom is an integer code for a string. The zero value maps to "".
21+
type Atom uint32
22+
23+
// String returns the atom's name.
24+
func (a Atom) String() string {
25+
start := uint32(a >> 8)
26+
n := uint32(a & 0xff)
27+
if start+n > uint32(len(atomText)) {
28+
return ""
29+
}
30+
return atomText[start : start+n]
31+
}
32+
33+
func (a Atom) string() string {
34+
return atomText[a>>8 : a>>8+a&0xff]
35+
}
36+
37+
// fnv computes the FNV hash with an arbitrary starting value h.
38+
func fnv(h uint32, s []byte) uint32 {
39+
for i := range s {
40+
h ^= uint32(s[i])
41+
h *= 16777619
42+
}
43+
return h
44+
}
45+
46+
func match(s string, t []byte) bool {
47+
for i, c := range t {
48+
if s[i] != c {
49+
return false
50+
}
51+
}
52+
return true
53+
}
54+
55+
// Lookup returns the atom whose name is s. It returns zero if there is no
56+
// such atom. The lookup is case sensitive.
57+
func Lookup(s []byte) Atom {
58+
if len(s) == 0 || len(s) > maxAtomLen {
59+
return 0
60+
}
61+
h := fnv(hash0, s)
62+
if a := table[h&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
63+
return a
64+
}
65+
if a := table[(h>>16)&uint32(len(table)-1)]; int(a&0xff) == len(s) && match(a.string(), s) {
66+
return a
67+
}
68+
return 0
69+
}
70+
71+
// String returns a string whose contents are equal to s. In that sense, it is
72+
// equivalent to string(s) but may be more efficient.
73+
func String(s []byte) string {
74+
if a := Lookup(s); a != 0 {
75+
return a.String()
76+
}
77+
return string(s)
78+
}

0 commit comments

Comments
 (0)