Skip to content

SVG export: Incorrect escaping for HTML within legend label #17753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1tgr opened this issue Sep 20, 2022 · 4 comments · Fixed by #18015
Closed

SVG export: Incorrect escaping for HTML within legend label #17753

1tgr opened this issue Sep 20, 2022 · 4 comments · Fixed by #18015

Comments

@1tgr
Copy link

1tgr commented Sep 20, 2022

Expected behaviour

When useHTML: true, a legend label whose HTML has a < symbol inside an attribute value produces valid SVG when exported.

Actual behaviour

The < symbol is not escaped on the exported SVG. Instead, it appears as a literal character within an SVG attribute value.

Live demo with steps to reproduce

https://jsfiddle.net/y7vtpzb4/2

  1. Add the exporting module:
    <script src="https://code.highcharts.com/modules/exporting.js"></script>
  2. Write JS code:
    Highcharts.chart('container', {
        legend: {
            useHTML: true,
            labelFormatter: () => `<span title="&lt;">hover on me</span>`,
        },
    
        series: [{
            data: [[1, 1]]
        }]
    });
    
  3. Click the button at the corner of the chart and export to an SVG file
  4. Open the SVG file in an editor and observe invalid XML:

    error on line 1 at column 4222: Unescaped '<' not allowed in attributes values

Product version

Highcharts 10.1.0, 10.2.1

Affected browser(s)

Google Chrome on Windows version 105.0.5195.127
Safari on Mac OS version 15.6.1 (17613.3.9.1.16)

@1tgr
Copy link
Author

1tgr commented Sep 20, 2022

The legend is part of the HTML portion of the chart. When the user exports to SVG, the sanitizeSVG function places the legend HTML inside a <foreignObject> element at the end of the SVG.

The body of the <foreignObject> comes from the browser's innerHTML text, which obeys HTML escaping rules, where almost all characters are allowed unescaped within an attribute value. SVG (ie XML) requires that < symbols are always escaped within attribute values.

@karolkolodziej
Copy link
Contributor

Thank you @1tgr for reporting that issue.

From what I can see, the &lt; is the only character that is failing to export properly to SVG.

@1tgr
Copy link
Author

1tgr commented Sep 24, 2022

I agree, since &lt; is the only character that's allowed in an HTML attribute value but not allowed in an XML/SVG attribute value.

The relevant part of the XML spec: https://www.w3.org/TR/xml/#NT-AttValue

There they have a regex for valid double-quoted attributes: "[^<&"]*"

And single-quoted attributes: '[^<&']*'

But ", ' and & would already be escaped in innerHTML.

@rdp1414
Copy link

rdp1414 commented Nov 21, 2022

SVG exporting not working when title contains "<"

When title contains less-than sign "<", then SVG exporting does not work - See fiddle (click on hamburger-icon -> Download SVG vector image -> open it).

Our team extracts a chart's SVG and using that svg does multiple exporting operations like copy chart, download png, print chart, mail chart, put it in our reporting system, etc. None of these are working when chart's title contains "<".

Reported at Topic#49880.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants