52

I'm trying to learn SVG for the first time but the code seems to have an issue with my block comments. I'm using:

/* This is my
 * block comment
 */

And when I run my code, I get the following error:

'return' statement outside of function
line: 116, column: 4

That so happens to be immediately before my block comment.

ardavis
  • 9,842
  • 12
  • 58
  • 112
  • I just took out all of the block comments, but it's still having an issue with another line. You can find all of the code from the example here: http://pastie.org/private/hdaccssdbcxal8s0o81ciq It is complaining about line 47 Column 8, which doesn't exist... – ardavis Mar 15 '11 at 04:45
  • 4
    As with HTML files, it is impossible to comment out anything but one or more whole elements. You can't comment out part of an SVG path, or even a single SVG object, for example. This goes way back to the origins of HTML, which were in library science and possibly in general semantics, not computer programming. Note the contrast with JavaScript and Style Sheets, both of which can contain very flexible character-based comment-out brackets. – David Spector Feb 05 '19 at 14:25

2 Answers2

89

As SVG is XML, you can use XML style comments:

<!-- 
    comment 
-->

For example:

<g onclick = "setScale(1)">
    <rect id = "scale1" x = "120" y = "10" width = "30" height = "30"
        fill = "#ffc" stroke = "black"/>
    <!-- 
        this text describes middle rectangle
    -->
    <text x = "135" y = "30" text-anchor = "middle">M</text>
</g>

Or you can exclude some part of code:

<!--
     this group is disabled for testing    
<g onclick = "setScale(1)">
    <rect id = "scale1" x = "120" y = "10" width = "30" height = "30"
        fill = "#ffc" stroke = "black"/>
    <text x = "135" y = "30" text-anchor = "middle">M</text>
</g>
-->
Yuriy
  • 1,188
  • 1
  • 9
  • 7
  • 2
    Note in this example that **a complete** "g" element has been commented out. It is impossible, however, to comment out arbitrary text. See my comment above. – David Spector Feb 05 '19 at 14:28
  • 1
    @Yuriy , maybe it would be important to mention if you use inside (double-hyphen), for example like this: `` then there is a parsing error! – MaZzIMo24 Oct 28 '22 at 06:34
1

An svg document is much the same as an html document as far as the DOM is concerned.

This line will break in all browsers:

svgDocument = evt.getTarget().getOwnerDocument();

And could simply be replaced by:

svgDocument = document;

Actually there's no real need to create a variable svgDocument since document is always defined and referring to the current document (the svg).

Please read https://jwatt.org/svg/authoring/ and in particular https://jwatt.org/svg/authoring/#asv-getters-and-setters.

Erik Dahlström
  • 59,452
  • 12
  • 120
  • 139