mirror of
https://github.com/golang/go
synced 2024-11-20 11:34:48 -07:00
html: move the sanity checking of the entity map from runtime
(during init) to test-time (via gotest). R=gri CC=golang-dev https://golang.org/cl/3466044
This commit is contained in:
parent
ae2495c5b0
commit
688a83128d
@ -4,10 +4,6 @@
|
|||||||
|
|
||||||
package html
|
package html
|
||||||
|
|
||||||
import (
|
|
||||||
"utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
// entity is a map from HTML entity names to their values. The semicolon matters:
|
// entity is a map from HTML entity names to their values. The semicolon matters:
|
||||||
// http://www.whatwg.org/specs/web-apps/current-work/multipage/named-character-references.html
|
// http://www.whatwg.org/specs/web-apps/current-work/multipage/named-character-references.html
|
||||||
// lists both "amp" and "amp;" as two separate entries.
|
// lists both "amp" and "amp;" as two separate entries.
|
||||||
@ -2252,19 +2248,3 @@ var entity2 = map[string][2]int{
|
|||||||
"vsupnE;": {'\u2ACC', '\uFE00'},
|
"vsupnE;": {'\u2ACC', '\uFE00'},
|
||||||
"vsupne;": {'\u228B', '\uFE00'},
|
"vsupne;": {'\u228B', '\uFE00'},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
// We verify that the length of UTF-8 encoding of each value is <= 1 + len(key).
|
|
||||||
// The +1 comes from the leading "&". This property implies that the length of
|
|
||||||
// unescaped text is <= the length of escaped text.
|
|
||||||
for k, v := range entity {
|
|
||||||
if 1+len(k) < utf8.RuneLen(v) {
|
|
||||||
panic("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for k, v := range entity2 {
|
|
||||||
if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) {
|
|
||||||
panic("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
26
src/pkg/html/entity_test.go
Normal file
26
src/pkg/html/entity_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package html
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEntityLength(t *testing.T) {
|
||||||
|
// We verify that the length of UTF-8 encoding of each value is <= 1 + len(key).
|
||||||
|
// The +1 comes from the leading "&". This property implies that the length of
|
||||||
|
// unescaped text is <= the length of escaped text.
|
||||||
|
for k, v := range entity {
|
||||||
|
if 1+len(k) < utf8.RuneLen(v) {
|
||||||
|
t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for k, v := range entity2 {
|
||||||
|
if 1+len(k) < utf8.RuneLen(v[0])+utf8.RuneLen(v[1]) {
|
||||||
|
t.Error("escaped entity &" + k + " is shorter than its UTF-8 encoding " + string(v[0]) + string(v[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user