Skip to content

refactor(mjml-hero): simplify getChildContext, drop dead width parsing#3104

Open
danfitz36 wants to merge 1 commit into
mjmlio:masterfrom
danfitz36:refactor/mjml-hero-simplify-child-context
Open

refactor(mjml-hero): simplify getChildContext, drop dead width parsing#3104
danfitz36 wants to merge 1 commit into
mjmlio:masterfrom
danfitz36:refactor/mjml-hero-simplify-child-context

Conversation

@danfitz36
Copy link
Copy Markdown

Fixes #2441.

MjHero.getChildContext was converting the parent container width to a px string and then re-parsing it with widthParser to branch on the unit — but the if (unit === '%') branch can never fire, because the value is appended with 'px' on the line above. The whole sequence collapses to a single subtraction.

As @iRyusa noted on the issue, the parsing was a remnant of an earlier attempt to support a width attribute on mj-hero and is no longer needed.

Changes

  • packages/mjml-hero/src/index.js
    • Replace the round-trip parse + dead if (unit === '%') branch with a direct parseFloat(containerWidth) - paddingSize subtraction
    • Drop the now-unused widthParser import
    • Remove the stale // Refactor -- removePaddingFor(...) TODO comment
  • packages/mjml/test/hero-padding-inner-width.test.js (new)
    • Five parametrized cases asserting the inner outlook table width equals containerWidth − horizontalPadding for a range of padding shorthands. mj-hero previously had no direct test coverage.

Verification

  • Existing suite: 74 → 79 passing (the 5 new cases)
  • Output is byte-identical to the old implementation; the dead branch was provably dead
  • Lint clean

The child container width was being formatted to a px string and then
re-parsed via widthParser, only to branch on whether the unit was %.
That branch can never fire because the value is appended with 'px' on
the line above. The whole sequence collapses to a single subtraction
of the horizontal padding from the parent container width.

Also removes the now-unused widthParser import and a stale TODO
comment that referenced this code.

Adds a regression test covering the inner outlook table width for
several padding shorthands; mj-hero had no direct test coverage
before.

Fix mjmlio#2441.
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.

This code in mj-hero makes no sense.

1 participant