Skip to content

Handle images already wrapped within a picture element #22

@dmnsgn

Description

@dmnsgn

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/my-image@2x.avif 2x">
  <img srcset="images/my-image.jpg 1x, images/my-image@2x.jpg 2x" alt="">
</picture>

Output:

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

Expected behaviour

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

Use case

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

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions