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

Fix SEF plugin breaking HTML while trying to make inline background url paths absolute #11266

Merged
merged 5 commits into from
Jan 20, 2017

Conversation

ggppdk
Copy link
Contributor

@ggppdk ggppdk commented Jul 23, 2016

Pull Request for Issue #11264 and issue: #7267

The SEF plugin attempts to make inline background url paths absolute but adding / or /jfolder/ to them

Example of broken cases:

<div id='bg-image' style='background-image:url(database/bg-home.jpg);'></div>
<div id='bg-image4' style='background-image:url("database/bg-home.jpg");'></div>
<div id='bg-image5' style='background-image:url(&34;database/bg-home.jpg&34;);'></div>

the above will become:

<div id='bg-image' style="background-image: url('/jfolder/database/bg-home.jpg');'></div>
<div id='bg-image4' style="background-image: url('/jfolder/database/bg-home.jpg');'></div>
<div id='bg-image5' style="background-image: url('/jfolder/&34;database/bg-home.jpg');'></div>

which are obviously broken

Summary of Changes

Added handling of cases

style='...url("...")'
style='...url(&#34;...&#34;);'
style="...url(&#39;...&#39;);"

Testing Instructions

Apply patch and at the bottom of index.php of protostar (just before </body> TAG) add:

<!-- RELATIVE urls (this must be replaced) -->
        <!-- WITHOUT quotes: url() -->
    <div id='rel-image1' style='background-image:url(relative/bg-home.jpg);'></div>
    <div id='rel-image2' style="background-image:url(relative/bg-home.jpg);"></div>
    <div id='rel-image1a' style='color:red; background-image:url(relative/bg-home.jpg);'></div>
    <div id='rel-image2a' style="color:red; background-image:url(relative/bg-home.jpg);"></div>

        <!-- WITH single/double quotes:  url("") or url('') -->
    <div id='rel-image3' style="background-image:url('relative/bg-home.jpg');"></div>
    <div id='rel-image4' style='background-image:url("relative/bg-home.jpg");'></div>
    <div id='rel-image3a' style="color:red; background-image:url('relative/bg-home.jpg');"></div>
    <div id='rel-image4a' style='color:red; background-image:url("relative/bg-home.jpg");'></div>    

        <!-- Single/double quotes written as special characters &#34; &#39;-->
    <div id='rel-image5' style='background-image:url(&#34;relative/bg-home.jpg&#34;);'></div>
    <div id='rel-image6' style="background-image:url(&#39;relative/bg-home.jpg&#39;);"></div>
    <div id='rel-image7' style='background-image:url(&#034;relative/bg-home.jpg&#034;);'></div>
    <div id='rel-image8' style="background-image:url(&#039;relative/bg-home.jpg&#039;);"></div>


<!-- ABSOLUTE urls (plugin must NOT replace) -->
        <!-- WITHOUT quotes: url() -->
    <div id='abs-image1' style='background-image:url(http://absolute_url_dom.org/absolute/bg-home.jpg);'></div>
    <div id='abs-image2' style="background-image:url(http://absolute_url_dom.org/absolute/bg-home.jpg);"></div>
    <div id='abs-image3' style='background-image:url(/absolute/bg-home.jpg);'></div>
    <div id='abs-image4' style="background-image:url(/absolute/bg-home.jpg);"></div>

        <!-- WITH single/double quotes:  url("") or url('') and ABSOLUTE urls-->        
    <div id='abs-image7' style="background-image:url('http://absolute_url_dom.org/absolute/bg-home.jpg');"></div>
    <div id='abs-image8' style='background-image:url("http://absolute_url_dom.org/absolute/bg-home.jpg");'></div>
    <div id='abs-image9' style="background-image:url('/absolute/bg-home.jpg');"></div>
    <div id='abs-image10' style='background-image:url("/absolute/bg-home.jpg");'></div>

        <!-- Case with single/double quotes written as special characters for ABSOLUTE URLs -->
    <div id='bg-image11' style='background-image:url(&#034;http://absolute_url_dom.org/absolute/bg-home.jpg&#034;);'></div>
    <div id='bg-image12' style="background-image:url(&#039;http://absolute_url_dom.org/absolute/bg-home.jpg&#039;);"></div>
    <div id='bg-image13' style='background-image:url(&#34;http://absolute_url_dom.org/absolute/bg-home.jpg&#34;);'></div>
    <div id='bg-image14' style="background-image:url(&#39;http://absolute_url_dom.org/absolute/bg-home.jpg&#39;);"></div>
    <div id='bg-image15' style='background-image:url(&#034;/absolute/bg-home.jpg&#034;);'></div>
    <div id='bg-image16' style="background-image:url(&#039;/absolute/bg-home.jpg&#039;);"></div>
    <div id='bg-image17' style='background-image:url(&#34;/absolute/bg-home.jpg&#34;);'></div>
    <div id='bg-image18' style="background-image:url(&#39;/absolute/bg-home.jpg&#39;);"></div>

after patch the will be no broken HTML

PS: there is almost zero performance impact to the regexp , we simply capture the quotes and use them, the regexp has no other changes

@HLeithner
Copy link
Member

Works for me now. Thx

@ggppdk
Copy link
Contributor Author

ggppdk commented Jul 26, 2016

@HLeithner

please visit
https://issues.joomla.org/tracker/joomla-cms/11266

and click "Test this" and the select "Tested successfuly" to mark a successful test

@HLeithner
Copy link
Member

I have tested this item ✅ successfully on ff76cd8


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11266.

@ggppdk ggppdk changed the title Fix SEF plugin some times breaking HTML while trying to make inline background url paths absolute Fix SEF plugin breaking HTML while trying to make inline background url paths absolute Jul 30, 2016
@hardiktailored
Copy link

I have tested this item ✅ successfully on ff76cd8

Works perfectly.


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11266.

@hardiktailored
Copy link

See results:

Before Patch:
screen shot 2016-08-01 at 06 18 37

After Patch:
(1)
screen shot 2016-08-01 at 06 18 56

(2)
screen shot 2016-08-01 at 06 19 06


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11266.

@brianteeman
Copy link
Contributor

Rtc


This comment was created with the J!Tracker Application at issues.joomla.org/joomla-cms/11266.

@ggppdk
Copy link
Contributor Author

ggppdk commented Aug 3, 2016

No longer RTC, it needs retesting, since i added handling of:

style='background-image:url(&#34;...&#34;);'
style="background-image:url(&#39;...&#39;);"

to fix issue: #7267

correct_replacements

@brianteeman brianteeman removed the RTC This Pull Request is Ready To Commit label Aug 3, 2016
@brianteeman brianteeman added the RTC This Pull Request is Ready To Commit label Sep 2, 2016
@brianteeman brianteeman added this to the Joomla 3.6.3 milestone Sep 2, 2016
@wilsonge wilsonge removed the RTC This Pull Request is Ready To Commit label Sep 2, 2016
@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Sep 2, 2016
@wilsonge wilsonge modified the milestone: Joomla 3.6.3 Sep 2, 2016
@wilsonge wilsonge removed the RTC This Pull Request is Ready To Commit label Sep 2, 2016
@@ -153,8 +153,9 @@ public function onAfterRender()
// Replace all unknown protocols in CSS background image.
if (strpos($buffer, 'style=') !== false)
{
$regex = '#style=\s*[\'\"](.*):\s*url\s*\([\'\"]?(?!/|' . $protocols . '|\#)([^\)\'\"]+)[\'\"]?\)#m';
$buffer = preg_replace($regex, 'style="$1: url(\'' . $base . '$2$3\')', $buffer);
$regex_url = '\s*url\s*\(([\'\"]|\&\#0?3[4|9];)?(?!/|\&\#0?3[4|9];|' . $protocols . '|\#)([^\)\'\"]+)([\'\"]|\&\#0?3[4|9];)?\)';
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you need pipe in [4|9]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, in total we want to detect these:
34, 39, 034, 039

Copy link
Contributor Author

@ggppdk ggppdk Dec 11, 2016

Choose a reason for hiding this comment

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

but now i see, yes pipe is a mistake, although it should not cause a problem since character #\03| does not exist

@ggppdk
Copy link
Contributor Author

ggppdk commented Dec 11, 2016

I have removed the redudant pipe character, if that case was ever encountered then HTML is already broken

$buffer = preg_replace($regex, 'style="$1: url(\'' . $base . '$2$3\')', $buffer);
$regex_url = '\s*url\s*\(([\'\"]|\&\#0?3[49];)?(?!/|\&\#0?3[49];|' . $protocols . '|\#)([^\)\'\"]+)([\'\"]|\&\#0?3[49];)?\)';
$regex = '#style=\s*([\'\"])(.*):' . $regex_url . '#m';
$buffer = preg_replace($regex, 'style=$1$2: url($3' . $base . '$4$5$6)', $buffer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove '$6' from '$4$5$6)' and I can mark as a successful test.

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 do not remember now, but by looking at $6 it is the closing quote ? which is either ' or "

Copy link
Contributor

Choose a reason for hiding this comment

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

Whole regex:

#style=\s*([\'\"])(.*):\s*url\s*\(([\'\"]|\&\#0?3[49];)?(?!/|\&\#0?3[49];|' . $protocols . '|\#)([^\)\'\"]+)([\'\"]|\&\#0?3[49];)?\)#
#style=\s*(______)(__):\s*url\s*\((___________________)?(?!/|\&\#0?3[49];|' . $protocols . '|\#)(__________)(___________________)?\)#

I counted to five:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are six, did you test it without $6 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I tested on your examples. If I remove $5 too then ' or " are missing. Without $6 everything is OK.
(?!....) - it is not captured by regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, i have taken time to look at it again and counted the blocks,
you are right, the $6 capturing block does not exist,
there are only 5 capturing blocks in the regular expressions, i will update the PR,
I missed it , as it does not cause error because $6 capturing block is always empty

@ggppdk
Copy link
Contributor Author

ggppdk commented Jan 20, 2017

I have removed the redudant $6 replacement of the 6th capturing block, as it does not the 6th capturing block, does not exist.
It should continue to work as before

@csthomas
Copy link
Contributor

I have tested this item ✅ successfully on b616e60


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/11266.

1 similar comment
@dgrammatiko
Copy link
Contributor

I have tested this item ✅ successfully on b616e60


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/11266.

@joomla-cms-bot joomla-cms-bot added the RTC This Pull Request is Ready To Commit label Jan 20, 2017
@dgrammatiko
Copy link
Contributor

RTC

Thanks @ggppdk


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/11266.

@zero-24 zero-24 added this to the Joomla 3.7.0 milestone Jan 20, 2017
@rdeutz rdeutz merged commit 7d34d41 into joomla:staging Jan 20, 2017
@joomla-cms-bot joomla-cms-bot removed the RTC This Pull Request is Ready To Commit label Jan 20, 2017
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.

10 participants