Skip to content

Commit

Permalink
Merge pull request #373 from marp-team/advanced-bg-a11y-reject-keywords
Browse files Browse the repository at this point in the history
Advanced background: Exclude Marpit reserved image keywords from `<figcaption>` correctly
  • Loading branch information
yhatt authored Sep 11, 2023
2 parents 16aaa4f + a27d688 commit b2afbf9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 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

- Advanced background: Exclude Marpit reserved image keywords from `<figcaption>` correctly ([#373](https://github.com/marp-team/marpit/pull/373))

## v2.5.2 - 2023-09-11

### Fixed
Expand Down
25 changes: 6 additions & 19 deletions src/markdown/background_image/advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ function _advancedBackground(md) {
style: style.toString(),
open: {
meta: {
// For getting better alt text, we should store
// the reference of the original image token.
marpitBackgroundSource: img.source,
marpitBackgroundAlt: img.alt,
},
},
},
Expand Down Expand Up @@ -168,27 +166,16 @@ function _advancedBackground(md) {
tokens,
idx,
options,
env,
_env,
self,
) => {
const token = tokens[idx]
const open = self.renderToken(tokens, idx, options)

if (token.meta && token.meta.marpitBackgroundSource) {
// Try to render figcaption for background image
// (Image token after parsed has text children without Marpit keywords)
const figcaption = self
.renderInlineAsText(
token.meta.marpitBackgroundSource.children,
options,
env,
)
.trim()

if (figcaption)
return `${open}<figcaption>${md.utils.escapeHtml(
figcaption,
)}</figcaption>`
if (token.meta?.marpitBackgroundAlt) {
return `${open}<figcaption>${md.utils.escapeHtml(
token.meta.marpitBackgroundAlt,
)}</figcaption>`
}

return open
Expand Down
8 changes: 7 additions & 1 deletion src/markdown/background_image/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function _backgroundImageApply(md) {
size,
url,
width,
options,
} = t.meta.marpitImage

if (background && !url.match(/^\s*$/)) {
Expand All @@ -85,6 +86,11 @@ function _backgroundImageApply(md) {
}
} else {
// Background image
let altText = ''

for (const opt of options)
if (!opt.consumed) altText += opt.leading + opt.content

current.images = [
...(current.images || []),
{
Expand All @@ -100,7 +106,7 @@ function _backgroundImageApply(md) {
})(),
url,
width,
source: t,
alt: altText.trimStart(),
},
]
}
Expand Down
2 changes: 1 addition & 1 deletion src/markdown/image/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function _parseImage(md) {

if (hasConsumed) {
let newTokens = []
md.inline.parse(updatedAlt.trimLeft(), state.md, state.env, newTokens)
md.inline.parse(updatedAlt.trimStart(), state.md, state.env, newTokens)

token.children = newTokens
}
Expand Down
1 change: 0 additions & 1 deletion test/markdown/directives/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ describe('Marpit directives apply plugin', () => {
`

const $ = load(mdWithSVG().render(paginateDirs))
console.log($.html())
const sections = $('section')

expect(sections.eq(0).data('marpit-pagination')).toBeUndefined()
Expand Down
21 changes: 21 additions & 0 deletions test/marpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,27 @@ describe('Marpit', () => {
}),
)
})

describe('Advanced background image powered by inline SVG mode', () => {
it('has figure element with background-image in the isolated layer', async () => {
const $ = load(
new Marpit({ inlineSVG: true }).render(
'![bg Advanced background](test)',
).html,
)

const figure = $('figure')
const ret = await postcssInstance.process(figure.attr('style'), {
from: undefined,
})

ret.root.walkDecls('background-image', (decl) => {
expect(decl.value).toBe('url("test")')
})

expect(figure.find('figcaption').html()).toBe('Advanced background')
})
})
})

describe('CSS Filters', () => {
Expand Down

0 comments on commit b2afbf9

Please sign in to comment.