w3c / w3c/editing

Safari/Chrome Desktop: Caret movement with up/down arrow keys breaks when DOM is constructed programmatically

Open
#513 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

caret-movement
Dominant language
HTML
Stars
202
Forks
43
PR merge metrics
No merged PRs in 30d

Description

When the following markup is constructed programmatically...

<div class="editable" contenteditable="true">
	<h3 class="centered field empty" placeholder="Hello"></h3>
	<!---->
	<h3 class="centered field empty" placeholder="Hello"></h3>
	<!---->
	<h3 class="centered field empty" placeholder="Hello"></h3>
	<!---->
	<h3 class="centered field empty" placeholder="Hello"></h3>
	<!---->
</div>

with this code...

function create_text(value = "") {
	return document.createTextNode(value);
}
function comment() {
	var frag = document.createDocumentFragment();
	var start = document.createComment("");
	var anchor = create_text();
	frag.append(start, anchor);
	return frag;
}
function first_child(fragment) {
	return Object.getOwnPropertyDescriptor(
		Node.prototype,
		"firstChild",
	)?.get?.call(fragment);
}
function element(node, tag, render) {
	const el = document.createElement(tag);
	el.append(create_text());
	render(el);
	node.before(el);
}
function render(root) {
	for (let i = 0; i < 4; i++) {
		const f = comment();
		const n = first_child(f);
		element(n, "h3", (el) => {
			el.className = "centered field empty";
			el.setAttribute("placeholder", "Hello");
		});
		root.append(f);
	}
}
render(document.getElementById("root"));

... caret movement breaks.

See Repro: https://w3c.github.io/editing/repros/513.html

Also see this issue in Svelte for context: https://github.com/sveltejs/svelte/issues/17164

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Repro at w3c.github.io/editing/repros/513.html and run the supplied DOM-construction example in Safari and Chrome. The issue names no repository file or test; first identify where the caret movement behavior is specified or exercised, then establish the expected up/down-arrow behavior and a reproducible verification step.

Written by the indexing model from the issue text.

Assessment

Tech stack
html, javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.