Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export function directiveToMarkdown(options) {
// Additionally, join shortcuts: `#a .b.c d="e"` -> `#a.b.c d="e"`
if (key === 'id') {
id =
settings.preferShortcut !== false && shortcut.test(value)
settings.preferShortcut !== false &&
shortcut.test(value) &&
encodeAmpersands(value) === value
? '#' + value
: quoted('id', value, node, state)
} else if (key === 'class') {
Expand All @@ -259,7 +261,9 @@ export function directiveToMarkdown(options) {
let index = -1

while (++index < list.length) {
;(settings.preferShortcut !== false && shortcut.test(list[index])
;(settings.preferShortcut !== false &&
shortcut.test(list[index]) &&
encodeAmpersands(list[index]) === list[index]
? classesList
: classesFullList
).push(list[index])
Expand Down Expand Up @@ -308,8 +312,10 @@ export function directiveToMarkdown(options) {
function quoted(key, value, node, state) {
if (settings.collapseEmptyAttributes !== false && !value) return key

if (settings.preferUnquoted && unquoted.test(value)) {
return key + '=' + value
const encoded = encodeAmpersands(value)

if (settings.preferUnquoted && unquoted.test(encoded)) {
return key + '=' + encoded
}

// If the alternative is less common than `quote`, switch.
Expand All @@ -330,7 +336,7 @@ export function directiveToMarkdown(options) {
key +
'=' +
appliedQuote +
stringifyEntitiesLight(value, {subset}) +
stringifyEntitiesLight(encoded, {subset}) +
appliedQuote
)
}
Expand Down Expand Up @@ -560,6 +566,36 @@ function inlineDirectiveLabel(node) {
)
}

/**
* Encode the ampersands that start character references in an attribute value,
* so that they are not decoded when parsed.
*
* @param {string} value
* Attribute value.
* @returns {string}
* Attribute value with those ampersands encoded.
*/
function encodeAmpersands(value) {
if (!value.includes('&')) return value

/** @type {Array<string>} */
const result = []
let start = 0

parseEntities(value, {
attribute: true,
reference(_, position) {
const {offset} = position.start
assert(offset !== undefined, 'expected `offset`')
result.push(value.slice(start, offset), '&#x26;')
start = offset + 1
}
})

result.push(value.slice(start))
return result.join('')
}

/**
* Determine the fence size for a directive node.
*
Expand Down
112 changes: 112 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,64 @@ test('directiveToMarkdown()', async function (t) {
}
)

await t.test(
'should encode the `&` of character references in attribute values',
async function () {
assert.equal(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {title: 'a &amp; b &#x26; c &copy d'},
children: []
},
{extensions: [directiveToMarkdown()]}
),
':i{title="a &#x26;amp; b &#x26;#x26; c &#x26;copy d"}\n'
)
}
)

await t.test(
'should not encode other `&` in attribute values',
async function () {
assert.equal(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {title: 'a & b&c &copy=d &unknown;'},
children: []
},
{extensions: [directiveToMarkdown()]}
),
':i{title="a & b&c &copy=d &unknown;"}\n'
)
}
)

await t.test(
'should roundtrip character references in attribute values',
async function () {
const tree = fromMarkdown(
'a :b{#c&amp;amp;d .e&amp;lt;f g="😀\r\n&amp;amp;&amp;nbsp;"} h.',
{
extensions: [directive()],
mdastExtensions: [directiveFromMarkdown()]
}
)
const result = fromMarkdown(
toMarkdown(tree, {extensions: [directiveToMarkdown()]}),
{extensions: [directive()], mdastExtensions: [directiveFromMarkdown()]}
)

removePosition(tree, {force: true})
removePosition(result, {force: true})

assert.deepEqual(result, tree)
}
)

await t.test(
'should not use the `id` shortcut if impossible characters exist',
async function () {
Expand Down Expand Up @@ -666,6 +724,42 @@ test('directiveToMarkdown()', async function (t) {
}
)

await t.test(
'should not use the `id` shortcut for character references',
async function () {
assert.equal(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {id: 'a&amp;b'},
children: []
},
{extensions: [directiveToMarkdown()]}
),
':i{id="a&#x26;amp;b"}\n'
)
}
)

await t.test(
'should not use the `class` shortcut for character references',
async function () {
assert.equal(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {class: 'a b&lt;c'},
children: []
},
{extensions: [directiveToMarkdown()]}
),
':i{.a class="b&#x26;lt;c"}\n'
)
}
)

await t.test(
'should not use the `class` shortcut if impossible characters exist (but should use it for classes that don’t)',
async function () {
Expand Down Expand Up @@ -1468,6 +1562,24 @@ test('directiveToMarkdown()', async function (t) {
}
)

await t.test(
'should encode the `&` of character references in `preferUnquoted`',
async function () {
assert.equal(
toMarkdown(
{
type: 'textDirective',
name: 'i',
attributes: {title: 'a&amp;b'},
children: []
},
{extensions: [directiveToMarkdown({preferUnquoted: true})]}
),
':i{title=a&#x26;amp;b}\n'
)
}
)

await t.test(
'should not add `=` when omitting quotes on empty values',
async function () {
Expand Down
Loading