Skip to content

Commit

Permalink
(fix) sanitize leading invalid attribute chars (#863)
Browse files Browse the repository at this point in the history
They are valid for Svelte, but invalid for JSX
#862
  • Loading branch information
dummdidumm authored Mar 9, 2021
1 parent af8af83 commit e79ee80
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/svelte2tsx/src/htmlxtojsx/nodes/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ export function handleAttribute(htmlx: string, str: MagicString, attr: Node, par
}

const equals = htmlx.lastIndexOf('=', attrVal.start);

const sanitizedName = sanitizeLeadingChars(attr.name);
if (sanitizedName !== attr.name) {
str.overwrite(attr.start, equals, sanitizedName);
}

if (attrVal.type == 'Text') {
const endsWithQuote =
htmlx.lastIndexOf('"', attrVal.end) === attrVal.end - 1 ||
Expand Down Expand Up @@ -166,3 +172,16 @@ export function handleAttribute(htmlx: string, str: MagicString, attr: Node, par
str.appendLeft(attr.end, '`}');
}
}

function sanitizeLeadingChars(attrName: string): string {
let sanitizedName = '';
for (let i = 0; i < attrName.length; i++) {
if (/[A-Za-z$_]/.test(attrName[i])) {
sanitizedName += attrName.substr(i);
return sanitizedName;
} else {
sanitizedName += '_';
}
}
return sanitizedName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<><Hello __custom-prop="foo"></Hello>
<div __custom-prop="foo"></div></>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<Hello --custom-prop="foo"></Hello>
<div --custom-prop="foo"></div>

0 comments on commit e79ee80

Please sign in to comment.