-
Notifications
You must be signed in to change notification settings - Fork 110
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
Optimization Detective's XPath expressions in HTML can be erroneously interpreted as JS/CSS comments #1947
Comments
Hi, I'm a bit new to the performance plugin but I would like to work on this. |
Hi, @nani-samireddy welcome to WP-Performance community, feel free to reach out if stuck while solving this issue. 😄 |
Thank you @sarthak-19 I have spent few hours on this and here are my findings: Here are setting the xpath attribute in the PHP. So we can change the following performance/plugins/optimization-detective/optimization.php Lines 346 to 348 in a3b0d7c
To if ( $tracked_in_url_metrics && $needs_detection ) {
$processor->set_meta_attribute( 'xpath', base64_encode( $processor->get_xpath() ) );
} And in the Javascript we are retrieving here: performance/plugins/optimization-detective/detect.js Lines 587 to 595 in a3b0d7c
To const breadcrumbedElementsMap = new Map(
[ ...breadcrumbedElements ].map(
/**
* @param {HTMLElement} element
* @return {[HTMLElement, string]} Tuple of element and its XPath.
*/
( element ) => [ element, atob( element.dataset.odXpath ) ]
)
); I tested this on But as I'm new to performance plugins cloud anyone help me to resolve this error on trunk Because of this error I tested this on also please let me know your thoughts on the above solution. cc: @westonruter |
@nani-samireddy Hello! Welcome. And your research looks correct. FYI: The latest release is Nevertheless, I'm not sure yet whether we should implement this change. It's something I opened to discuss. Given it addresses a specific compatibility problem observed in another plugin, I'm wanting to find out if there are more examples of this where we should go ahead and implement this workaround. In regards to the error you're seeing, this is expected when using wp-env, because it still doesn't support loopback requests. Nevertheless, the plugin specifically excludes this from blocking pages from being optimized by checking if it is a performance/plugins/optimization-detective/optimization.php Lines 88 to 91 in a3b0d7c
|
@westonruter Thank you for the warm welcome. As you mentioned, let's wait for more insights and other opinions on this and move forward accordingly. |
Bug Description
I just discovered that certain plugins (example) may implement HTML compression that collapses whitespace and removes JS/CSS comments by means of regular expressions. For example (do not do this):
This is dangerous and will often result in corrupted HTML markup.
This has the effect of turning this:
Into:
This is because the regular expression starts matching the supposed comment start in the first paragraph which mentions
/HTML/BODY/*
and then it terminates the "comment" in the code sample in the third block.This will break pages that contain
data-od-xpath
attributes which Optimization Detective adds when detection is needed.Granted, such regular expression logic will invariably cause many more problems than this, but we might want to consider hardening the
data-od-xpath
attribute to prevent this from happening, namely by doing something like base64-encoding it in the attribute viabase64_encode()
in PHP. It could then be converted back to the non-encoded form in JavaScript viaatob()
.Just something to consider.
The text was updated successfully, but these errors were encountered: