Skip to content

Spec: Empty addressable content fields with placeholders #528

@michael

Description

@michael

Here's a proposal for an official recommendation (spec?) to create "empty addressable content fields with placeholders" inside a contenteditable.

Recommended markup for making an element addressable with the caret:

<div contenteditable="true">
  <p><br></p>
</div>

Why?

  • the <br> element ensures there's a single addressable caret position. If you added a zero-width-space instead, or any other character, you'd end up with two addressable caret positions.
  • most editors are already handling it this way (e.g. ProseMirror <p><br class="ProseMirror-trailingBreak"></p>

To add a placeholder, use an empty class and a placeholder attribute and CSS to display it:

<div contenteditable="true">
  <p class="empty" placeholder="Enter text"><br></p>
</div>
[contenteditable="true"] [placeholder].empty::before {
  color: #ccc;
  content: attr(placeholder);
}

Considerations:

  • In my opinion <div></div> without the <br> should not be addressable with the caret.
    • When developing editors very often those leaf-nodes become addressable accidentally. You'd have to write <div contenteditable="false"></div> to avoid that.
    • Already some browsers skip caret positions here, with keyboard movement. See Firefox: Empty fields with placeholder are not keyboard selectable #500
    • however, this may break some existing apps, that rely on <div></div> being addressable (though that doesn't work in all browsers)

Known issues (only related to placeholder rendering/behavior):

Metadata

Metadata

Assignees

No one assigned

    Labels

    Agenda+Agenda item to be inserted in the Editing TF meeting queuecaret-movement

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions