Skip to content

Commit

Permalink
Fix forceSimpleAmpersand config implementation
Browse files Browse the repository at this point in the history
Run ampersand replacement after the  htmlEncodeAttr, otherwise the results are overwritten (#965)
  • Loading branch information
alexmaris authored and mlewand committed Sep 14, 2018
1 parent 7880739 commit 48eeb5f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugins/htmlwriter/plugin.js
Expand Up @@ -179,9 +179,11 @@ CKEDITOR.htmlWriter = CKEDITOR.tools.createClass( {
attribute: function( attName, attValue ) {

if ( typeof attValue == 'string' ) {
this.forceSimpleAmpersand && ( attValue = attValue.replace( /&/g, '&' ) );
// Browsers don't always escape special character in attribute values. (https://dev.ckeditor.com/ticket/4683, https://dev.ckeditor.com/ticket/4719).
attValue = CKEDITOR.tools.htmlEncodeAttr( attValue );

// Run ampersand replacement after the htmlEncodeAttr, otherwise the results are overwritten (https://github.com/ckeditor/ckeditor-dev/issues/965)
this.forceSimpleAmpersand && ( attValue = attValue.replace( /&/g, '&' ) );
}

this._.output.push( ' ', attName, '="', attValue, '"' );
Expand Down
36 changes: 35 additions & 1 deletion tests/plugins/htmlwriter/htmlwriter.js
Expand Up @@ -39,5 +39,39 @@ bender.test( {
assert.areSame( afterFormat, bot.getData( false, false ) );
} );
} );
},
'test editor config.forceSimpleAmpersand in html element attributes': function () {
var data = '<p><a href="http://www.blah.com?foo=1&bar=2">Test link</a></p>';
bender.editorBot.create({
name: 'basic_forceSimpleAmpersand',
formattedOutput: true,

config: {
allowedContent: true,
forceSimpleAmpersand: true,

on: {
instanceReady: function (evt) {
var wrtierConfig = {
indent: true,
breakBeforeOpen: false,
breakAfterOpen: false,
breakBeforeClose: false,
breakAfterClose: false
};

evt.editor.dataProcessor.writer.setRules('p', wrtierConfig);
evt.editor.dataProcessor.writer.setRules('div', wrtierConfig);
}
}
}
}, function (bot) {
bot.setData( data, function () {
var afterFormat = bot.getData( false, false);

// Trigger getData a second time to reveal bug.
assert.areSame( afterFormat, data);
});
});
}
} );
} );

0 comments on commit 48eeb5f

Please sign in to comment.