Skip to content

Fix support for empty attributes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2023
Merged
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
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function parseParameters(value) {

return value
.replace(
/\s+([-\w]+)(?:=(?:"((?:\\[\s\S]|[^"])+)"|'((?:\\[\s\S]|[^'])+)'|((?:\\[\s\S]|[^"'\s])+)))?/gi,
/\s+([-\w]+)(?:=(?:"((?:\\[\s\S]|[^"])*)"|'((?:\\[\s\S]|[^'])*)'|((?:\\[\s\S]|[^"'\s])+)))?/gi,
replacer
)
.replace(/\s+/g, '')
Expand All @@ -128,14 +128,15 @@ function parseParameters(value) {
// eslint-disable-next-line max-params
function replacer(_, $1, $2, $3, $4) {
/** @type {MarkerParameterValue} */
let value = $2 || $3 || $4 || ''

let value = $2 === undefined ? ($3 === undefined ? $4 : $3) : $2;
const number = Number(value)

if (value === 'true' || value === '') {
if (value === 'true' || value === undefined) {
value = true
} else if (value === 'false') {
value = false
} else if (!Number.isNaN(number)) {
} else if (value.trim() && !Number.isNaN(number)) {
value = number
}

Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,32 @@ test('commentMaker', () => {
null,
'marker stop for invalid parameters (#3)'
)

html = {type: 'html', value: '<!--foo bar="" -->'}

assert.deepEqual(
commentMarker(html),
{
name: 'foo',
attributes: 'bar=""',
parameters: {bar: ''},
node: html
},
'marker with empty string attribute'
)

html = {type: 'html', value: '<!--foo bar=" " -->'}

assert.deepEqual(
commentMarker(html),
{
name: 'foo',
attributes: 'bar=" "',
parameters: {bar: ' '},
node: html
},
'marker with whitespace attribute'
)
})

test('comment node', () => {
Expand Down