Skip to content

Commit ebc3997

Browse files
authored
Fix runtime dependency parity data targets (#485)
1 parent fdefa83 commit ebc3997

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

php-transformer/src/ArtifactCompiler/RuntimeDependencyParityReport.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,20 @@ private function withRuntimeIslandTargets(array $targets, array $runtimeIslands)
674674
$targets['classes'][$class] = true;
675675
}
676676
}
677+
678+
$tag = is_string($island['tag'] ?? null) ? strtolower(trim($island['tag'])) : '';
679+
foreach ( $attributes as $name => $value ) {
680+
$attributeName = strtolower((string) $name);
681+
if ( ! str_starts_with($attributeName, 'data-') || ! preg_match('/^data-[a-z][a-z0-9_-]*$/', $attributeName) ) {
682+
continue;
683+
}
684+
685+
$attributeSelector = '[' . $attributeName . ']';
686+
$targets['selectors'][$attributeSelector] = true;
687+
if ( '' !== $tag ) {
688+
$targets['selectors'][$tag . $attributeSelector] = true;
689+
}
690+
}
677691
}
678692

679693
return $targets;
@@ -1175,7 +1189,7 @@ private function targetExists(array $dependency, array $targets): bool
11751189
private function scriptKind(string $path, string $script): string
11761190
{
11771191
$haystack = strtolower($path . "\n" . substr($script, 0, 2000));
1178-
if ( str_contains($haystack, 'netlify') || str_contains($haystack, 'rum') || str_contains($haystack, 'analytics') || str_contains($haystack, 'gtag') ) {
1192+
if ( preg_match('#(?:netlify|analytics|gtag|\brum\b|[-_./]rum[-_./])#', $haystack) ) {
11791193
return 'telemetry';
11801194
}
11811195

php-transformer/tests/contract/run.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,7 @@ public function match(DOMElement $element, PatternContext $context): ?array
17131713
$assert(true === ($expandedRuntimeDependencies['canvas.preview']['canvas_api'] ?? null), 'compound canvas selector records canvas API usage');
17141714
$assert(str_contains($expandedRuntimeMarkup, '<canvas class="preview" aria-label="Preview"></canvas>'), 'compound canvas selector preserves canvas markup');
17151715
$assert(str_contains($expandedRuntimeMarkup, 'data-tool'), 'data attribute runtime selector remains addressable in generated markup');
1716+
$assert(true === ($expandedRuntimeDependencies['[data-tool]']['source_present'] ?? null), 'data attribute runtime selector is recorded as present in source markup');
17161717
$assert(str_contains($expandedRuntimeMarkup, 'mounted-app'), 'app root receiving appended children remains addressable in generated markup');
17171718
$assert(array() === ($expandedRuntimeReport['findings'] ?? array()), 'expanded runtime target selectors do not emit missing-target findings');
17181719

@@ -1774,6 +1775,32 @@ public function match(DOMElement $element, PatternContext $context): ?array
17741775
'runtime dependency parity does not fail entry output for selectors absent from that entry source'
17751776
);
17761777

1778+
$sharedDrumScriptSite = $compiler->compile(
1779+
array(
1780+
'entrypoint' => 'index.html',
1781+
'files' => array(
1782+
'index.html' => '<main><h1>Drum machine</h1><script src="js/site.js"></script></main>',
1783+
'patterns.html' => '<main><button data-voice-demo="kick">Kick</button><button data-groove="classic">Classic</button><script src="js/site.js"></script></main>',
1784+
'js/site.js' => 'document.querySelectorAll("[data-groove], [data-voice-demo]"); document.querySelectorAll(".is-playing").forEach(function (button) { button.classList.remove("is-playing"); });',
1785+
),
1786+
)
1787+
)->toArray();
1788+
$sharedDrumScriptReport = $sharedDrumScriptSite['source_reports']['runtime_dependency_parity'] ?? array();
1789+
$sharedDrumDependencies = $sharedDrumScriptReport['dependencies'] ?? array();
1790+
$sharedDrumDependency = array_values(array_filter($sharedDrumDependencies, static fn (array $dependency): bool => '[data-voice-demo]' === ($dependency['selector'] ?? '')))[0] ?? null;
1791+
$assert(
1792+
null !== $sharedDrumDependency,
1793+
'runtime dependency parity records shared data-attribute selectors absent from the entry source'
1794+
);
1795+
$assert(
1796+
'first_party' === ($sharedDrumDependency['script_kind'] ?? ''),
1797+
'runtime dependency parity does not classify drum scripts as RUM telemetry'
1798+
);
1799+
$assert(
1800+
array() === array_values(array_filter($sharedDrumScriptReport['findings'] ?? array(), static fn (array $finding): bool => in_array($finding['selector'] ?? '', array('.is-playing', '[data-voice-demo]', '[data-groove]'), true))),
1801+
'runtime dependency parity does not fail entry output for shared drum script selectors absent from that entry source'
1802+
);
1803+
17771804
$hamburgerOverlaySite = $compiler->compile(
17781805
array(
17791806
'entrypoint' => 'index.html',

0 commit comments

Comments
 (0)