Skip to content

Commit be73a8f

Browse files
authored
Merge pull request #20 from iMattPro/iframe-updates
Display iframe original url/link when deferred
2 parents 71dd091 + f9b4fed commit be73a8f

7 files changed

Lines changed: 75 additions & 7 deletions

File tree

DOCUMENTATION.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Twig example:
576576
```twig
577577
{% if S_CONSENTMANAGER_MEDIA_ENABLED %}
578578
<span data-consent-media-container="1" data-consent-category="media">
579-
<span data-consent-media-placeholder="1"></span>
579+
<span data-consent-media-placeholder="1" data-consent-link="https://media.example.com/watch/123"></span>
580580
<span data-consent-media-content="1" hidden="hidden">
581581
<iframe
582582
data-consent-media-frame="1"
@@ -602,6 +602,7 @@ How it works:
602602
- `data-consent-media-container="1"` marks the deferred embed block
603603
- `data-consent-category="media"` ties the block to the embedded media consent category
604604
- `data-consent-media-placeholder="1"` marks the blocked placeholder content
605+
- `data-consent-link` optionally stores the original user-facing URL for the placeholder link
605606
- `data-consent-media-content="1"` wraps the real media markup
606607
- `data-consent-media-frame="1"` marks iframe nodes that should be activated after consent
607608
- `data-consent-src` stores the real iframe URL until Consent Manager moves it back to `src`

service/media_manager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ protected function wrap_media_root(\DOMDocument $dom, \DOMElement $media_root)
245245

246246
$placeholder = $dom->createElement('span');
247247
$placeholder->setAttribute('data-consent-media-placeholder', '1');
248+
$link_attribute = $dom->createElementNS(self::XSL_NAMESPACE, 'xsl:attribute');
249+
$link_attribute->setAttribute('name', 'data-consent-link');
250+
$link_value = $dom->createElementNS(self::XSL_NAMESPACE, 'xsl:value-of');
251+
$link_value->setAttribute('select', '.');
252+
$link_attribute->appendChild($link_value);
253+
$placeholder->appendChild($link_attribute);
248254

249255
$media_root->setAttribute('data-consent-media-content', '1');
250256
$media_root->setAttribute('hidden', 'hidden');

styles/all/template/js/consentmanager.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,34 @@
703703
}
704704
}
705705

706+
function renderMediaPlaceholder(placeholder)
707+
{
708+
if (!placeholder)
709+
{
710+
return;
711+
}
712+
713+
const source = placeholder.getAttribute('data-consent-link');
714+
placeholder.textContent = mediaPlaceholderLabel || '';
715+
716+
if (!source || !isSafeEmbedSource(source))
717+
{
718+
return;
719+
}
720+
721+
if (mediaPlaceholderLabel)
722+
{
723+
placeholder.appendChild(document.createTextNode(' '));
724+
}
725+
726+
const link = document.createElement('a');
727+
link.href = source;
728+
link.target = '_blank';
729+
link.rel = 'noopener noreferrer';
730+
link.textContent = source;
731+
placeholder.appendChild(link);
732+
}
733+
706734
function processDeferredEmbeds(scope)
707735
{
708736
const nodes = collectMatchingNodes(scope, deferredEmbedSelector);
@@ -715,10 +743,7 @@
715743
const placeholder = container.querySelector('[data-consent-media-placeholder]');
716744
const frames = container.querySelectorAll('[data-consent-media-frame]');
717745

718-
if (mediaPlaceholderLabel && placeholder)
719-
{
720-
placeholder.textContent = mediaPlaceholderLabel;
721-
}
746+
renderMediaPlaceholder(placeholder);
722747

723748
if (!content || !frames.length)
724749
{
@@ -1152,7 +1177,6 @@
11521177
{
11531178
document.addEventListener('DOMContentLoaded', function() {
11541179
processRegisteredScripts();
1155-
processDeferredEmbeds(document);
11561180
initUi();
11571181
});
11581182
}

styles/all/theme/consentmanager.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@
228228
align-items: flex-start;
229229
padding: 1rem;
230230
gap: 0.75rem;
231+
overflow-wrap: anywhere;
232+
}
233+
234+
[data-consent-media-placeholder] a {
235+
word-break: break-word;
236+
max-width: 100%;
237+
overflow-wrap: anywhere;
231238
}
232239

233240
[data-consent-media-placeholder][hidden],

tests/functional/iframe_embed_test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public function test_custom_iframe_bbcodes_are_deferred_until_media_consent_is_g
3232
$post_selector = '#post_content' . $post['topic_id'];
3333

3434
self::assertCount(1, $crawler->filter($post_selector . ' [data-consent-media-container="1"]'));
35-
self::assertCount(1, $crawler->filter($post_selector . ' [data-consent-media-placeholder="1"]'));
35+
$placeholder = $crawler->filter($post_selector . ' [data-consent-media-placeholder="1"]');
36+
self::assertCount(1, $placeholder);
37+
self::assertSame('https://video.example.com/embed/123', $placeholder->attr('data-consent-link'));
3638
self::assertCount(1, $crawler->filter($post_selector . ' [data-consent-media-content="1"][hidden="hidden"]'));
3739

3840
$iframe = $crawler->filter($post_selector . ' iframe[data-consent-media-frame="1"]');

tests/javascript/consentmanager.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,31 @@ test('saving newly granted media consent activates blocked embeds immediately',
373373
expect(frame.hasAttribute('data-consent-src')).toBe(false);
374374
});
375375

376+
test('renders deferred media placeholder link from original posted URL once before DOMContentLoaded', () => {
377+
const { document } = setupConsentManager({
378+
readyState: 'loading',
379+
extraMarkup: `
380+
<span data-consent-media-container="1" data-consent-category="media">
381+
<span data-consent-media-placeholder="1" data-consent-link="https://video.example.com/watch?v=123"></span>
382+
<span data-consent-media-content="1" hidden="hidden">
383+
<iframe
384+
data-consent-media-frame="1"
385+
data-consent-src="https://media.example.com/embed/123"
386+
></iframe>
387+
</span>
388+
</span>
389+
`
390+
});
391+
392+
const placeholder = document.querySelector('[data-consent-media-placeholder="1"]');
393+
394+
expect(placeholder.textContent).toBe('This content is blocked until you allow embedded media in the Privacy Settings. https://video.example.com/watch?v=123');
395+
expect(placeholder.querySelectorAll('a')).toHaveLength(1);
396+
expect(placeholder.querySelector('a').getAttribute('href')).toBe('https://video.example.com/watch?v=123');
397+
expect(placeholder.querySelector('a').getAttribute('target')).toBe('_blank');
398+
expect(placeholder.querySelector('a').getAttribute('rel')).toBe('noopener noreferrer');
399+
});
400+
376401
test('activates deferred embeds when the media content element is the iframe itself', () => {
377402
const { document } = setupConsentManager({
378403
localState: createState([ 'necessary', 'media' ], '2026-04-28T00:00:00.000Z'),

tests/service/media_manager_test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function test_configure_iframe_embeds_rewrites_xsl_generated_iframe_attri
9393
self::assertStringContainsString('data-consent-src="https://video.example.com/embed/123"', $template);
9494
self::assertStringContainsString('data-consent-onload="boot()"', $template);
9595
self::assertStringContainsString('data-consent-media-placeholder="1"', $template);
96+
self::assertStringContainsString('data-consent-link="{.}"', $template);
9697
self::assertStringContainsString('data-consent-media-frame="1"', $template);
9798
self::assertStringNotContainsString('$L_CONSENTMANAGER_MEDIA_PLACEHOLDER', $template);
9899
self::assertStringNotContainsString('data-consent-open-settings="1"', $template);
@@ -116,6 +117,7 @@ public function test_configure_iframe_embeds_rewrites_mediaembed_iframes()
116117
self::assertStringContainsString('name="data-consent-media-container"', $template);
117118
self::assertStringContainsString('name="data-consent-src"', $template);
118119
self::assertStringContainsString('data-consent-media-placeholder="1"', $template);
120+
self::assertStringContainsString('data-consent-link="{.}"', $template);
119121
self::assertStringContainsString('data-consent-media-frame="1"', $template);
120122
self::assertStringNotContainsString('$L_CONSENTMANAGER_MEDIA_PLACEHOLDER', $template);
121123
self::assertStringNotContainsString('data-consent-open-settings="1"', $template);
@@ -139,6 +141,7 @@ public function test_configure_iframe_embeds_rewrites_custom_s9e_iframes()
139141
self::assertStringContainsString('data-consent-media-container="1"', $template);
140142
self::assertStringContainsString('data-consent-src="https://video.example.com/embed/123"', $template);
141143
self::assertStringContainsString('data-consent-media-placeholder="1"', $template);
144+
self::assertStringContainsString('data-consent-link="{.}"', $template);
142145
self::assertStringContainsString('data-consent-media-frame="1"', $template);
143146
self::assertStringContainsString('class="custom-embed"', $template);
144147
self::assertStringContainsString('data-consent-media-content="1"', $template);

0 commit comments

Comments
 (0)