Skip to content

Commit

Permalink
Merge pull request #312 from marp-team/workaround-for-vanished-auto-s…
Browse files Browse the repository at this point in the history
…caling-component

Apply workaround for vanished <marp-auto-scaling> component in Chrome 105+
  • Loading branch information
yhatt committed Aug 11, 2022
2 parents 771b77f + 5df1b49 commit d8db5fa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Apply workaround for vanished `<marp-auto-scaling>` in Chrome 105+ ([#312](https://github.com/marp-team/marp-core/pull/312))

## v3.3.0 - 2022-08-11

### Changed
Expand Down
15 changes: 15 additions & 0 deletions src/custom-elements/browser/marp-auto-scaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ export class MarpAutoScaling extends HTMLElement {
: undefined
}

// Workaround for the latest Chromium browser (>= 105?)
// TODO: Remove this workaround when the bug is fixed
if (this.svg) {
const { svg: connectedSvg } = this

// I don't know why but a nested SVG may require to flush the display style for rendering correctly
requestAnimationFrame(() => {
connectedSvg.style.display = 'inline'

requestAnimationFrame(() => {
connectedSvg.style.display = ''
})
})
}

this.container =
this.svg?.querySelector<HTMLSpanElement>(`span[${dataContainer}]`) ??
undefined
Expand Down
28 changes: 28 additions & 0 deletions test/custom-elements/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,33 @@ describe('The hydration script for custom elements', () => {
expect(overloaded.container.style.marginRight).toBe('0px')
expect(overloaded.container.style.marginLeft).toBe('auto')
})

describe('Rendering workaround for Chromium 105+', () => {
const waitNextRendering = () =>
new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))

it("flushes SVG's display style on mounted", async () => {
expect.hasAssertions()

browser.applyCustomElements()
document.body.innerHTML = '<marp-auto-scaling>test</marp-auto-scaling>'

const autoScaling = document.querySelector(
'marp-auto-scaling'
) as MarpAutoScaling
const svg = autoScaling.shadowRoot.querySelector('svg') as SVGElement

// Initially SVG's display style is not set
expect(svg.style.display).toBe('')

// At the next rendering frame, display style is set as `inline`
await waitNextRendering()
expect(svg.style.display).toBe('inline')

// After that, display style is reverted to empty string
await waitNextRendering()
expect(svg.style.display).toBe('')
})
})
})
})

0 comments on commit d8db5fa

Please sign in to comment.