Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1029)

Issue 4999042: code review 4999042: exp/template/html: elide comments in template source. (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
12 years, 8 months ago by MikeSamuel
Modified:
12 years, 8 months ago
Reviewers:
CC:
nigeltao, golang-dev
Visibility:
Public.

Description

exp/template/html: elide comments in template source. When templates are stored in external files, developers often embed comments to explain&|disable code. <!-- Oblique reference to project code name here --> {{if .C}}...{{else}}<!-- commented out default -->{{end}} This unnecessarily increases the size of shipped HTML and can leak information. This change elides all comments of the following types: 1. <!-- ... --> comments found in source. 2. /*...*/ and // comments found in <script> elements. 3. /*...*/ and // comments found in <style> elements. It does not elide /*...*/ or // comments found in HTML attributes: 4. <button onclick="/*...*/"> 5. <div style="/*...*/"> I can find no examples of comments in attributes in Closure Templates code and doing so would require keeping track of character positions post decode in <button onclick="/&#42;...*/"> To prevent token joining, /*comments*/ are JS and CSS comments are replaced with a whitespace char. HTML comments are not, but to prevent token joining we could try to detect cases like <<!---->b> </<!---->b> which has a well defined meaning in HTML but will cause a validator to barf. This is difficult, and this is a very minor case. I have punted for now, but if we need to address this case, the best way would be to normalize '<' in stateText to '&lt;' consistently. The whitespace to replace a JS /*comment*/ with depends on whether there is an embedded line terminator since break/* */foo ... is equivalent to break; foo ... while break/**/foo ... is equivalent to break foo; ... Comment eliding can interfere with IE conditional comments. http://en.wikipedia.org/wiki/Conditional_comment <!--[if IE 6]> <p>You are using Internet Explorer 6.</p> <![endif]--> /*@cc_on document.write("You are using IE4 or higher"); @*/ I have not encountered these in production template code, and the typed content change in CL 4962067 provides an escape-hatch if conditional comments are needed.

Patch Set 1 #

Patch Set 2 : diff -r 546f21eebee8 https://go.googlecode.com/hg/ #

Patch Set 3 : diff -r 546f21eebee8 https://go.googlecode.com/hg/ #

Patch Set 4 : diff -r 546f21eebee8 https://go.googlecode.com/hg/ #

Patch Set 5 : diff -r 11e69ba5ea31 https://go.googlecode.com/hg/ #

Patch Set 6 : diff -r 11e69ba5ea31 https://go.googlecode.com/hg/ #

Patch Set 7 : diff -r 91a983d0f4dd https://go.googlecode.com/hg/ #

Patch Set 8 : diff -r fe9f0d3b9825 https://go.googlecode.com/hg/ #

Patch Set 9 : diff -r 50977e660c80 https://go.googlecode.com/hg/ #

Patch Set 10 : diff -r 50977e660c80 https://go.googlecode.com/hg/ #

Patch Set 11 : diff -r 683a34d57871 https://go.googlecode.com/hg/ #

Patch Set 12 : diff -r f6df6a0d58e5 https://go.googlecode.com/hg/ #

Patch Set 13 : diff -r 2a7e3e5d18bd https://go.googlecode.com/hg/ #

Patch Set 14 : diff -r 5381c9668045 https://go.googlecode.com/hg/ #

Patch Set 15 : diff -r a33d941e6c51 https://go.googlecode.com/hg/ #

Patch Set 16 : diff -r 2e00b068ff36 https://go.googlecode.com/hg/ #

Patch Set 17 : diff -r e357d4d799ec https://go.googlecode.com/hg/ #

Patch Set 18 : diff -r e357d4d799ec https://go.googlecode.com/hg/ #

Patch Set 19 : diff -r e357d4d799ec https://go.googlecode.com/hg/ #

Total comments: 5

Patch Set 20 : diff -r e357d4d799ec https://go.googlecode.com/hg/ #

Patch Set 21 : diff -r e357d4d799ec https://go.googlecode.com/hg/ #

Patch Set 22 : diff -r b1457b9442a7 https://go.googlecode.com/hg/ #

Patch Set 23 : diff -r 1b91cb2a8f58 https://go.googlecode.com/hg/ #

Patch Set 24 : diff -r 685b10f88700 https://go.googlecode.com/hg/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+46 lines, -16 lines) Patch
M src/pkg/exp/template/html/escape.go View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 chunk +32 lines, -1 line 0 comments Download
M src/pkg/exp/template/html/escape_test.go View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 5 chunks +14 lines, -15 lines 0 comments Download

Messages

Total messages: 6
MikeSamuel
Hello nigeltao@golang.org (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://go.googlecode.com/hg/
12 years, 8 months ago (2011-09-19 02:19:04 UTC) #1
nigeltao
LGTM. Can we also get some tests for: `<button onclick="/*...*/">` "<script>var a/*b*//c\nd</script>" "<script>var a/*b*///c\nd</script>" http://codereview.appspot.com/4999042/diff/30004/src/pkg/exp/template/html/context.go ...
12 years, 8 months ago (2011-09-19 05:49:07 UTC) #2
MikeSamuel
I addressed most of these issues, but I'm going to put this CL on hold. ...
12 years, 8 months ago (2011-09-19 20:56:37 UTC) #3
MikeSamuel
Most of this CL is now gone. Only the changes to escapeText and tests remain.
12 years, 8 months ago (2011-09-22 02:23:23 UTC) #4
nigeltao
LGTM.
12 years, 8 months ago (2011-09-22 03:39:17 UTC) #5
MikeSamuel
12 years, 8 months ago (2011-09-22 04:38:46 UTC) #6
*** Submitted as http://code.google.com/p/go/source/detail?r=b2f72c9b13e9 ***

exp/template/html: elide comments in template source.

When templates are stored in external files, developers often embed
comments to explain&|disable code.

  <!-- Oblique reference to project code name here -->
  {{if .C}}...{{else}}<!-- commented out default -->{{end}}

This unnecessarily increases the size of shipped HTML and can leak
information.

This change elides all comments of the following types:
1. <!-- ... --> comments found in source.
2. /*...*/ and // comments found in <script> elements.
3. /*...*/ and // comments found in <style> elements.

It does not elide /*...*/ or // comments found in HTML attributes:
4. <button onclick="/*...*/">
5. <div style="/*...*/">

I can find no examples of comments in attributes in Closure Templates
code and doing so would require keeping track of character positions
post decode in

  <button onclick="/&#42;...*/">

To prevent token joining, /*comments*/ are JS and CSS comments are
replaced with a whitespace char.
HTML comments are not, but to prevent token joining we could try to
detect cases like
   <<!---->b>
   </<!---->b>
which has a well defined meaning in HTML but will cause a validator
to barf.  This is difficult, and this is a very minor case.
I have punted for now, but if we need to address this case, the best
way would be to normalize '<' in stateText to '&lt;' consistently.

The whitespace to replace a JS /*comment*/ with depends on whether
there is an embedded line terminator since
    break/*
    */foo
    ...
is equivalent to
    break;
    foo
    ...
while
    break/**/foo
    ...
is equivalent to
    break foo;
    ...

Comment eliding can interfere with IE conditional comments.
http://en.wikipedia.org/wiki/Conditional_comment

<!--[if IE 6]>
<p>You are using Internet Explorer 6.</p>
<![endif]-->

/*@cc_on
  document.write("You are using IE4 or higher");
@*/

I have not encountered these in production template code, and
the typed content change in CL 4962067 provides an escape-hatch
if conditional comments are needed.

R=nigeltao
CC=golang-dev
http://codereview.appspot.com/4999042
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b