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

Fixes 6132: Added tests and new logic for supporting pseudoclasses #6150

Merged
merged 4 commits into from
Sep 12, 2023

Conversation

CrochetFeve0251
Copy link
Contributor

Description

Add support for pseudoclasses for the bg css ll.
For that we refactored the logic from the MappingParser.

Fixes #6132

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue).

Is the solution different from the one proposed during the grooming?

No.

Checklists

Generic development checklist

  • My code follows the style guidelines of this project, with adapted comments and without new warnings.
  • I have added unit and integration tests that prove my fix is effective or that my feature works.
  • The CI passes locally with my changes (including unit tests, integration tests, linter).
  • Any dependent changes have been merged and published in downstream modules.
  • If applicable, I have made corresponding changes to the documentation. Provide a link to the documentation.

Test summary

  • I triggered all changed lines of code at least once without new errors/warnings/notices.
  • I validated all Acceptance Criteria of the related issues. (If applicable, provide proof).
  • I validated all test plan the QA Review asked me to.

If not, detail what you could not test.

Please describe any additional tests you performed.

@CrochetFeve0251 CrochetFeve0251 self-assigned this Sep 7, 2023
@CrochetFeve0251 CrochetFeve0251 added type: bug Indicates an unexpected problem or unintended behavior module: lazyload background css images labels Sep 7, 2023
@CrochetFeve0251 CrochetFeve0251 changed the title Added tests and new logic for supporting pseudoclasses Fixes 6132: Added tests and new logic for supporting pseudoclasses Sep 7, 2023
@CrochetFeve0251 CrochetFeve0251 marked this pull request as ready for review September 7, 2023 12:45
@piotrbak piotrbak requested a review from a team September 8, 2023 11:49
Copy link
Contributor

@engahmeds3ed engahmeds3ed left a comment

Choose a reason for hiding this comment

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

It'll be easier for us to maintain or even review if u changed those two anonymous functions to normal methods in the class.
You can also simplify things by creating a new method to return the filtered pseudo_elements_to_remove

what do u think?

@CrochetFeve0251
Copy link
Contributor Author

It'll be easier for us to maintain or even review if u changed those two anonymous functions to normal methods in the class. You can also simplify things by creating a new method to return the filtered pseudo_elements_to_remove

what do u think?

The second one needs the context I think that using normal function will make it confusing as we will have to use property which is not natural.

@engahmeds3ed
Copy link
Contributor

No need to create a property, I meant something like that (that's a draft):

    private function get_pseudo_elements_to_remove() {
	    $original_pseudo_elements = [
		    ':before',
		    ':after',
		    ':first-line',
		    ':first-letter',
		    ':active',
		    ':hover',
		    ':focus',
		    ':visited',
		    ':focus-within',
		    ':focus-visible',
	    ];

	    /**
	     * Pseudo elements to remove from lazyload CSS selector.
	     *
	     * @param string[] Pseudo elements to remove.
	     */
	    $pseudo_elements_to_remove = apply_filters( 'rocket_lazyload_css_ignored_pseudo_elements', $original_pseudo_elements );

	    if ( ! is_array( $original_pseudo_elements ) ) {
		    $pseudo_elements_to_remove = $original_pseudo_elements;
	    }

	    usort(
		    $pseudo_elements_to_remove,
		    static function ( $first, $second ) {
			    if ( strlen( $first ) === strlen( $second ) ) {
				    return 0;
			    }
			    return ( strlen( $first ) > strlen( $second ) ) ? -1 : 1;
		    }
	    );

        return $pseudo_elements_to_remove;
    }

    public function remove_pseudo_classes_for_selector( $selector ) {
	    $selector = preg_replace( '/::[\w-]+/', '', $selector );

	    $pseudo_elements_to_remove = $this->get_pseudo_elements_to_remove();
	    foreach ( $pseudo_elements_to_remove as $element ) {
		    $selector = str_replace( $element, '', $selector );
	    }
	    if ( in_array( substr( $selector, -1 ), [ '&', '~', '+', '>' ], true ) ) {
		    $selector .= '*';
	    }

	    if ( ! $selector ) {
		    return 'body';
	    }

	    return $selector;
    }

	protected function remove_pseudo_classes( string $selector ): string {
		$result = preg_replace( '/::[\w\-]+/', '', $selector );



		$selectors = explode( ',', $selector );

		$selectors = array_map( array( $this, 'remove_pseudo_classes_for_selector' ), $selectors );

		$selectors = array_unique( $selectors );

		$selector = implode( ',', $selectors );

		if ( ! $selector ) {
			return 'body';
		}

		return (string) $selector;
	}

@vmanthos vmanthos self-requested a review September 11, 2023 11:35
Copy link
Contributor

@vmanthos vmanthos left a comment

Choose a reason for hiding this comment

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

The PR is working. 👍

NOTE

#6158 was discovered during the investigation of this feature.
We'll need to re-test pseudo classes when that's fixed.

@vmanthos vmanthos added this pull request to the merge queue Sep 12, 2023
Merged via the queue into develop with commit 6ff262d Sep 12, 2023
8 checks passed
@vmanthos vmanthos deleted the bug/6132-fix-pseudoclasses branch September 12, 2023 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: lazyload background css images type: bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LazyLoad Background CSS Images pseudo classes handling improvement
3 participants