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

Throw friendly error for invalid/missing id's on editor creation #2748

Closed
blzaugg opened this issue Jan 11, 2019 · 4 comments
Closed

Throw friendly error for invalid/missing id's on editor creation #2748

blzaugg opened this issue Jan 11, 2019 · 4 comments
Labels
changelog:api A changelog entry should be put in the API section of the changelog. status:confirmed An issue confirmed by the development team. target:major Any docs related issue that should be merged into a major branch. type:feature A feature request.
Milestone

Comments

@blzaugg
Copy link
Contributor

blzaugg commented Jan 11, 2019

Type of report

Feature request

Provide description of the new feature

Given the code below:

<textarea id="editor1"></textarea>

<script>
    CKEDITOR.replace('editor2');
</script>

The developer intended to target editor1, but by mistake targeted editor2, which doesn't exist in the DOM.

Doing this causes an error.

Uncaught TypeError: Cannot read property 'getEditor' of undefined

Without looking at the CKEditor source, it's not obvious what's wrong.

The Feature request is to throw a friendly error for invalid/missing id's.

Solution:

diff --git a/core/creators/inline.js b/core/creators/inline.js
index 61f88b2..d48af34 100644
--- a/core/creators/inline.js
+++ b/core/creators/inline.js
@@ -31,8 +31,15 @@
                if ( !CKEDITOR.env.isCompatible )
                        return null;

+               var elementArg = element;
+
                element = CKEDITOR.dom.element.get( element );

+               // Throw error on missing target element.
+               if ( !element ) {
+                       throw 'The provided element, "' + elementArg + '", is missing from the DOM.';
+               }
+
                // Avoid multiple inline editor instances on the same element.
                if ( element.getEditor() )
                        throw 'The editor instance "' + element.getEditor().name + '" is already attached to the provided element.';
diff --git a/core/creators/themedui.js b/core/creators/themedui.js
index 3b1b00b..814b815 100644
--- a/core/creators/themedui.js
+++ b/core/creators/themedui.js
@@ -323,8 +323,15 @@ CKEDITOR.replaceClass = 'ckeditor';
                if ( !CKEDITOR.env.isCompatible )
                        return null;

+               var elementArg = element;
+
                element = CKEDITOR.dom.element.get( element );

+               // Throw error on missing target element.
+               if ( !element ) {
+                       throw 'The provided element, "' + elementArg + '", is missing from the DOM.';
+               }
+
                // Avoid multiple inline editor instances on the same element.
                if ( element.getEditor() )
                        throw 'The editor instance "' + element.getEditor().name + '" is already attached to the provided element.';
@jacekbogdanski jacekbogdanski self-assigned this Jan 14, 2019
@jacekbogdanski jacekbogdanski added type:bug A bug. status:confirmed An issue confirmed by the development team. labels Jan 14, 2019
@jacekbogdanski jacekbogdanski removed their assignment Jan 14, 2019
@jacekbogdanski
Copy link
Member

Hello,

this is the kind of the issue which I prefer to label as a bug rather than a feature cause it's more like missing implementation. @blzaugg as I see you already wrote some code, would you like to create PR for the issue?

@blzaugg
Copy link
Contributor Author

blzaugg commented Jan 15, 2019

Sure. I'll send a PR tomorrow.

@mlewand
Copy link
Contributor

mlewand commented Feb 6, 2019

@jacekbogdanski it is actually a more of new feature rather than bugfix. I'll revert the original issue type, and because of that also the PR will need to be targetted at major branch instead. But Reviewer will handle this. Sorry for minor confusion 🙂

@mlewand mlewand added type:feature A feature request. target:major Any docs related issue that should be merged into a major branch. changelog:api A changelog entry should be put in the API section of the changelog. and removed type:bug A bug. labels Feb 6, 2019
Comandeer added a commit that referenced this issue Feb 6, 2019
Fixes #2748 Throw friendly error for invalid/missing id's on editor creation
@mlewand mlewand added this to the 4.12.0 milestone Feb 7, 2019
@mlewand
Copy link
Contributor

mlewand commented Feb 7, 2019

Cool, the fix has been included and is staged to be released with 4.12.0. Thanks for the contribution @blzaugg!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:api A changelog entry should be put in the API section of the changelog. status:confirmed An issue confirmed by the development team. target:major Any docs related issue that should be merged into a major branch. type:feature A feature request.
Projects
None yet
Development

No branches or pull requests

4 participants