Skip to content

Commit

Permalink
Fix to remove non-terminated character references
Browse files Browse the repository at this point in the history
Related-to: remarkjs/remark#913.
  • Loading branch information
wooorm committed Oct 24, 2022
1 parent 5bd2e1e commit 857caa4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {PhrasingContent, BlockContent} from 'mdast'

type DirectiveAttributes = Record<string, string>

/* eslint-disable @typescript-eslint/consistent-type-definitions */

interface DirectiveFields {
name: string
attributes?: DirectiveAttributes
Expand Down Expand Up @@ -33,3 +35,5 @@ declare module 'mdast' {
leafDirective: LeafDirective
}
}

/* eslint-enable @typescript-eslint/consistent-type-definitions */
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,35 @@ function exitAttributeIdValue(token) {
const list = /** @type {Array.<[string, string]>} */ (
this.getData('directiveAttributes')
)
list.push(['id', parseEntities(this.sliceSerialize(token))])
list.push([
'id',
parseEntities(this.sliceSerialize(token), {
attribute: true
})
])
}

/** @type {FromMarkdownHandle} */
function exitAttributeClassValue(token) {
const list = /** @type {Array.<[string, string]>} */ (
this.getData('directiveAttributes')
)
list.push(['class', parseEntities(this.sliceSerialize(token))])
list.push([
'class',
parseEntities(this.sliceSerialize(token), {
attribute: true
})
])
}

/** @type {FromMarkdownHandle} */
function exitAttributeValue(token) {
const list = /** @type {Array.<[string, string]>} */ (
this.getData('directiveAttributes')
)
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token))
list[list.length - 1][1] = parseEntities(this.sliceSerialize(token), {
attribute: true
})
}

/** @type {FromMarkdownHandle} */
Expand Down
27 changes: 27 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,33 @@ test('markdown -> mdast', (t) => {
'should support attributes'
)

t.deepEqual(
removePosition(
fromMarkdown(':a{b=&param c="&param" d=\'&param\'}', {
extensions: [directive()],
mdastExtensions: [directiveFromMarkdown]
}),
true
),
{
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'textDirective',
name: 'a',
attributes: {b: '&param', c: '&param', d: '&param'},
children: []
}
]
}
]
},
'should not support non-terminated character references'
)

t.deepEqual(
removePosition(
fromMarkdown(':a{b\nc="d\ne"}', {
Expand Down

0 comments on commit 857caa4

Please sign in to comment.