Skip to content

Editable content duplicated on blur #16993

@Stadly

Description

@Stadly

Describe the bug

Consider this rather contrived example. When I enter some text and blur the element, the input text is duplicated, first as plain text and then as HTML, though the content variable contains the right contents.

<script lang="ts">
  let content = $state("");
</script>

<div onblur={(e) => { content = e.currentTarget.innerText; }} contenteditable>
  {@html content}
</div>

The real use case I have is a simple markdown editor. content is a markdown string. The div contains {@html markdownToHTML(content)}, and on blur, I do content = HTMLToMarkdown(e.currentTarget.innerHTML).

I've found a workaround using key, but I think it seems strange that it should be necessary:

{#key content}
  <div onblur={(e) => { content = e.currentTarget.innerText; }} contenteditable>
    {@html content}
  </div>
{/key}

Reproduction

https://svelte.dev/playground/ea78f3db19fa4fd2bdae97a908d0ddbf?version=5.41.1

Logs

System Info

Same as latest Svelte playground

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions