-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Implemented color normalizer #4366
Conversation
Thank you for rebasing. I have some issues with solving I may be wrong - but after fix commit there are two One in the proper namespace Line 2096 in bbc6ca5
The second one is without any namespace: Line 2240 in bbc6ca5
The only diff is in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code
I had to rethink the entire approach especially that we have an issue with circular deps here. It is caused by the fact that Color
class tries to use createClass
function from tools
and tools
refers to some method/props from Color
. From what I see createClass
doesn't give us many benefits (almost none I suppose) and the complications caused by using it are quite significant... So I would get rid of creating Color
class this way and go with another standard way we use - see for example
Lines 11 to 34 in c01d652
/** | |
* Represents a DOM element. | |
* | |
* // Create a new <span> element. | |
* var element = new CKEDITOR.dom.element( 'span' ); | |
* | |
* // Create an element based on a native DOM element. | |
* var element = new CKEDITOR.dom.element( document.getElementById( 'myId' ) ); | |
* | |
* @class | |
* @extends CKEDITOR.dom.node | |
* @constructor Creates an element class instance. | |
* @param {Object/String} element A native DOM element or the element name for | |
* new elements. | |
* @param {CKEDITOR.dom.document} [ownerDocument] The document that will contain | |
* the element in case of element creation. | |
*/ | |
CKEDITOR.dom.element = function( element, ownerDocument ) { | |
if ( typeof element == 'string' ) | |
element = ( ownerDocument ? ownerDocument.$ : document ).createElement( element ); | |
// Call the base constructor (we must not call CKEDITOR.dom.node). | |
CKEDITOR.dom.domObject.call( this, element ); | |
}; |
This would mean we can get rid of all the magic from https://github.com/ckeditor/ckeditor4/pull/4366/files#diff-82fb93ef0501268a8be9290add301a266e430ea74b7f96dd0d437b10e6183de9R444 and simplify Color
class.
Also I'm not sure about changes in tools.js
. Assuming we would go with the above proposal (so getting rid of circular dependency), tools itself would not require flattening entire CKEDITOR.tools
object and could exists in the current form. It would only need to refer to some of (The Color
props/methods (liked namedColors
) but since there will be direct, one-way dependency it will no longer cause any issues.Color
class should inject those as mentioned in #4366 (comment)).
Other things:
- I would be for moving
colors.js
tocore/tools/color.js
. - Something strange is going on since I see
colornormalizer.js
andcolor.js
which has the same (partially) code 🤔 Some rebase issue probably? I assume thecolor.js
one is correct so will add my comments there. And as you also pointed out in Implemented color normalizer #4366 (comment) thatzip
function got duplicated during my rebase... 🤔 Could I ask you to do a clean up here? - Please see also review comments.
I hadn't dive deeper into the code since it will be good to first clean up a little and then we can polish the whole thing.
I feel like some clarifications are needed here 🤔💡 The current approach assumes that I had some doubts whether |
Realize that there is no API-doc in my PR. But maybe it will be good if I wait till all code will be accepted? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, starting to look good 👍
Apart from review comments, I would also suggest cleaning-up color.js
a little:
Realize that there is no API-doc in my PR. But maybe it will be good if I wait till all code will be accepted?
I think it will be good opportunity to clean up color.js
class and make it more readable (and also easier to review 😉 ) so I think it's good idea to work on docs now. So:
- Please add API docs to
color.js
file. Please remember about descriptive docs and marking private methods with@private
etc. - Also I would suggest slight code restructuring - getters should go firts then setters and then all the related helpers.
- If you think any method should not be accessible in any way through API you can move it out from class scope as a local function (e.g.
clamp
function, percentage normalization, etc).
I'm not sure about Lines 1078 to 1084 in 88adc86
Marked it as deprecated before, but now I'm not sure if I should move it to |
I made all the requested changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As for the main part of the code (color.js
) we agreed with @sculpt0r for a pair refactoring session to polish the solution together. Apart form that I'm leaving few comments related to docs and tests (which we will cover too).
Documentation
There are some invalid API docs properties:
Tests
See review comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took this occasion to polish the code a little. Extracting and operating on RGBA color channels simplifies code visibly, but still there were some unnecessary conversions between formats in parseInput()
method. Since we need color channels only, it makes more sense to extract them without converting all formats to HEX first (still we will need to have color channels conversion from HSLA since it uses different representation). And so my proposal is to just introduce extraction methods - I already prepared one which handles HEX - see 6f52e75. RGBA and HSLA still needs to be covered.
I tried to split refactoring into separate logical parts (commits) so you can see what and why specific changes were applied. And so:
- 6f52e75 - as already mentioned switching from unnecessary conversions to RGBA channels extraction.
- 3a51038 - simplified HEX getters. You have introduced higher order function (method to be precise)
getColorValue()
only to handle simple logic in getters. This significantly increases code complexity and doesn't really bring any benefit (apart from few lines of code less). - cc13329 - just paying attention to details in API docs and minor rewordings. Please also keep in mind that all types in API docs type annotations should start with uppercase like
{String}
or{Function}
. - fec25d5 - moved
blendAlphaColor()
method outside of class scope. I'm not sure, on the second thought it seems all color related functions/methods should be color class private members 🤔 - f5e360e - simplified HEX3 to HEX6 conversion. If you have a method like this, which does one thing only and the input is always the same (sting with fixed length here) there is no reason to complicate things. Also since it's private method, you can see based on it's usage that it will always get valid input.
- 40b0748 - since we are going to extract color channels from initial formats, there is no need for those conversion methods.
- 79e630f - again attention to details, fixed camelCase for static properties and added proper spacing between API comments there.
- 413997d - at this point I build CKEditor 4 docs locally to check how it looks, there was one missing
@private
annotation.
There is also some stuff mentioned in previous comments not yet covered:
- Implemented color normalizer #4366 (comment)
- Implemented color normalizer #4366 (comment)
-
I would be for moving colors.js to core/tools/color.js. (see Implemented color normalizer #4366 (review))
As for now, unit tests related to RGBA and HSLA handling are failing. This is due missing extraction methods for those formats. I would like to ask you @sculpt0r to add these methods, cleanup unnecessary stuff and polish API docs if necessary.
It is possible now to pass four values for not alpha prefix. So `rgb` or `hsl` should have only three values in round brackets.
It was possible to pass four arguments and declared prefix without alpha: rgb(10,10,10,1) which is not valid color code. Same for hsl.
We have mismatch with `rgb` and `RGB` naming.
I made corrects according to #4366 (comment)
Also, I thought that range inclusiveness (while checking if a variable is within range) is more obvious in the new form d17b51b Found missed cases with invalid arguments number according to color code prefix (rgb & hsl). So I add two failing tests for it 943dd73 and then fixed it in e2b23c1 There was a mismatch in test naming: sometimes Finally, I rebased on the newest Now it's ready for another review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 🎉 🍰
Code
👍
Docs
Did some minor polishing only 👍
As for CKEDITOR.tools.convertRgbToHex()
function it cannot be entirely replaced by color
class since it operates on styles string, for example:
background-color: rgb(255,255,255); text-align: center;
so it does a little more. I have removed deprecated annotation and rephrase Note
there.
Tests
- IE8 - IE11
- Chrome
- Firefox
- Safari
Apart from the above, this PR does not introduce any feature but extends public API. In general, such changes should be targeted to minor release (master
branch). However, since we deprecate some API here too it means major
release (major
branch) 👍
What is the purpose of this pull request?
New feature: Implemented color normalizer. Refactor color operations in
CKEDITOR.tools
Does your PR contain necessary tests?
All patches that change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.
This PR contains
Did you follow the CKEditor 4 code style guide?
Your code should follow the guidelines from the CKEditor 4 code style guide which helps keep the entire codebase consistent.
What is the proposed changelog entry for this pull request?
What changes did you make?
Added new class
Color
incore
inCKEDITOR.tools.style
namespace.Added Color as a dependency for
tools
. AlsoColor
depends ontools
.Move color operations, RegExps, and
Names Colors
intoColors
.tools
preserved old methods (for backward compatibility). Now old methods and properties are usingColor
object inside.Color
constructor hascompatibility
flag as second parameter. It can be skipped, soColor
class should behave as expected - so only valid color codes can be received (with default color:#000000
). If compatibility flag is passed, then if there is no match toNamed Colors
,HEX
,RGB
orHSL
input string (formerly first constructor argument) is returned.Due to circular dependency (
Color
needsCKEDITOR.tools.createClass
andtools
needsColor
for backward compatibility):Color
class wrap class creation inside function. Until firstColor
object is created - there is no need fortools
Color
andtools
are checking if a namespace is already declared.tools
namespace has now a more extensible approach, instead of 'declaration'. Before,Color
was declared inCKEDITOR.tools.style
namespace and aftertools
loaded - namespace was overwritten. A new namespace declaration was done for almost every object intools
so it could be a nice step to separate other classes from this swollen file.Which issues does your PR resolve?
Closes #4358