@@ -116,10 +116,7 @@ public function beforeLoad($html)
116116 * @return string Replacement.
117117 */
118118 $ replaceCallback = function ($ tagMatches ) {
119-
120- // Strip the self-closing slash as long as it is not an attribute value, like for the href attribute.
121- $ oldAttrs = rtrim (preg_replace ('#(?<!=)/$# ' , '' , $ tagMatches ['attrs ' ]));
122-
119+ $ oldAttrs = $ this ->maybeStripSelfClosingSlash ($ tagMatches ['attrs ' ]);
123120 $ newAttrs = '' ;
124121 $ offset = 0 ;
125122 while (
@@ -151,22 +148,17 @@ public function beforeLoad($html)
151148 return '< ' . $ tagMatches ['name ' ] . $ newAttrs . '> ' ;
152149 };
153150
154- $ converted = preg_replace_callback (
151+ $ result = preg_replace_callback (
155152 self ::AMP_BIND_SQUARE_START_PATTERN ,
156153 $ replaceCallback ,
157154 $ html
158155 );
159156
160- /*
161- * If the regex engine incurred an error during processing, for example exceeding the backtrack
162- * limit, $converted will be null. In this case we return the originally passed document to allow
163- * DOMDocument to attempt to load it. If the AMP HTML doesn't make use of amp-bind or similar
164- * attributes, then everything should still work.
165- *
166- * See https://github.com/ampproject/amp-wp/issues/993 for additional context on this issue.
167- * See http://php.net/manual/en/pcre.constants.php for additional info on PCRE errors.
168- */
169- return (null !== $ converted ) ? $ converted : $ html ;
157+ if (! is_string ($ result )) {
158+ return $ html ;
159+ }
160+
161+ return $ result ;
170162 }
171163
172164 /**
@@ -205,10 +197,7 @@ public function afterSave($html)
205197 * @return string Replacement.
206198 */
207199 $ replaceCallback = function ($ tagMatches ) {
208-
209- // Strip the self-closing slash as long as it is not an attribute value, like for the href attribute.
210- $ oldAttrs = rtrim (preg_replace ('#(?<!=)/$# ' , '' , $ tagMatches ['attrs ' ]));
211-
200+ $ oldAttrs = $ this ->maybeStripSelfClosingSlash ($ tagMatches ['attrs ' ]);
212201 $ newAttrs = '' ;
213202 $ offset = 0 ;
214203 while (
@@ -241,21 +230,33 @@ public function afterSave($html)
241230 return '< ' . $ tagMatches ['name ' ] . $ newAttrs . '> ' ;
242231 };
243232
244- $ converted = preg_replace_callback (
233+ $ result = preg_replace_callback (
245234 self ::AMP_BIND_DATA_START_PATTERN ,
246235 $ replaceCallback ,
247236 $ html
248237 );
249238
250- /*
251- * If the regex engine incurred an error during processing, for example exceeding the backtrack
252- * limit, $converted will be null. In this case we return the originally passed document to allow
253- * DOMDocument to attempt to load it. If the AMP HTML doesn't make use of amp-bind or similar
254- * attributes, then everything should still work.
255- *
256- * See https://github.com/ampproject/amp-wp/issues/993 for additional context on this issue.
257- * See http://php.net/manual/en/pcre.constants.php for additional info on PCRE errors.
258- */
259- return (null !== $ converted ) ? $ converted : $ html ;
239+ if (! is_string ($ result )) {
240+ return $ html ;
241+ }
242+
243+ return $ result ;
244+ }
245+
246+ /**
247+ * Strip the self-closing slash as long as it is not an attribute value, like for the href attribute.
248+ *
249+ * @param string $attributes Attributes to strip the self-closing slash of.
250+ * @return string Adapted attributes.
251+ */
252+ private function maybeStripSelfClosingSlash ($ attributes )
253+ {
254+ $ result = preg_replace ('#(?<!=)/$# ' , '' , $ attributes );
255+
256+ if (! is_string ($ result )) {
257+ return rtrim ($ attributes );
258+ }
259+
260+ return rtrim ($ result );
260261 }
261262}
0 commit comments