Skip to content

Commit

Permalink
Hoist regex and entity subset
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Apr 3, 2024
1 parent c8d5f67 commit c998f7a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/handle/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

import {stringifyEntities} from 'stringify-entities'

const htmlCommentRegex = /^>|^->|<!--|-->|--!>|<!-$/g

// Declare arrays as variables so it can be cached by `stringifyEntities`
const bogusCommentEntitySubset = ['>']
const commentEntitySubset = ['<', '>']

/**
* Serialize a comment.
*
Expand All @@ -27,10 +33,12 @@ export function comment(node, _1, _2, state) {
? '<?' +
stringifyEntities(
node.value,
Object.assign({}, state.settings.characterReferences, {subset: ['>']})
Object.assign({}, state.settings.characterReferences, {
subset: bogusCommentEntitySubset
})
) +
'>'
: '<!--' + node.value.replace(/^>|^->|<!--|-->|--!>|<!-$/g, encode) + '-->'
: '<!--' + node.value.replace(htmlCommentRegex, encode) + '-->'

/**
* @param {string} $0
Expand All @@ -39,7 +47,7 @@ export function comment(node, _1, _2, state) {
return stringifyEntities(
$0,
Object.assign({}, state.settings.characterReferences, {
subset: ['<', '>']
subset: commentEntitySubset
})
)
}
Expand Down
5 changes: 4 additions & 1 deletion lib/handle/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

import {stringifyEntities} from 'stringify-entities'

// Declare array as variable so it can be cached by `stringifyEntities`
const textEntitySubset = ['<', '&']

/**
* Serialize a text node.
*
Expand All @@ -32,7 +35,7 @@ export function text(node, _, parent, state) {
: stringifyEntities(
node.value,
Object.assign({}, state.settings.characterReferences, {
subset: ['<', '&']
subset: textEntitySubset
})
)
}

0 comments on commit c998f7a

Please sign in to comment.