-
Notifications
You must be signed in to change notification settings - Fork 20
Conversation
wp-lazy-loading.php
Outdated
return $content; | ||
} | ||
|
||
return preg_replace_callback( | ||
'/<(' . implode( '|', $tags ) . ')(\s)[^>]+>/', | ||
'/<img(\s)[^>]+>/', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think we need to capture the space here. Doesn't make sense to reinsert it. We only need to check for its existence.
This is a tiny optimization, but optimization nevertheless :)
In nearly all cases capturing and reinserting the space char will make no difference at all. If the tag is formatted with line breaks between the attributes (cannot happen in post_content, comments, widget text, and pretty much all content entered by the users as it breaks wpautop), capturing and reinserting the space will "push" the attribute to the second line, but still "break" the formatting as there will be two attributes there.
In any case, "white space" is ignored in HTML, and adding another attribute will "shuffle" the existing attributes a bit. Don't see a good reason to make the regex a tiny bit slower in order to "shuffle" the attributes in one way and not the other :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO this should be '/<img\s[^>]+>/'
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine with me
wp-lazy-loading.php
Outdated
if ( null === $context ) { | ||
$context = current_filter(); | ||
} | ||
|
||
if ( wp_lazy_loading_enabled( 'img', $context ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking we should keep this. wp_lazy_loading_enabled()
requires context in order to be "most useful". Passing current_filter()
is quite helpful. For example it would allows adding of the attribute in post_content, but not adding it in comments, etc. This also would let plugins customize it further if/when they call wp_add_lazy_load_attributes()
directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, removing the $context = current_filter()
if no context is provided was unintentional - so I agree :)
return $content; | ||
} | ||
|
||
return preg_replace_callback( | ||
'/<(' . implode( '|', $tags ) . ')(\s)[^>]+>/', | ||
'/<img(\s)[^>]+>/', | ||
function( array $matches ) { | ||
if ( ! preg_match( '/\sloading\s*=/', $matches[0] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex here should look for \b
not \s
at the beginning. Same considerations as above, if the img tag was formatted with each attribute starting on a new line, there's a chance that loading
won't have a white-space char before it.
Actually, nevermind :) Both \b
and \s
should work, "word boundary" perhaps being a bit more intuitive.
Add the suggested changes so this branch can be used as base for #10.
This PR removes support for
iframe
tags, since they are not part of the currently proposed spec for theloading
attribute (see #3 and #5).