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

New: Add pseudos that depend on optional Adapter APIs #139

Merged
merged 2 commits into from
Nov 9, 2019

Conversation

jaspreet57
Copy link
Contributor

Pseudos like element state hover, active, visited, etc can be supported conditionally.

This PR enables such pseudos to be supported only if associated Adapter method is implemented.

For example:
hover pseudo may give results, only if isHovered is implemented in given Adapter APIs. Otherwise, hover will result into falseFunc and will give empty result.

This logic can be improved in various ways. Please review once and see my comments on code and let me know if I am going in right direction.

I will be updating Readme and Test Cases once logic is finalised.

Resolves: #80

@coveralls
Copy link

coveralls commented Aug 18, 2019

Coverage Status

Coverage decreased (-1.07%) to 97.009% when pulling ed044ff on jaspreet57:feat/element-state-support into 184180e on fb55:master.

/**
* is the element in hovered state?
*/
isHovered?: (elem: ElementNode) => boolean;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I name it as getHovered or isHovered is fine?
This method checks if give elem is in hovered state or not. So I named it as isHovered.

Instead if we could have used getState and then check state === 'hovered', then get suits more.

Copy link
Owner

Choose a reason for hiding this comment

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

I'm okay with these — just having a subset available seems alright.

lib/pseudos.js Outdated
@@ -189,6 +189,13 @@ function getFirstElement(elems, adapter) {
}
}

//pseudos that depends on optional Adapter methods. (key, value) = (pseudoName, adapterMethod)
var pseudosWithOptionalImplementation = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept this list, so that before getting function for particular pseudo, we can check if its associated Adapter method is implemented or not. Otherwise we will simply use falseFunc.

lib/pseudos.js Outdated
@@ -262,7 +269,24 @@ var pseudos = {
link: function(elem, adapter) {
return adapter.hasAttrib(elem, "href");
},
visited: falseFunc, //Valid implementation
hover: function(elem, adapter) {
if (adapter.isHovered) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am checking this here also in case pseudo function is being used directly instead of from compile.

As we are exporting all pseudos, third party can use this function directly also.

Copy link
Owner

Choose a reason for hiding this comment

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

My preference would be to implement these as filters, which can return falseFunc themselves.

@jaspreet57
Copy link
Contributor Author

jaspreet57 commented Aug 21, 2019

Hi @fb55 , could you please review the current code, and clear my doubts so that I can proceed on this PR.

Copy link
Owner

@fb55 fb55 left a comment

Choose a reason for hiding this comment

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

Looks good so far. Two things I'd like to see are 1) implement the pseudo-selectors as filters and 2) some test cases.

/**
* is the element in hovered state?
*/
isHovered?: (elem: ElementNode) => boolean;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm okay with these — just having a subset available seems alright.

lib/pseudos.js Outdated
@@ -262,7 +269,24 @@ var pseudos = {
link: function(elem, adapter) {
return adapter.hasAttrib(elem, "href");
},
visited: falseFunc, //Valid implementation
hover: function(elem, adapter) {
if (adapter.isHovered) {
Copy link
Owner

Choose a reason for hiding this comment

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

My preference would be to implement these as filters, which can return falseFunc themselves.

@jaspreet57
Copy link
Contributor Author

@fb55 Thanks for the feedback. I have added these pseudos as filters.

Could you help me in test cases. Here test cases seems to depend on the implementation of default Adapter APIs.

So, to get full coverage of new pseudos, we somehow have to provide the implementation of optional Adapter methods.

One way is to pass my own custom Adapter. Another way could be to mock something else.

Thoughts please !

@fb55 fb55 merged commit fa51bcf into fb55:master Nov 9, 2019
@fb55
Copy link
Owner

fb55 commented Nov 9, 2019

Given that the current behavior is now covered by a test, I'm okay with merging as-is in order of not blocking this any further. Thanks a lot @jaspreet57!

@jaspreet57 jaspreet57 deleted the feat/element-state-support branch April 26, 2020 04:54
fb55 added a commit that referenced this pull request Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add element state support (e.g. :hover, :active)
3 participants