OSDN Git Service

Update Go library to last weekly.
[pf3gnuchains/gcc-fork.git] / libgo / go / exp / template / html / clone.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package html
6
7 import (
8         "template/parse"
9 )
10
11 // clone clones a template Node.
12 func clone(n parse.Node) parse.Node {
13         switch t := n.(type) {
14         case *parse.ActionNode:
15                 return cloneAction(t)
16         case *parse.IfNode:
17                 b := new(parse.IfNode)
18                 copyBranch(&b.BranchNode, &t.BranchNode)
19                 return b
20         case *parse.ListNode:
21                 return cloneList(t)
22         case *parse.RangeNode:
23                 b := new(parse.RangeNode)
24                 copyBranch(&b.BranchNode, &t.BranchNode)
25                 return b
26         case *parse.TemplateNode:
27                 return cloneTemplate(t)
28         case *parse.TextNode:
29                 return cloneText(t)
30         case *parse.WithNode:
31                 b := new(parse.WithNode)
32                 copyBranch(&b.BranchNode, &t.BranchNode)
33                 return b
34         }
35         panic("cloning " + n.String() + " is unimplemented")
36 }
37
38 // cloneAction returns a deep clone of n.
39 func cloneAction(n *parse.ActionNode) *parse.ActionNode {
40         // We use keyless fields because they won't compile if a field is added.
41         return &parse.ActionNode{n.NodeType, n.Line, clonePipe(n.Pipe)}
42 }
43
44 // cloneList returns a deep clone of n.
45 func cloneList(n *parse.ListNode) *parse.ListNode {
46         if n == nil {
47                 return nil
48         }
49         // We use keyless fields because they won't compile if a field is added.
50         c := parse.ListNode{n.NodeType, make([]parse.Node, len(n.Nodes))}
51         for i, child := range n.Nodes {
52                 c.Nodes[i] = clone(child)
53         }
54         return &c
55 }
56
57 // clonePipe returns a shallow clone of n.
58 // The escaper does not modify pipe descendants in place so there's no need to
59 // clone deeply.
60 func clonePipe(n *parse.PipeNode) *parse.PipeNode {
61         if n == nil {
62                 return nil
63         }
64         // We use keyless fields because they won't compile if a field is added.
65         return &parse.PipeNode{n.NodeType, n.Line, n.Decl, n.Cmds}
66 }
67
68 // cloneTemplate returns a deep clone of n.
69 func cloneTemplate(n *parse.TemplateNode) *parse.TemplateNode {
70         // We use keyless fields because they won't compile if a field is added.
71         return &parse.TemplateNode{n.NodeType, n.Line, n.Name, clonePipe(n.Pipe)}
72 }
73
74 // cloneText clones the given node sharing its []byte.
75 func cloneText(n *parse.TextNode) *parse.TextNode {
76         // We use keyless fields because they won't compile if a field is added.
77         return &parse.TextNode{n.NodeType, n.Text}
78 }
79
80 // copyBranch clones src into dst.
81 func copyBranch(dst, src *parse.BranchNode) {
82         // We use keyless fields because they won't compile if a field is added.
83         *dst = parse.BranchNode{
84                 src.NodeType,
85                 src.Line,
86                 clonePipe(src.Pipe),
87                 cloneList(src.List),
88                 cloneList(src.ElseList),
89         }
90 }