aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/html/render.go
diff options
context:
space:
mode:
authorAndrew Balholm <andybalholm@gmail.com>2011-11-24 09:28:58 +1100
committerNigel Tao <nigeltao@golang.org>2011-11-24 09:28:58 +1100
commit77b0ad1e806580e47e4f682dfb912c55e1411b73 (patch)
treeee5f208102be1f8295b68215ffe2861ca619fa99 /src/pkg/html/render.go
parentb3923a27dd80592ec4cd21ca04ea2a736578c9ad (diff)
downloadgo-77b0ad1e806580e47e4f682dfb912c55e1411b73.tar.xz
html: parse DOCTYPE into name and public and system identifiers
Pass tests2.dat, test 59: <!DOCTYPE <!DOCTYPE HTML>><!--<!--x-->--> | <!DOCTYPE <!doctype> | <html> | <head> | <body> | ">" | <!-- <!--x --> | "-->" Pass all the tests in doctype01.dat. Also pass tests2.dat, test 60: <!doctype html><div><form></form><div></div></div> R=nigeltao CC=golang-dev https://golang.org/cl/5437045
Diffstat (limited to 'src/pkg/html/render.go')
-rw-r--r--src/pkg/html/render.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/pkg/html/render.go b/src/pkg/html/render.go
index 92c349fb32..57d78beef1 100644
--- a/src/pkg/html/render.go
+++ b/src/pkg/html/render.go
@@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
+ "strings"
)
type writer interface {
@@ -98,6 +99,40 @@ func render1(w writer, n *Node) error {
if _, err := w.WriteString(n.Data); err != nil {
return err
}
+ if n.Attr != nil {
+ var p, s string
+ for _, a := range n.Attr {
+ switch a.Key {
+ case "public":
+ p = a.Val
+ case "system":
+ s = a.Val
+ }
+ }
+ if p != "" {
+ if _, err := w.WriteString(" PUBLIC "); err != nil {
+ return err
+ }
+ if err := writeQuoted(w, p); err != nil {
+ return err
+ }
+ if s != "" {
+ if err := w.WriteByte(' '); err != nil {
+ return err
+ }
+ if err := writeQuoted(w, s); err != nil {
+ return err
+ }
+ }
+ } else if s != "" {
+ if _, err := w.WriteString(" SYSTEM "); err != nil {
+ return err
+ }
+ if err := writeQuoted(w, s); err != nil {
+ return err
+ }
+ }
+ }
return w.WriteByte('>')
default:
return errors.New("html: unknown node type")
@@ -181,6 +216,27 @@ func render1(w writer, n *Node) error {
return w.WriteByte('>')
}
+// writeQuoted writes s to w surrounded by quotes. Normally it will use double
+// quotes, but if s contains a double quote, it will use single quotes.
+// It is used for writing the identifiers in a doctype declaration.
+// In valid HTML, they can't contain both types of quotes.
+func writeQuoted(w writer, s string) error {
+ var q byte = '"'
+ if strings.Contains(s, `"`) {
+ q = '\''
+ }
+ if err := w.WriteByte(q); err != nil {
+ return err
+ }
+ if _, err := w.WriteString(s); err != nil {
+ return err
+ }
+ if err := w.WriteByte(q); err != nil {
+ return err
+ }
+ return nil
+}
+
// Section 13.1.2, "Elements", gives this list of void elements. Void elements
// are those that can't have any contents.
var voidElements = map[string]bool{