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

Handle images already wrapped within a picture element #22

Open
dmnsgn opened this issue Jan 8, 2021 · 4 comments
Open

Handle images already wrapped within a picture element #22

dmnsgn opened this issue Jan 8, 2021 · 4 comments
Labels
question Further information is requested

Comments

@dmnsgn
Copy link
Contributor

dmnsgn commented Jan 8, 2021

Is there any way the plugin could detect the parent node of the image for a <picture> element and:

  • only wrap the <img> if it is not already in a picture
  • preserve any already present <source> as a sibling to the generated webp source

Current behaviour

Input:

<picture>
  <source type="image/avif" srcset="images/my-image.avif 1x, images/[email protected] 2x">
  <img srcset="images/my-image.jpg 1x, images/[email protected] 2x" alt="">
</picture>

Output:

<picture>
  <source type="image/avif" srcset="images/my-image.avif 1x, images/[email protected] 2x">
  <picture>
    <source type="image/webp" srcset="images/my-image.webp 1x, images/[email protected] 2x">
    <img srcset="images/my-image.jpg 1x, images/[email protected] 2x" alt="">
  </picture>
</picture>

Expected behaviour

<picture>
  <source type="image/avif" srcset="images/my-image.avif 1x, images/[email protected] 2x">
  <source type="image/webp" srcset="images/my-image.webp 1x, images/[email protected] 2x">
  <img srcset="images/my-image.jpg 1x, images/[email protected] 2x" alt="">
</picture>

Use case

This could also be useful when several different plugin are trying to add sources to a picture.

Thanks

@Scrum
Copy link
Member

Scrum commented Jan 11, 2021

To do this, initially you need to go through all <picture> and look at the presence in it <source>. For example, you can add any attribute to such parents that means that it already contains <source>.

I think it would be better to make it a separate plugin !!! since this is beyond the logic of this plugin

@Scrum Scrum added the question Further information is requested label Jan 11, 2021
@dmnsgn
Copy link
Contributor Author

dmnsgn commented Jan 17, 2021

I see, the logic is a bit different indeed.
Are there any documentation on parsing the html ast tree, parent and children nodes? Or is it just a cascade of tree.match ?

@Scrum
Copy link
Member

Scrum commented Jan 18, 2021

Or is it just a cascade of tree.match ?

yep

@ausir0726
Copy link

same problem here...
I have some img use pictrue tag for meida query
but plugin break it.

<picture>
  <source srcset="./images/index/cover-1-4k.jpg" media="(min-width: 2000px)">
  <source srcset="./images/index/photo-banner-mobile-1.jpg" media="(max-width: 767px)">
  <picture>
    <source type="image/webp" srcset="./images/index/cover-1-2k.jpg.webp">
    <img class="absolute left-0 top-0 w-full h-full object-cover object-center" src="./images/index/cover-1-2k.jpg">
  </picture>
</picture>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants