Skip to content
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

encode and decode chart value to prevent problems during loading #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&apos;');
}

function decodeData(str)
{
return JSON.parse(str.replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&'));
}

CKEDITOR.plugins.add( 'chart', {
// Required plugins
requires: 'widget,dialog',
Expand Down Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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;
}
} );
Expand Down
8 changes: 7 additions & 1 deletion widget2chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&'));
}

// Make sure Chart.js is enabled on a page.
if ( typeof Chart === 'undefined' ) {
if ( typeof console !== 'undefined' ) {
Expand Down Expand Up @@ -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 )
Expand Down