Skip to content

Commit

Permalink
fix: replacement was not working for urls with special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
selul committed Aug 6, 2019
1 parent 1ab1739 commit 48a4966
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions inc/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function process_urls_from_json( $json ) {
* @return array array of urls.
*/
public function extract_urls_from_json( $content ) {
$regex = '/(?<!(=|\\\\)(?:"|\'|"))(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\??[\w|=|&|\-|\.|:]*)/';
$regex = '/(?<!(=|\\\\)(?:"|\'|"))(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|@|%|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\??[\w|=|&|\-|\.|:]*)/';
preg_match_all(
$regex,
$content,
Expand Down Expand Up @@ -481,7 +481,7 @@ public function process_urls_from_content( $html ) {
* @return array
*/
public function extract_image_urls_from_content( $content ) {
$regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?/';
$regex = '/(?:http(?:s?):)(?:[\/\\\\|.|\w|\s|@|%|-])*\.(?:' . implode( '|', array_keys( Optml_Config::$extensions ) ) . ')(?:\?{1}[\w|=|&|\-|\.|:|;]*)?/';
preg_match_all(
$regex,
$content,
Expand Down
8 changes: 8 additions & 0 deletions tests/test-replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ public function test_replacement_with_relative_url() {
$this->assertContains( 'i.optimole.com', $replaced_content );

}
public function test_replacement_strange_chars(){
$content = '
https://www.example.org/wp-content/uploads/2018/05/@brands.png
https://www.example.org/wp-content/uploads/2018/05/%brands.png
';
$replaced_content = Optml_Manager::instance()->replace_content( $content );
$this->assertEquals( 2, substr_count( $replaced_content, 'i.optimole.com' ) );

}
// TODO We need to extend this to single url replacement. If we make the url extractor regex with option scheme, the parsing will take huge amount of time. We need to think alternatives.
public function test_replacement_without_scheme() {
$content = '<div class="before-footer">
Expand Down

0 comments on commit 48a4966

Please sign in to comment.