Skip to content

Commit

Permalink
feat: support migration of self closing tags (#13479)
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti authored Oct 3, 2024
1 parent 073ef17 commit a51b3df
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-camels-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: support migration of self closing tags
10 changes: 10 additions & 0 deletions packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { extract_identifiers } from '../utils/ast.js';
import { migrate_svelte_ignore } from '../utils/extract_svelte_ignore.js';
import { determine_slot } from '../utils/slot.js';
import { validate_component_options } from '../validate-options.js';
import { is_svg, is_void } from '../../utils.js';

const regex_style_tags = /(<style[^>]+>)([\S\s]*?)(<\/style>)/g;
const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';
Expand Down Expand Up @@ -580,6 +581,15 @@ const template = {
},
RegularElement(node, { state, next }) {
handle_events(node, state);
// Strip off any namespace from the beginning of the node name.
const node_name = node.name.replace(/[a-zA-Z-]*:/g, '');

if (state.analysis.source[node.end - 2] === '/' && !is_void(node_name) && !is_svg(node_name)) {
let trimmed_position = node.end - 2;
while (state.str.original.charAt(trimmed_position - 1) === ' ') trimmed_position--;
state.str.remove(trimmed_position, node.end - 1);
state.str.appendRight(node.end, `</${node.name}>`);
}
next();
},
SvelteElement(node, { state, next }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div />
<div title="preserve" />
<input type="text" />
<hr />
<f:table />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div></div>
<div title="preserve"></div>
<input type="text" />
<hr />
<f:table></f:table>

0 comments on commit a51b3df

Please sign in to comment.