Refactor: extract shared DOM/HTML helpers into DomHelpersTrait (slice 2)#245
Merged
Conversation
Pure, behavior-preserving move of 22 broadly-shared low-level helpers out of HtmlTransformer into HtmlToBlocks\Support\DomHelpersTrait, which HtmlTransformer now `use`s. No logic, signature, or output changes — trait methods are inlined into the class so private state/method access is unchanged. Refs #242 (slice 2: shared DOM helpers). Unblocks future slices that depend on attr/elementSelector/safeFallbackHtml/boundedFallbackHtml/normalizedNavigationLabel and friends without dragging HtmlTransformer along. Parity 128->128, canonical contracts green, full `composer test` green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #242 — decomposition epic, slice 2: shared DOM helpers.
What
Pure, behavior-preserving extraction of 22 broadly-shared low-level helpers out of
src/HtmlToBlocks/HtmlTransformer.phpinto a new reusable traitsrc/HtmlToBlocks/Support/DomHelpersTrait.php, whichHtmlTransformernowuses. Slice 1 (#243) showed the big concerns (semantic-parity builders, behavior-loss emitters, style, nav-chrome, runtime islands) were blocked precisely because they depend on these shared utilities. This moves them to a shared home so future slices can extract those concerns without draggingHtmlTransformeralong.Why a trait
A
used trait is the lowest-risk, most byte-identical move: PHP inlines trait methods into the using class, so private property/method access ($this->runtime,$this->outerHtml(), etc.) is unchanged. Method bodies and signatures were moved verbatim — no logic changes.Helpers moved (22)
DOM traversal/structure:
innerHtml,innerHtmlPreservingWhitespace,outerHtml,innerHtmlWithoutTags,closestTagName,firstChildElement,onlyChildElement,childElementCount,directElementChildCount,ancestorTags.Attribute/selector/class access:
attr,htmlAttributes,elementSelector,hasClass,classNames.String/HTML utilities:
safeAnchor,mergeClassNames,htmlAttributeString,normalizedNavigationLabel,safeFallbackHtml,boundedFallbackHtml,boundedFallbackText.Concern-specific code was intentionally left in place for later slices.
Line-count delta
HtmlTransformer.php: 6723 → 6475 (−248). NewDomHelpersTrait.php: 269 lines.Verification — behavior-preserving, zero output change
Baseline (before) and after are identical:
composer test:canonical— all contracts passed (before & after)composer parity— 128 → 128 fixtures passed (before & after)composer testgreen;php -lclean on both filesNo fixtures were edited.
🤖 Generated with Claude Code