Skip to content

Commit 683ef8c

Browse files
nussjustinagnivade
authored andcommitted
html/template: document handling of namespaced and data- attributes
Attributes with a namespace or a data- prefix are handled as if they had no namespace/data- prefix. There is also a special case, where attributes with a "xmlns" namespace are always treated as containing URLs. This could surprise users of the package, since this behaviour was not documented anywhere, so this change adds some documentation for all three cases. Fixes #12648 Change-Id: If57a2ec49fec91a330fc04795726e8cffa9b75c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/79895 Run-TryBot: Andrew Bonventre <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Andrew Bonventre <[email protected]>
1 parent db87c9f commit 683ef8c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/html/template/doc.go

+45
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,51 @@ functions.
7373
For these internal escaping functions, if an action pipeline evaluates to
7474
a nil interface value, it is treated as though it were an empty string.
7575
76+
Namespaced and data- attributes
77+
78+
Attributes with a namespace are treated as if they had no namespace.
79+
Given the excerpt
80+
81+
<a my:href="{{.}}"></a>
82+
83+
At parse time the attribute will be treated as if it were just "href".
84+
So at parse time the template becomes:
85+
86+
<a my:href="{{. | urlescaper | attrescaper}}"></a>
87+
88+
Similarly to attributes with namespaces, attributes with a "data-" prefix are
89+
treated as if they had no "data-" prefix. So given
90+
91+
<a data-href="{{.}}"></a>
92+
93+
At parse time this becomes
94+
95+
<a data-href="{{. | urlescaper | attrescaper}}"></a>
96+
97+
If an attribute has both a namespace and a "data-" prefix, only the namespace
98+
will be removed when determining the context. For example
99+
100+
<a my:data-href="{{.}}"></a>
101+
102+
This is handled as if "my:data-href" was just "data-href" and not "href" as
103+
it would be if the "data-" prefix were to be ignored too. Thus at parse
104+
time this becomes just
105+
106+
<a my:data-href="{{. | attrescaper}}"></a>
107+
108+
As a special case, attributes with the namespace "xmlns" are always treated
109+
as containing URLs. Given the excerpts
110+
111+
<a xmlns:title="{{.}}"></a>
112+
<a xmlns:href="{{.}}"></a>
113+
<a xmlns:onclick="{{.}}"></a>
114+
115+
At parse time they become:
116+
117+
<a xmlns:title="{{. | urlescaper | attrescaper}}"></a>
118+
<a xmlns:href="{{. | urlescaper | attrescaper}}"></a>
119+
<a xmlns:onclick="{{. | urlescaper | attrescaper}}"></a>
120+
76121
Errors
77122
78123
See the documentation of ErrorCode for details.

0 commit comments

Comments
 (0)