|
| 1 | +/** |
| 2 | + * Applies fixes to the bundled Highcharts version, same as in @vaadin/charts. |
| 3 | + * TODO: Remove when upgrading Highcharts, see vaadin/web-components#12213 |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-env node, es6 */ |
| 7 | + |
| 8 | +const RESERVED_KEYS = ['__proto__', 'constructor']; |
| 9 | + |
| 10 | +const UNSUPPORTED_SCHEMES = ['javascript', 'vbscript', 'data']; |
| 11 | + |
| 12 | +// Relative links are resolved against a placeholder, as the exporter has no page URL |
| 13 | +const BASE_URL = 'https://localhost/'; |
| 14 | + |
| 15 | +const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key); |
| 16 | + |
| 17 | +const isReservedKey = (key) => RESERVED_KEYS.includes(key); |
| 18 | + |
| 19 | +const isUnsupportedLink = (value) => { |
| 20 | + if (typeof value !== 'string') { |
| 21 | + return true; |
| 22 | + } |
| 23 | + try { |
| 24 | + // The URL parser normalizes case and whitespace the same way as navigation does |
| 25 | + return UNSUPPORTED_SCHEMES.includes(new URL(value, BASE_URL).protocol.slice(0, -1)); |
| 26 | + } catch (_) { |
| 27 | + return true; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +function applyHighchartsPatches(Highcharts) { |
| 32 | + const { Chart, Point } = Highcharts; |
| 33 | + |
| 34 | + // Skip reserved option names in point objects |
| 35 | + Highcharts.wrap(Point.prototype, 'optionsToObject', function (proceed, options) { |
| 36 | + if ( |
| 37 | + options && |
| 38 | + typeof options === 'object' && |
| 39 | + !Array.isArray(options) && |
| 40 | + RESERVED_KEYS.some((key) => hasOwn(options, key)) |
| 41 | + ) { |
| 42 | + options = Object.fromEntries(Object.entries(options).filter(([key]) => !isReservedKey(key))); |
| 43 | + } |
| 44 | + return proceed.call(this, options); |
| 45 | + }); |
| 46 | + |
| 47 | + // Skip reserved path segments in point keys |
| 48 | + Highcharts.wrap(Point.prototype, 'setNestedProperty', function (proceed, object, value, key) { |
| 49 | + if (String(key).split('.').some(isReservedKey)) { |
| 50 | + return object; |
| 51 | + } |
| 52 | + return proceed.call(this, object, value, key); |
| 53 | + }); |
| 54 | + |
| 55 | + // Ignore credits links that use an unsupported URL scheme |
| 56 | + Highcharts.wrap(Chart.prototype, 'addCredits', function (proceed, credits) { |
| 57 | + const options = Highcharts.merge(true, this.options.credits, credits); |
| 58 | + if (options && options.href && isUnsupportedLink(options.href)) { |
| 59 | + Highcharts.error(33, false, this, { 'Invalid attribute in config': 'credits.href' }); |
| 60 | + delete options.href; |
| 61 | + } |
| 62 | + return proceed.call(this, options); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +module.exports = applyHighchartsPatches; |
0 commit comments