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

Support custom type attribute in scriptLoader method with new parameter T/5434 #5435

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions core/scriptloader.js
Expand Up @@ -36,6 +36,8 @@ CKEDITOR.scriptLoader = ( function() {
* alert( 'Number of failures: ' + failed.length );
* } );
*
* CKEDITOR.scriptLoader.load( '/myscript.js', callback, CKEDITOR, false, 'module' );
*
* @param {String/Array} scriptUrl One or more URLs pointing to the
* scripts to be loaded.
* @param {Function} [callback] A function to be called when the script
Expand All @@ -48,8 +50,10 @@ CKEDITOR.scriptLoader = ( function() {
* the callback call. Defaults to {@link CKEDITOR}.
* @param {Boolean} [showBusy] Changes the cursor of the document while
* the script is loaded.
* @param {String} [typeAttribute] Set a custom type attribute on the
* script tag. Defaults to `text/javascript`.
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To indicate the default value of the parameter, please use the [key=value] syntax – it's automatically recognized by our documentation generator and transformed into appropriate information about the default value:

Suggested change
* @param {String} [typeAttribute] Set a custom type attribute on the
* script tag. Defaults to `text/javascript`.
* @param {String} [typeAttribute='text/javascript'] Set a custom `type` attribute on the
* script tag.

*/
load: function( scriptUrl, callback, scope, showBusy ) {
load: function( scriptUrl, callback, scope, showBusy, typeAttribute ) {
var isString = ( typeof scriptUrl == 'string' );

if ( isString )
Expand All @@ -58,6 +62,9 @@ CKEDITOR.scriptLoader = ( function() {
if ( !scope )
scope = CKEDITOR;

if ( !typeAttribute )
typeAttribute = 'text/javascript';
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In newer code we use braces. There are absent in this file due to its age (in older code we were often omitting them):

Suggested change
if ( !typeAttribute )
typeAttribute = 'text/javascript';
if ( !typeAttribute ) {
typeAttribute = 'text/javascript';
}


var scriptCount = scriptUrl.length,
scriptCountDoCallback = scriptCount,
completed = [],
Expand Down Expand Up @@ -115,7 +122,7 @@ CKEDITOR.scriptLoader = ( function() {
// Create the <script> element.
var script = new CKEDITOR.dom.element( 'script' );
script.setAttributes( {
type: 'text/javascript',
type: typeAttribute,
src: url
} );

Expand Down
17 changes: 17 additions & 0 deletions tests/core/scriptloader.js
Expand Up @@ -25,6 +25,23 @@ var tests = {
this.wait();
},

'test load with custom type attribute': function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test fails in all browsers. The first reason is the usage of the script.$.attr() method. The script.$ property points to the native script element, so to get its type you can use script.$.type.

However, even after this change, the test still fails – probably due to the fact that the ../_assets/sample.js script is already added to the document and the findOne() method gets the first added script element with it. You should check the latest added script.

Additionally, this test won't work in Internet Explorer as it won't preserve the module value of the type attribute. I'm not sure if it's possible to create a test that would work there. Could you check it and – if it's not possible – ignore the test there? For ignoring you could use a code similar to the one below:

if ( CKEDITOR.env.ie ) {
	assert.ignore();
}

var tc = this;

function callback() {
tc.resume( function() {
var script = CKEDITOR.document.findOne( 'script[src="../_assets/sample.js"]' );

assert.areSame( script.$.attr( 'type' ), 'module' );
assert.areSame( 'Test!', testVar );
} );
}

CKEDITOR.scriptLoader.load( '../_assets/sample.js', callback, CKEDITOR, false, 'module' );

this.wait();
},

'test load event handling': function() {
var tc = this;

Expand Down