diff --git a/packages/comark/src/internal/parse/tree.ts b/packages/comark/src/internal/parse/tree.ts index 06c813fd..53b1ffd1 100644 --- a/packages/comark/src/internal/parse/tree.ts +++ b/packages/comark/src/internal/parse/tree.ts @@ -243,10 +243,20 @@ function processChildren( return { children, nextIndex: i } } -function processPossibleAttributesSyntax(tokens: Token[], value: { nextIndex: number; node: Node }) { - const extractedAttributes = extractAttributes(tokens, value.nextIndex) +function processPossibleAttributesSyntax( + tokens: Token[], + value: { nextIndex: number; node: Node }, + skipEmptyText: boolean = true +) { + const extractedAttributes = extractAttributes(tokens, value.nextIndex, skipEmptyText) if (value.nextIndex < extractedAttributes.nextIndex) { - ;(value.node as ElementNode)[1] = Object.assign(value.node[1], extractedAttributes.attrs) + const attrs = extractedAttributes.attrs + const current = value.node[1] as ElementNodeAttributes + // `{.x}` adds to a class the element already has, like `attrJoin` in the markdown-it adapter. + if (typeof current.class === 'string' && typeof attrs.class === 'string') { + attrs.class = `${current.class} ${attrs.class}` + } + ;(value.node as ElementNode)[1] = Object.assign(current, attrs) value.nextIndex = extractedAttributes.nextIndex } @@ -547,9 +557,9 @@ const processors: Record = { }, mdc_inline_component(tokens, start) { const token = tokens[start] - const tokenAttrs = processAttributes(token.attrs) - const { attrs, nextIndex } = extractAttributes(tokens, start + 1, false) - return { node: [token.tag, { ...tokenAttrs, ...attrs }] as Node, nextIndex } + // Copy own properties so a `__proto__` attribute from a plugin token cannot set the prototype. + const node = [token.tag, { ...processAttributes(token.attrs) }] as Node + return processPossibleAttributesSyntax(tokens, { node, nextIndex: start + 1 }, false) }, image(tokens, start) { diff --git a/packages/comark/test/misc.test.ts b/packages/comark/test/misc.test.ts index 0aab0203..4edf8e91 100644 --- a/packages/comark/test/misc.test.ts +++ b/packages/comark/test/misc.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from 'vitest' -import { parseMarkdown } from '../src/index' +import type { MarkdownItPlugin } from '../src/types' +import { defineComarkPlugin, parseMarkdown } from '../src/index' describe('misc', () => { it('ignore props with invalid chars, only /^[a-z0-9_-:]+$/gi', async () => { @@ -44,6 +45,44 @@ describe('misc', () => { ]) }) + it('adds `{.x}` to a class the element already has (#568)', async () => { + // A plugin that turns `++text++` into `` and `%%` into ``. + const markdownItKbd: MarkdownItPlugin = (md) => { + md.inline.ruler.before('emphasis', 'kbd', (state: any, silent: boolean) => { + if (!state.src.startsWith('++', state.pos)) return false + const end = state.src.indexOf('++', state.pos + 2) + if (end < 0) return false + if (!silent) { + state.push('mdc_inline_span_open', 'kbd', 1).attrs = [['class', 'key']] + state.push('text', '', 0).content = state.src.slice(state.pos + 2, end) + state.push('mdc_inline_span_close', 'kbd', -1) + } + state.pos = end + 2 + return true + }) + md.inline.ruler.before('emphasis', 'icon', (state: any, silent: boolean) => { + if (!state.src.startsWith('%%', state.pos)) return false + if (!silent) state.push('mdc_inline_component', 'icon', 0).attrs = [['class', 'icon']] + state.pos += 2 + return true + }) + } + const kbd = defineComarkPlugin(() => ({ name: 'kbd', markdownItPlugins: [markdownItKbd] })) + + const doc = await parseMarkdown('++a++{.big .wide #k} ++b++{id="x"} %%{.big}', { plugins: [kbd()] }) + expect(doc.nodes).toEqual([ + [ + 'p', + {}, + ['kbd', { class: 'key big wide', id: 'k' }, 'a'], + ' ', + ['kbd', { class: 'key', id: 'x' }, 'b'], + ' ', + ['icon', { class: 'icon big' }], + ], + ]) + }) + describe('boolean attribute coercion', () => { it('promotes bare, explicit-true, and explicit-false attrs to :bindings', async () => { const tree = await parseMarkdown('::x{flag other="true" off="false" count="5"}\n::')