Skip to content

Refactor: extract shared DOM/HTML helpers into DomHelpersTrait (slice 2)#245

Merged
chubes4 merged 1 commit into
trunkfrom
refactor/htmltransformer-dom-helpers
Jun 27, 2026
Merged

Refactor: extract shared DOM/HTML helpers into DomHelpersTrait (slice 2)#245
chubes4 merged 1 commit into
trunkfrom
refactor/htmltransformer-dom-helpers

Conversation

@chubes4

@chubes4 chubes4 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

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.php into a new reusable trait src/HtmlToBlocks/Support/DomHelpersTrait.php, which HtmlTransformer now uses. 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 dragging HtmlTransformer along.

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). New DomHelpersTrait.php: 269 lines.

Verification — behavior-preserving, zero output change

Baseline (before) and after are identical:

  • composer test:canonical — all contracts passed (before & after)
  • composer parity128 → 128 fixtures passed (before & after)
  • full composer test green; php -l clean on both files

No fixtures were edited.

🤖 Generated with Claude Code

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>
@chubes4 chubes4 merged commit ce5bf15 into trunk Jun 27, 2026
1 check passed
@chubes4 chubes4 deleted the refactor/htmltransformer-dom-helpers branch June 27, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant