Completed
Pull Request — 8.x-1.x (#32)
by Janez
04:18
created
src/Plugin/Filter/ConvertUrlToEmbedFilter.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -78,25 +78,25 @@  discard block
 block discarded – undo
78 78
     // optional for all protocols.
79 79
     // @see \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols()
80 80
     $protocols = \Drupal::getContainer()->getParameter('filter_protocols');
81
-    $protocols = implode(':(?://)?|', $protocols) . ':(?://)?';
81
+    $protocols = implode(':(?://)?|', $protocols).':(?://)?';
82 82
 
83 83
     $valid_url_path_characters = "[\p{L}\p{M}\p{N}!\*\';:=\+,\.\$\/%#\[\]\-_~@&]";
84 84
 
85 85
     // Allow URL paths to contain balanced parens
86 86
     // 1. Used in Wikipedia URLs like /Primer_(film)
87 87
     // 2. Used in IIS sessions like /S(dfd346)/
88
-    $valid_url_balanced_parens = '\('. $valid_url_path_characters . '+\)';
88
+    $valid_url_balanced_parens = '\('.$valid_url_path_characters.'+\)';
89 89
 
90 90
     // Valid end-of-path characters (so /foo. does not gobble the period).
91 91
     // 1. Allow =&# for empty URL parameters and other URL-join artifacts
92
-    $valid_url_ending_characters = '[\p{L}\p{M}\p{N}:_+~#=/]|(?:' . $valid_url_balanced_parens . ')';
92
+    $valid_url_ending_characters = '[\p{L}\p{M}\p{N}:_+~#=/]|(?:'.$valid_url_balanced_parens.')';
93 93
 
94 94
     $valid_url_query_chars = '[a-zA-Z0-9!?\*\'@\(\);:&=\+\$\/%#\[\]\-_\.,~|]';
95 95
     $valid_url_query_ending_chars = '[a-zA-Z0-9_&=#\/]';
96 96
 
97 97
     //full path
98 98
     //and allow @ in a url, but only in the middle. Catch things like http://example.com/@user/
99
-    $valid_url_path = '(?:(?:'.$valid_url_path_characters . '*(?:'.$valid_url_balanced_parens .$valid_url_path_characters . '*)*'. $valid_url_ending_characters . ')|(?:@' . $valid_url_path_characters . '+\/))';
99
+    $valid_url_path = '(?:(?:'.$valid_url_path_characters.'*(?:'.$valid_url_balanced_parens.$valid_url_path_characters.'*)*'.$valid_url_ending_characters.')|(?:@'.$valid_url_path_characters.'+\/))';
100 100
 
101 101
     // Prepare domain name pattern.
102 102
     // The ICANN seems to be on track towards accepting more diverse top level
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
     $domain = '(?:[\p{L}\p{M}\p{N}._+-]+\.)?[\p{L}\p{M}]{2,64}\b';
106 106
     $ip = '(?:[0-9]{1,3}\.){3}[0-9]{1,3}';
107 107
     $auth = '[\p{L}\p{M}\p{N}:%_+*~#?&=.,/;-]+@';
108
-    $trail = '('.$valid_url_path.'*)?(\\?'.$valid_url_query_chars .'*'.$valid_url_query_ending_chars.')?';
108
+    $trail = '('.$valid_url_path.'*)?(\\?'.$valid_url_query_chars.'*'.$valid_url_query_ending_chars.')?';
109 109
 
110 110
     // Match absolute URLs.
111 111
     $url_pattern = "(?:$auth)?(?:$domain|$ip)/?(?:$trail)?";
@@ -137,9 +137,9 @@  discard block
 block discarded – undo
137 137
           // function contained in $task.
138 138
           $chunks[$i] = preg_replace_callback(
139 139
             $pattern,
140
-            function ($match) {
140
+            function($match) {
141 141
               if (\Drupal::service('url_embed')->getEmbed(Html::decodeEntities($match[1]))) {
142
-                return '<drupal-url data-embed-url="' . $match[1] . '"></drupal-url>';
142
+                return '<drupal-url data-embed-url="'.$match[1].'"></drupal-url>';
143 143
               }
144 144
               else {
145 145
                 return $match[1];
Please login to merge, or discard this patch.
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -140,8 +140,7 @@  discard block
 block discarded – undo
140 140
             function ($match) {
141 141
               if (\Drupal::service('url_embed')->getEmbed(Html::decodeEntities($match[1]))) {
142 142
                 return '<drupal-url data-embed-url="' . $match[1] . '"></drupal-url>';
143
-              }
144
-              else {
143
+              } else {
145 144
                 return $match[1];
146 145
               }
147 146
             },
@@ -150,8 +149,7 @@  discard block
 block discarded – undo
150 149
         }
151 150
         // Text chunk is done, so next chunk must be a tag.
152 151
         $chunk_type = 'tag';
153
-      }
154
-      else {
152
+      } else {
155 153
         // Only process this tag if there are no unclosed $ignore_tags.
156 154
         if ($open_tag == '') {
157 155
           // Check whether this tag is contained in $ignore_tags.
@@ -182,8 +180,7 @@  discard block
 block discarded – undo
182 180
   public function tips($long = FALSE) {
183 181
     if ($long) {
184 182
       return $this->t('<p>You can convert plain URLs to &lt;drupal-url&gt; HTML elements. Those elements are later converted to embeds using "Display embedded URLs" text filter.</p>');
185
-    }
186
-    else {
183
+    } else {
187 184
       return $this->t('You can convert plain URLs to embed elements.');
188 185
     }
189 186
   }
Please login to merge, or discard this patch.
src/Tests/ConvertUrlToEmbedFilterTest.php 1 patch
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
     $settings['title'] = 'Test convert url to embed with sample Twitter url';
81 81
     $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
82 82
     $node = $this->drupalCreateNode($settings);
83
-    $this->drupalGet('node/' . $node->id());
83
+    $this->drupalGet('node/'.$node->id());
84 84
     $this->assertRaw('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
85 85
     $this->assertNoText(strip_tags($content), 'URL does not appear in the output when embed is successful.');
86 86
 
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
     $settings['title'] = 'Test convert url to embed with non valid URL';
91 91
     $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
92 92
     $node = $this->drupalCreateNode($settings);
93
-    $this->drupalGet('node/' . $node->id());
93
+    $this->drupalGet('node/'.$node->id());
94 94
     $this->assertRaw($content);
95 95
 
96 96
     /** @var \Drupal\filter\FilterFormatInterface $format */
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
     $settings['title'] = 'Test convert url to embed with sample Twitter url and no prefix';
107 107
     $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
108 108
     $node = $this->drupalCreateNode($settings);
109
-    $this->drupalGet('node/' . $node->id());
109
+    $this->drupalGet('node/'.$node->id());
110 110
     $this->assertRaw(strip_tags($content));
111 111
     $this->assertNoRaw('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
112 112
 
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
     $settings['title'] = 'Test convert url to embed with sample Twitter url with the prefix';
117 117
     $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
118 118
     $node = $this->drupalCreateNode($settings);
119
-    $this->drupalGet('node/' . $node->id());
119
+    $this->drupalGet('node/'.$node->id());
120 120
     $this->assertRaw('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
121 121
     $this->assertNoText(strip_tags($content), 'URL does not appear in the output when embed is successful.');
122 122
 
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
     $settings['title'] = 'Test convert url to embed with sample Twitter url with wrong prefix';
127 127
     $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
128 128
     $node = $this->drupalCreateNode($settings);
129
-    $this->drupalGet('node/' . $node->id());
129
+    $this->drupalGet('node/'.$node->id());
130 130
     $this->assertRaw(strip_tags($content));
131 131
     $this->assertNoRaw('<drupal-url data-embed-url="https://twitter.com/drupal/status/735873777683320832"></drupal-url>');
132 132
   }
Please login to merge, or discard this patch.