Skip to content

Commit

Permalink
Add loading="lazy" only to images with width and height attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
filipsobol committed Dec 12, 2024
1 parent 8099d92 commit 35192d0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/ckeditor5-image/src/image/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type ImageUtils from '../imageutils.js';
*/
export function createInlineImageViewElement( writer: DowncastWriter ): ViewContainerElement {
return writer.createContainerElement( 'span', { class: 'image-inline' },
writer.createEmptyElement( 'img', { loading: 'lazy' } )
writer.createEmptyElement( 'img' )
);
}

Expand All @@ -51,7 +51,7 @@ export function createInlineImageViewElement( writer: DowncastWriter ): ViewCont
*/
export function createBlockImageViewElement( writer: DowncastWriter ): ViewContainerElement {
return writer.createContainerElement( 'figure', { class: 'image' }, [
writer.createEmptyElement( 'img', { loading: 'lazy' } ),
writer.createEmptyElement( 'img' ),
writer.createSlot( 'children' )
] );
}
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-image/src/imagesizeattributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ export default class ImageSizeAttributes extends Plugin {

if ( width && height ) {
viewWriter.setStyle( 'aspect-ratio', `${ width }/${ height }`, img );
viewWriter.setAttribute( 'loading', 'lazy', img );
}
} );
}
Expand Down
6 changes: 3 additions & 3 deletions tests/_data/data-sets/ghs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ const initialData =
<div class="columns" style="gap:10px">
<div class="column" style="border: 1px solid orange">
<figure class="image">
<img src="/ckeditor5/tests/manual/sample.jpg"/>
<img src="/ckeditor5/tests/manual/sample.jpg" width="826" height="388"/>
<figcaption><abbr title="HyperText Markup Language">HTML</abbr></figcaption>
</figure>
</div>
<div class="column" style="border: 1px dotted blue">
<figure class="image">
<img src="/ckeditor5/tests/manual/sample.jpg"/>
<img src="/ckeditor5/tests/manual/sample.jpg" width="826" height="388"/>
<figcaption><abbr title="Cascading Style Sheets">CSS</abbr></figcaption>
</figure>
</div>
<div class="column" style="border: 1px dashed olive">
<figure class="image">
<img src="/ckeditor5/tests/manual/sample.jpg"/>
<img src="/ckeditor5/tests/manual/sample.jpg" width="826" height="388"/>
<figcaption><abbr title="JavaScript">JS</abbr></figcaption>
</figure>
</div>
Expand Down

0 comments on commit 35192d0

Please sign in to comment.