Skip to content

Commit 278c6cf

Browse files
namusyakatombergan
authored andcommitted
html/atom: sync html table with the current whatwg spec
Some elements and attributes have been removed from the spec, but are kept here for backwards compatibility. Also, this makes gen.go support go:generate and write contents into target files directly. Finally, this makes `go generate` work on an entire directory. Change-Id: I8d41840eec69eec1ef08527d8d71786612a3121d Reviewed-on: https://go-review.googlesource.com/65152 Run-TryBot: Tom Bergan <tombergan@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Tom Bergan <tombergan@google.com>
1 parent 0744d00 commit 278c6cf

3 files changed

Lines changed: 896 additions & 749 deletions

File tree

html/atom/gen.go

Lines changed: 98 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
// +build ignore
66

7-
package main
7+
//go:generate go run gen.go
8+
//go:generate go run gen.go -test
89

9-
// This program generates table.go and table_test.go.
10-
// Invoke as
11-
//
12-
// go run gen.go |gofmt >table.go
13-
// go run gen.go -test |gofmt >table_test.go
10+
package main
1411

1512
import (
13+
"bytes"
1614
"flag"
1715
"fmt"
16+
"go/format"
17+
"io/ioutil"
1818
"math/rand"
1919
"os"
2020
"sort"
@@ -42,6 +42,18 @@ func identifier(s string) string {
4242

4343
var test = flag.Bool("test", false, "generate table_test.go")
4444

45+
func genFile(name string, buf *bytes.Buffer) {
46+
b, err := format.Source(buf.Bytes())
47+
if err != nil {
48+
fmt.Fprintln(os.Stderr, err)
49+
os.Exit(1)
50+
}
51+
if err := ioutil.WriteFile(name, b, 0644); err != nil {
52+
fmt.Fprintln(os.Stderr, err)
53+
os.Exit(1)
54+
}
55+
}
56+
4557
func main() {
4658
flag.Parse()
4759

@@ -52,32 +64,31 @@ func main() {
5264
all = append(all, extra...)
5365
sort.Strings(all)
5466

55-
if *test {
56-
fmt.Printf("// generated by go run gen.go -test; DO NOT EDIT\n\n")
57-
fmt.Printf("package atom\n\n")
58-
fmt.Printf("var testAtomList = []string{\n")
59-
for _, s := range all {
60-
fmt.Printf("\t%q,\n", s)
61-
}
62-
fmt.Printf("}\n")
63-
return
64-
}
65-
6667
// uniq - lists have dups
67-
// compute max len too
68-
maxLen := 0
6968
w := 0
7069
for _, s := range all {
7170
if w == 0 || all[w-1] != s {
72-
if maxLen < len(s) {
73-
maxLen = len(s)
74-
}
7571
all[w] = s
7672
w++
7773
}
7874
}
7975
all = all[:w]
8076

77+
if *test {
78+
var buf bytes.Buffer
79+
fmt.Fprintln(&buf, "// Code generated by go generate gen.go; DO NOT EDIT.\n")
80+
fmt.Fprintln(&buf, "//go:generate go run gen.go -test\n")
81+
fmt.Fprintln(&buf, "package atom\n")
82+
fmt.Fprintln(&buf, "var testAtomList = []string{")
83+
for _, s := range all {
84+
fmt.Fprintf(&buf, "\t%q,\n", s)
85+
}
86+
fmt.Fprintln(&buf, "}")
87+
88+
genFile("table_test.go", &buf)
89+
return
90+
}
91+
8192
// Find hash that minimizes table size.
8293
var best *table
8394
for i := 0; i < 1000000; i++ {
@@ -163,36 +174,46 @@ func main() {
163174
atom[s] = uint32(off<<8 | len(s))
164175
}
165176

177+
var buf bytes.Buffer
166178
// Generate the Go code.
167-
fmt.Printf("// generated by go run gen.go; DO NOT EDIT\n\n")
168-
fmt.Printf("package atom\n\nconst (\n")
179+
fmt.Fprintln(&buf, "// Code generated by go generate gen.go; DO NOT EDIT.\n")
180+
fmt.Fprintln(&buf, "//go:generate go run gen.go\n")
181+
fmt.Fprintln(&buf, "package atom\n\nconst (")
182+
183+
// compute max len
184+
maxLen := 0
169185
for _, s := range all {
170-
fmt.Printf("\t%s Atom = %#x\n", identifier(s), atom[s])
186+
if maxLen < len(s) {
187+
maxLen = len(s)
188+
}
189+
fmt.Fprintf(&buf, "\t%s Atom = %#x\n", identifier(s), atom[s])
171190
}
172-
fmt.Printf(")\n\n")
191+
fmt.Fprintln(&buf, ")\n")
173192

174-
fmt.Printf("const hash0 = %#x\n\n", best.h0)
175-
fmt.Printf("const maxAtomLen = %d\n\n", maxLen)
193+
fmt.Fprintf(&buf, "const hash0 = %#x\n\n", best.h0)
194+
fmt.Fprintf(&buf, "const maxAtomLen = %d\n\n", maxLen)
176195

177-
fmt.Printf("var table = [1<<%d]Atom{\n", best.k)
196+
fmt.Fprintf(&buf, "var table = [1<<%d]Atom{\n", best.k)
178197
for i, s := range best.tab {
179198
if s == "" {
180199
continue
181200
}
182-
fmt.Printf("\t%#x: %#x, // %s\n", i, atom[s], s)
201+
fmt.Fprintf(&buf, "\t%#x: %#x, // %s\n", i, atom[s], s)
183202
}
184-
fmt.Printf("}\n")
203+
fmt.Fprintf(&buf, "}\n")
185204
datasize := (1 << best.k) * 4
186205

187-
fmt.Printf("const atomText =\n")
206+
fmt.Fprintln(&buf, "const atomText =")
188207
textsize := len(text)
189208
for len(text) > 60 {
190-
fmt.Printf("\t%q +\n", text[:60])
209+
fmt.Fprintf(&buf, "\t%q +\n", text[:60])
191210
text = text[60:]
192211
}
193-
fmt.Printf("\t%q\n\n", text)
212+
fmt.Fprintf(&buf, "\t%q\n\n", text)
213+
214+
genFile("table.go", &buf)
194215

195-
fmt.Fprintf(os.Stderr, "%d atoms; %d string bytes + %d tables = %d total data\n", len(all), textsize, datasize, textsize+datasize)
216+
fmt.Fprintf(os.Stdout, "%d atoms; %d string bytes + %d tables = %d total data\n", len(all), textsize, datasize, textsize+datasize)
196217
}
197218

198219
type byLen []string
@@ -285,8 +306,10 @@ func (t *table) push(i uint32, depth int) bool {
285306

286307
// The lists of element names and attribute keys were taken from
287308
// https://html.spec.whatwg.org/multipage/indices.html#index
288-
// as of the "HTML Living Standard - Last Updated 21 February 2015" version.
309+
// as of the "HTML Living Standard - Last Updated 18 September 2017" version.
289310

311+
// "command", "keygen" and "menuitem" have been removed from the spec,
312+
// but are kept here for backwards compatibility.
290313
var elements = []string{
291314
"a",
292315
"abbr",
@@ -349,6 +372,7 @@ var elements = []string{
349372
"legend",
350373
"li",
351374
"link",
375+
"main",
352376
"map",
353377
"mark",
354378
"menu",
@@ -364,6 +388,7 @@ var elements = []string{
364388
"output",
365389
"p",
366390
"param",
391+
"picture",
367392
"pre",
368393
"progress",
369394
"q",
@@ -375,6 +400,7 @@ var elements = []string{
375400
"script",
376401
"section",
377402
"select",
403+
"slot",
378404
"small",
379405
"source",
380406
"span",
@@ -403,14 +429,21 @@ var elements = []string{
403429
}
404430

405431
// https://html.spec.whatwg.org/multipage/indices.html#attributes-3
406-
432+
//
433+
// "challenge", "command", "contextmenu", "dropzone", "icon", "keytype", "mediagroup",
434+
// "radiogroup", "spellcheck", "scoped", "seamless", "sortable" and "sorted" have been removed from the spec,
435+
// but are kept here for backwards compatibility.
407436
var attributes = []string{
408437
"abbr",
409438
"accept",
410439
"accept-charset",
411440
"accesskey",
412441
"action",
442+
"allowfullscreen",
443+
"allowpaymentrequest",
444+
"allowusermedia",
413445
"alt",
446+
"as",
414447
"async",
415448
"autocomplete",
416449
"autofocus",
@@ -420,6 +453,7 @@ var attributes = []string{
420453
"checked",
421454
"cite",
422455
"class",
456+
"color",
423457
"cols",
424458
"colspan",
425459
"command",
@@ -457,6 +491,8 @@ var attributes = []string{
457491
"icon",
458492
"id",
459493
"inputmode",
494+
"integrity",
495+
"is",
460496
"ismap",
461497
"itemid",
462498
"itemprop",
@@ -481,16 +517,20 @@ var attributes = []string{
481517
"multiple",
482518
"muted",
483519
"name",
520+
"nomodule",
521+
"nonce",
484522
"novalidate",
485523
"open",
486524
"optimum",
487525
"pattern",
488526
"ping",
489527
"placeholder",
528+
"playsinline",
490529
"poster",
491530
"preload",
492531
"radiogroup",
493532
"readonly",
533+
"referrerpolicy",
494534
"rel",
495535
"required",
496536
"reversed",
@@ -507,10 +547,13 @@ var attributes = []string{
507547
"sizes",
508548
"sortable",
509549
"sorted",
550+
"slot",
510551
"span",
552+
"spellcheck",
511553
"src",
512554
"srcdoc",
513555
"srclang",
556+
"srcset",
514557
"start",
515558
"step",
516559
"style",
@@ -520,16 +563,22 @@ var attributes = []string{
520563
"translate",
521564
"type",
522565
"typemustmatch",
566+
"updateviacache",
523567
"usemap",
524568
"value",
525569
"width",
570+
"workertype",
526571
"wrap",
527572
}
528573

574+
// "onautocomplete", "onautocompleteerror", "onmousewheel",
575+
// "onshow" and "onsort" have been removed from the spec,
576+
// but are kept here for backwards compatibility.
529577
var eventHandlers = []string{
530578
"onabort",
531579
"onautocomplete",
532580
"onautocompleteerror",
581+
"onauxclick",
533582
"onafterprint",
534583
"onbeforeprint",
535584
"onbeforeunload",
@@ -541,11 +590,14 @@ var eventHandlers = []string{
541590
"onclick",
542591
"onclose",
543592
"oncontextmenu",
593+
"oncopy",
544594
"oncuechange",
595+
"oncut",
545596
"ondblclick",
546597
"ondrag",
547598
"ondragend",
548599
"ondragenter",
600+
"ondragexit",
549601
"ondragleave",
550602
"ondragover",
551603
"ondragstart",
@@ -565,18 +617,24 @@ var eventHandlers = []string{
565617
"onload",
566618
"onloadeddata",
567619
"onloadedmetadata",
620+
"onloadend",
568621
"onloadstart",
569622
"onmessage",
623+
"onmessageerror",
570624
"onmousedown",
625+
"onmouseenter",
626+
"onmouseleave",
571627
"onmousemove",
572628
"onmouseout",
573629
"onmouseover",
574630
"onmouseup",
575631
"onmousewheel",
632+
"onwheel",
576633
"onoffline",
577634
"ononline",
578635
"onpagehide",
579636
"onpageshow",
637+
"onpaste",
580638
"onpause",
581639
"onplay",
582640
"onplaying",
@@ -585,7 +643,9 @@ var eventHandlers = []string{
585643
"onratechange",
586644
"onreset",
587645
"onresize",
646+
"onrejectionhandled",
588647
"onscroll",
648+
"onsecuritypolicyviolation",
589649
"onseeked",
590650
"onseeking",
591651
"onselect",
@@ -597,6 +657,7 @@ var eventHandlers = []string{
597657
"onsuspend",
598658
"ontimeupdate",
599659
"ontoggle",
660+
"onunhandledrejection",
600661
"onunload",
601662
"onvolumechange",
602663
"onwaiting",

0 commit comments

Comments
 (0)