diff options
Diffstat (limited to 'libgo/go/html/render.go')
-rw-r--r-- | libgo/go/html/render.go | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/libgo/go/html/render.go b/libgo/go/html/render.go index 92c349fb32c..7e1a4669657 100644 --- a/libgo/go/html/render.go +++ b/libgo/go/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") @@ -138,9 +173,19 @@ func render1(w writer, n *Node) error { return err } + // Add initial newline where there is danger of a newline beging ignored. + if len(n.Child) > 0 && n.Child[0].Type == TextNode && strings.HasPrefix(n.Child[0].Data, "\n") { + switch n.Data { + case "pre", "listing", "textarea": + if err := w.WriteByte('\n'); err != nil { + return err + } + } + } + // Render any child nodes. switch n.Data { - case "noembed", "noframes", "noscript", "plaintext", "script", "style": + case "iframe", "noembed", "noframes", "noscript", "plaintext", "script", "style", "xmp": for _, c := range n.Child { if c.Type != TextNode { return fmt.Errorf("html: raw text element <%s> has non-text child node", n.Data) @@ -181,6 +226,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{ |