Menu

#2715 Embedded javascript in svg output causes XML Parsing Error

None
pending-accepted
nobody
None
2024-06-13
2024-06-11
No

Loading an svg file with embedded javascript, generated by gnuplot 6.0 in a browser (Firefox, Chromium) causes a fatal error:

XML Parsing Error: not well-formed

The culprit seems to be this line:

var scriptText = script[1].innerHTML.replaceAll("<![CDATA[", "").replaceAll("]]>", "")

Escaping the two closing brackets, seems to fix the problem.

Discussion

  • Ethan Merritt

    Ethan Merritt - 2024-06-11

    I can't reproduce this problem in either Firefox or Chrome (build 125.0.6422.60).
    Do you see this for all gnuplot svg files?
    For the scripts on the demo pages on the web site?
    https://gnuplot.sourceforge.net/demo_svg_6.0/

     
  • Christos Gourdoupis

    Ok I got it. The demos work fine.
    However I am using 'standalone' mode.
    Here is a sample file.
    By the way I am on Linux.
    You should see an error message like this:

    XML Parsing Error: not well-formed
    Location: file:///home/xrigou/port/energy.svg
    Line Number 128, Column 24:
            for (i = 0; i < toggleVisibility.length; i++) {
    -----------------------^
    
     

    Last edit: Christos Gourdoupis 2024-06-12
  • Ethan Merritt

    Ethan Merritt - 2024-06-12

    Thanks. So apparently the browsers treat the file differently when it's embedded locally as compared to retrieved via a URL. Different parser? Extra syntax sanitation step during download? I can't say I understand what the reason is, but yes I can now reproduce both the problem and your fix.

    OK. I'll add the escape characters to the replace command in the gnuplot source distribution. Normally I'd try to correct it on the web site also but in this case it seems that the problem doesn't trigger when the file is executed from the web.

     
  • Ethan Merritt

    Ethan Merritt - 2024-06-13
    • status: open --> pending-accepted
    • Group: -->
    • Priority: -->
     
  • Christos Gourdoupis

    I am not any javascript guru, but I noticed that in standalone mode there is a CDATA tag which does not exist in the external js file. Obviously the xml parser thinks that the closing brackets terminate the CDATA section, and so some error is triggered further below.

     
    • Ethan Merritt

      Ethan Merritt - 2024-06-13

      The thing that perplexes me is that the same file /usr/local/share/gnuplot/6.0/js/gnuplot_svg.js is used both for the standalone output and for the accesss-via-url output. The difference is that the former copies the file line by line into the output file, while the latter inserts a single line containing xlink:href=\"%sgnuplot_svg.js\" where %sis expanded either to a directory on the local machine or a URL for remote access based on the set term svg ... jsdir <URL> keyword. I don't understand why that line in the file triggers a syntax error in one case but not the other. The only thing I can think of is that the browser itself does the equivalent of adding escape characters when it retrieves the file via xlink, which actually makes some sense but I have no idea where one would find this documented.

      Anyhow, thanks much for the analysis and the fix. I don't think I would have figured it out based on that error message.

      Note: It may be obvious but I'll mention anyway that you don't have to wait for a fix to the distributed source. You can make that fix locally in the file gnuplot_svg.js and gnuplot will copy from the fixed version when you select standalone mode for the svg terminal.

       

Log in to post a comment.