Skip to content

Commit af9182b

Browse files
committed
Add test coverage for setting preload link
1 parent 91c1d65 commit af9182b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="utf-8">
4+
<title>...</title>
5+
</head>
6+
<body>
7+
<div id="page">
8+
<img src="https://example.com/image.jpg" alt="" width="100" height="100">
9+
</div>
10+
</body>
11+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html lang="en">
2+
<head>
3+
<meta charset="utf-8">
4+
<title>...</title>
5+
<link data-od-added-tag rel="preload" as="image" href="https://example.com/image.jpg">
6+
</head>
7+
<body>
8+
<div id="page">
9+
<img data-od-xpath="/HTML/BODY/DIV/*[1][self::IMG]" src="https://example.com/image.jpg" alt="" width="100" height="100">
10+
</div>
11+
<script type="module">/* import detect ... */</script>
12+
</body>
13+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
return static function (): void {
3+
4+
add_action(
5+
'od_register_tag_visitors',
6+
static function ( OD_Tag_Visitor_Registry $registry ): void {
7+
$registry->register(
8+
'img-preload',
9+
static function ( OD_Tag_Visitor_Context $context ): bool {
10+
if ( 'IMG' === $context->processor->get_tag() ) {
11+
$context->link_collection->add_link(
12+
array(
13+
'rel' => 'preload',
14+
'as' => 'image',
15+
'href' => $context->processor->get_attribute( 'src' ),
16+
)
17+
);
18+
}
19+
return false;
20+
}
21+
);
22+
}
23+
);
24+
};

0 commit comments

Comments
 (0)