-
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
Added htmlParser.element.findOne #2719
Conversation
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.
findOne
method should have the same signature as find
one, therefore recursive search should be activated via recursive
parameter.
core/htmlparser/element.js
Outdated
* | ||
* @param {String/Function} criteria Tag name or evaluator function. | ||
* @param {CKEDITOR.htmlParser.node} criteria.child The currently iterated child. | ||
* @returns {CKEDITOR.htmlParser.node} First matched child, `undefined` otherwise. |
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.
If undefined
is also allowed, then returned type is CKEDITOR.htmlParser.node/undefined
.
Additionally IMO it should return null
, as every API searching for elements returns it in case of no match.
core/htmlparser/element.js
Outdated
*/ | ||
findOne: function( criteria ) { | ||
var findMethod = typeof criteria === 'function' ? criteria : findByName, | ||
nestedMatch, |
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.
Variables without value should be declared at the end.
core/htmlparser/element.js
Outdated
match = CKEDITOR.tools.array.find( this.children, function( child ) { | ||
if ( findMethod( child ) ) { | ||
return true; | ||
} else if ( child.children && child.findOne ) { |
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.
This else if
statement is not necessary, as execution will end on return true;
statement, if the first condition is true.
tests/core/htmlparser/element.js
Outdated
@@ -16,6 +16,14 @@ function write( el ) { | |||
return writer.getHtml(); | |||
} | |||
|
|||
var testHtml = '<body>' + | |||
'<p>Foo' + | |||
'<div>Baz' + |
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.
This HTML is incorrect: div
can't be nested in p
tag.
|
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.
Overall looks good, only some minor issues left.
Additionally I'm thinking about adding unit test with deeper, 2/3 level recursion – just to be sure that it really goes deeper than the first child.
core/htmlparser/element.js
Outdated
|
||
return nestedMatch || match || null; | ||
|
||
function findByName( item ) { |
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.
This method can be inlined into ret
declaration.
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.
We can also rename ret
to something more meaningful, like match
or something like that.
} | ||
node = element.findOne( function( node ) { | ||
return node.value && node.value.indexOf( symbol ) > -1; | ||
}, true ), |
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.
Code style issue – there should be one more indentation level.
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.
Adding indentation level would make closing bracket higher indentation than opening, shouldn't they be the same?
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.
It's a very similar case to objects in such lists and they are indented with that extra tab: https://github.com/ckeditor/ckeditor-dev/blob/1f365357c043a74e69ad21f32c829929d8610106/plugins/tableselection/plugin.js#L893-L901
( function() { | ||
'use strict'; | ||
|
||
var testElement = CKEDITOR.htmlParser.fragment.fromHtml( |
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 wonder if it wouldn't be more readable to move the HTML into separate file, elementfindone.html
.
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.
Looks good, just some couple of style issues.
} | ||
node = element.findOne( function( node ) { | ||
return node.value && node.value.indexOf( symbol ) > -1; | ||
}, true ), |
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.
It's a very similar case to objects in such lists and they are indented with that extra tab: https://github.com/ckeditor/ckeditor-dev/blob/1f365357c043a74e69ad21f32c829929d8610106/plugins/tableselection/plugin.js#L893-L901
|
||
element.forEach( function( node ) { | ||
if ( node ) { | ||
// Since symbol may contains special characters we use `indexOf` (instead of RegExp) which is sufficient (#877). |
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.
indexOf
was moved up, so the comment also should.
core/htmlparser/element.js
Outdated
* @param {String/Function} criteria Tag name or evaluator function. | ||
* @param {CKEDITOR.htmlParser.node} criteria.child The currently iterated child. | ||
* @param {Boolean} [recursive=false] If set to `true` will iterate all descendants, otherwise only direct children. | ||
* @returns {CKEDITOR.htmlParser.node} First matched child, `null` otherwise. |
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.
It should be marked as nullable type.
core/htmlparser/element.js
Outdated
* @returns {CKEDITOR.htmlParser.node} First matched child, `null` otherwise. | ||
*/ | ||
findOne: function( criteria, recursive ) { | ||
var findMethod = typeof criteria === 'function' ? criteria : function( item ) { |
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.
By inlining I meant "inline it directly into CKEDITOR.tools.array.find
callback":
var isMatching = typeof criteria === 'function' ? criteria( child ) : item.name === criteria;
IMO such version is still readable and dedicated function findMethod
is not necessary in this case.
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.
Actually I wanted to requested changes… Sorry for misclick.
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, however http://tests.ckeditor.test:1030/tests/core/htmlparser/elementfindone#tests%2Fcore%2Fhtmlparser%2Felementfindone%20test%20findOne%20by%20function fails. Additionally failure message is not visible.
Fixed failing test. Fail message was wrongly placed as |
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.
http://tests.ckeditor.test:1030/tests/core/htmlparser/elementfindone#tests%2Fcore%2Fhtmlparser%2Felementfindone%20test%20findOne%20deep%20nested%20child fails in IE8. Everything else LGTM. I've also rebased branch onto latest major
and corrected API docs. I've removed criteria.child
from them as it was rendered as property of criteria
parameter, not a parameter passed to it.
IE cuts some of text nodes. To make test constant regardless of environment I've moved tested html code back into js file and removed html file. |
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!
What is the purpose of this pull request?
New feature
Does your PR contain necessary tests?
All patches which 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
What changes did you make?
CKEDITOR.htmlParser.element.findOne
removeSymbolText
.Closes #2698.