]> Cypherpunks repositories - gostls13.git/commitdiff
html, exp/html: escape ' and " as ' and ", since IE8 and
authorNigel Tao <nigeltao@golang.org>
Wed, 11 Apr 2012 23:35:43 +0000 (09:35 +1000)
committerNigel Tao <nigeltao@golang.org>
Wed, 11 Apr 2012 23:35:43 +0000 (09:35 +1000)
below do not support &apos;.

This makes package html consistent with package text/template's
HTMLEscape function.

Fixes #3489.

R=rsc, mikesamuel, dsymonds
CC=golang-dev
https://golang.org/cl/5992071

src/pkg/exp/html/escape.go
src/pkg/exp/html/render_test.go
src/pkg/exp/html/token_test.go
src/pkg/html/escape.go
src/pkg/net/http/server.go
src/pkg/text/template/funcs.go

index 8f62a8c288079fb8110b73fe1cf9afa815e2d359..c177a66068e38050141d4744c1acb74b27eb10dd 100644 (file)
@@ -205,13 +205,15 @@ func escape(w writer, s string) error {
                case '&':
                        esc = "&amp;"
                case '\'':
-                       esc = "&apos;"
+                       // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
+                       esc = "&#39;"
                case '<':
                        esc = "&lt;"
                case '>':
                        esc = "&gt;"
                case '"':
-                       esc = "&quot;"
+                       // "&#34;" is shorter than "&quot;".
+                       esc = "&#34;"
                default:
                        panic("unrecognized escape character")
                }
@@ -226,7 +228,7 @@ func escape(w writer, s string) error {
 }
 
 // EscapeString escapes special characters like "<" to become "&lt;". It
-// escapes only five such characters: amp, apos, lt, gt and quot.
+// escapes only five such characters: <, >, &, ' and ".
 // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
 // always true.
 func EscapeString(s string) string {
index 0584f35abdb264de4d0c18a5bef7b9cd2572292d..a2e205275d8d83865c0d9da3d298c145e6e8efef 100644 (file)
@@ -98,7 +98,7 @@ func TestRenderer(t *testing.T) {
                        },
                },
        }
-       want := `<html><head></head><body>0&lt;1<p id="A" foo="abc&quot;def">` +
+       want := `<html><head></head><body>0&lt;1<p id="A" foo="abc&#34;def">` +
                `2<b empty="">3</b><i backslash="\">&amp;4</i></p>` +
                `5<blockquote></blockquote><br/>6</body></html>`
        b := new(bytes.Buffer)
index 61d74006eab6154664dee216cf3759a07d5e4b71..4e6eca93c53207d036a0c6c4fbaa12636891261d 100644 (file)
@@ -359,7 +359,7 @@ var tokenTests = []tokenTest{
        {
                "tricky",
                "<p \t\n iD=\"a&quot;B\"  foo=\"bar\"><EM>te&lt;&amp;;xt</em></p>",
-               `<p id="a&quot;B" foo="bar">$<em>$te&lt;&amp;;xt$</em>$</p>`,
+               `<p id="a&#34;B" foo="bar">$<em>$te&lt;&amp;;xt$</em>$</p>`,
        },
        // A nonexistent entity. Tokenizing and converting back to a string should
        // escape the "&" to become "&amp;".
@@ -421,7 +421,7 @@ var tokenTests = []tokenTest{
        {
                "Double-quoted attribute value",
                `<input value="I'm an attribute" FOO="BAR">`,
-               `<input value="I&apos;m an attribute" foo="BAR">`,
+               `<input value="I&#39;m an attribute" foo="BAR">`,
        },
        {
                "Attribute name characters",
@@ -436,7 +436,7 @@ var tokenTests = []tokenTest{
        {
                "Attributes with a solitary single quote",
                `<p id=can't><p id=won't>`,
-               `<p id="can&apos;t">$<p id="won&apos;t">`,
+               `<p id="can&#39;t">$<p id="won&#39;t">`,
        },
 }
 
@@ -545,10 +545,11 @@ func TestUnescapeEscape(t *testing.T) {
                `"<&>"`,
                `&quot;&lt;&amp;&gt;&quot;`,
                `3&5==1 && 0<1, "0&lt;1", a+acute=&aacute;`,
+               `The special characters are: <, >, &, ' and "`,
        }
        for _, s := range ss {
-               if s != UnescapeString(EscapeString(s)) {
-                       t.Errorf("s != UnescapeString(EscapeString(s)), s=%q", s)
+               if got := UnescapeString(EscapeString(s)); got != s {
+                       t.Errorf("got %q want %q", got, s)
                }
        }
 }
index fee771a5784e200eb47d42072ebb463e78aaaef9..24cb7af85240ff6cdec990aaa15891c05547019f 100644 (file)
@@ -210,13 +210,15 @@ func escape(w writer, s string) error {
                case '&':
                        esc = "&amp;"
                case '\'':
-                       esc = "&apos;"
+                       // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
+                       esc = "&#39;"
                case '<':
                        esc = "&lt;"
                case '>':
                        esc = "&gt;"
                case '"':
-                       esc = "&quot;"
+                       // "&#34;" is shorter than "&quot;".
+                       esc = "&#34;"
                default:
                        panic("unrecognized escape character")
                }
@@ -231,7 +233,7 @@ func escape(w writer, s string) error {
 }
 
 // EscapeString escapes special characters like "<" to become "&lt;". It
-// escapes only five such characters: amp, apos, lt, gt and quot.
+// escapes only five such characters: <, >, &, ' and ".
 // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't
 // always true.
 func EscapeString(s string) string {
index 228ac401968182fe6b4c2a42588892b322f0fec7..924ffd348156e030a98e227ec4bb5691761af46e 100644 (file)
@@ -785,8 +785,10 @@ var htmlReplacer = strings.NewReplacer(
        "&", "&amp;",
        "<", "&lt;",
        ">", "&gt;",
-       `"`, "&quot;",
-       "'", "&apos;",
+       // "&#34;" is shorter than "&quot;".
+       `"`, "&#34;",
+       // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
+       "'", "&#39;",
 )
 
 func htmlEscape(s string) string {
index 525179cb499839a8a0e853d94e3dfe9b5bf3871f..8fbf0ef50a765eac21e96f1524c0f0936368c24e 100644 (file)
@@ -246,7 +246,7 @@ func not(arg interface{}) (truth bool) {
 
 var (
        htmlQuot = []byte("&#34;") // shorter than "&quot;"
-       htmlApos = []byte("&#39;") // shorter than "&apos;"
+       htmlApos = []byte("&#39;") // shorter than "&apos;" and apos was not in HTML until HTML5
        htmlAmp  = []byte("&amp;")
        htmlLt   = []byte("&lt;")
        htmlGt   = []byte("&gt;")