diff --git a/plugin.js b/plugin.js index c8ef772..958c526 100644 --- a/plugin.js +++ b/plugin.js @@ -14,6 +14,17 @@ // TODO IE8 fallback to a table maybe? // TODO a11y http://www.w3.org/html/wg/wiki/Correct_Hidden_Attribute_Section_v4 ( function() { + + function encodeData(data) + { + return JSON.stringify(data).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, '''); + } + + function decodeData(str) + { + return JSON.parse(str.replace(/'/g, "'").replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&')); + } + CKEDITOR.plugins.add( 'chart', { // Required plugins requires: 'widget,dialog', @@ -324,7 +335,11 @@ init: function() { // When an empty widget is initialized after clicking a button in the toolbar, we do not have yet chart values. if ( this.element.data( 'chart-value' ) ) { - this.setData( 'values', JSON.parse( this.element.data( 'chart-value' ) ) ); + try { + this.setData('values', decodeData(this.element.data('chart-value'))); + } catch(e) { + alert('Loading the chart values failed'); + } } // Chart is specified in a template, so it is available even in an empty widget. this.setData( 'chart', this.element.data( 'chart' ) ); @@ -409,10 +424,9 @@ 'class': element.attributes['class'], 'data-chart': this.data.chart, 'data-chart-height': this.data.height, - // Feature detection (editor.getSelectedHtml) to check if CKEditor 4.5+ is used. - // CKEditor < 4.5 and CKEditor 4.5+ require different code due to https://dev.ckeditor.com/ticket/13105 - 'data-chart-value': editor.getSelectedHtml ? JSON.stringify( data ) : CKEDITOR.tools.htmlEncodeAttr( JSON.stringify( data ) ) + 'data-chart-value': encodeData(data) } ); + return el; } } ); diff --git a/widget2chart.js b/widget2chart.js index 7ccad5c..a086420 100644 --- a/widget2chart.js +++ b/widget2chart.js @@ -15,6 +15,12 @@ // For IE8 and below the code will not be executed. if ( typeof document.addEventListener !== 'undefined' ) document.addEventListener( 'DOMContentLoaded', function() { + + function decodeData(str) + { + return JSON.parse(str.replace(/'/g, "'").replace(/"/g, '"').replace(/>/g, '>').replace(/</g, '<').replace(/&/g, '&')); + } + // Make sure Chart.js is enabled on a page. if ( typeof Chart === 'undefined' ) { if ( typeof console !== 'undefined' ) { @@ -68,7 +74,7 @@ if ( typeof document.addEventListener !== 'undefined' ) // Get chart information from data attributes. var chartType = el.getAttribute( 'data-chart' ), - values = JSON.parse( el.getAttribute( 'data-chart-value' ) ); + values = decodeData(el.getAttribute('data-chart-value')); // Malformed element, exit. if ( !values || !values.length || !chartType )