Bug 93583 - Select/insertHTML at element boundaries inserts content in the wrong place
Summary: Select/insertHTML at element boundaries inserts content in the wrong place
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac (Intel) OS X 10.8
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-08 23:12 PDT by David Gatwood
Modified: 2023-02-18 15:39 PST (History)
4 users (show)

See Also:


Attachments
Sample script that demonstrates the bug. (709 bytes, text/html)
2012-08-08 23:13 PDT, David Gatwood
no flags Details
Second test case that nukes the entire body of a node and replaces it (unsuccessfully) (828 bytes, text/html)
2012-08-08 23:26 PDT, David Gatwood
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Gatwood 2012-08-08 23:12:53 PDT
1.  Set document body to contentEditable.
2.  Create a range and add it to the window selection.
3.  range.setStartAfter(document.body.lastChild);
4.  document.execCommand("insertHTML", false, "content to insert")

Notice that the content gets inserted *inside* the last element even though the insertion point is explicity *after* the last element.  This can't be correct behavior.  Firefox behaves as I would expect (inserting the data outside the last element).

Example attached.
Comment 1 David Gatwood 2012-08-08 23:13:57 PDT
Created attachment 157395 [details]
Sample script that demonstrates the bug.
Comment 2 David Gatwood 2012-08-08 23:25:32 PDT
It also fails if you try to replace the entire contents of a node in lots of spectacular ways.  For example, the second script I'm attaching, inserthtmlWholeBody.html, makes a copy of document.body.innerHTML, then uses execCommand's insertHTML to replace the entire body with its former contents.  This works correctly in Firefox (the body's contents are identical after the operation).  However, in Safari/WebKit, you get nodes nested inside other nodes that should not exist.
Comment 3 David Gatwood 2012-08-08 23:26:28 PDT
Created attachment 157397 [details]
Second test case that nukes the entire body of a node and replaces it (unsuccessfully)
Comment 4 Ahmad Saleem 2022-05-29 07:12:20 PDT
Attached test cases produces "fail" results for Safari 15.4 and Chrome Canary 104 but it shows as "pass" for Firefox Nightly 102.
Comment 5 Ryosuke Niwa 2022-05-31 13:36:37 PDT
This is the expected behavior as of now since we canonicalize positions before applying editing operations.
Comment 6 Ahmad Saleem 2022-05-31 14:36:21 PDT
If it is aligned with spec, should we close this? or is something needed here? Thanks!
Comment 7 David Gatwood 2022-05-31 16:52:47 PDT
If it is aligned with the spec, then the spec is badly broken.  The current behavior makes a lot of useful editing behavior impossible, and that's not good.

For background on this, I was trying to construct some semi-WYSISYG structured XML editors (for writing novels and screenplays) using WebKit, and I just plain gave up because I couldn't insert nodes inside empty elements, I couldn't reliably insert nodes between elements, I couldn't put the insertion cursor between elements even with element boundaries visible, etc.

If element boundaries are visible (with ::before and ::after pseudo-elements), then all of these locations (between elements, inside elements, etc.) are visually distinct locations in the document, and should absolutely not be canonicalized into a single position.

And the same is true for buttons.  No reasonable person would expect inserting content after a button to insert it into the button.  The inside of a button is visually distinct from the outside of a button.

So the current canonicalization behavior makes no sense.  It may be expected based on the way things are written right now, but it certainly isn't expected.  :-)
Comment 8 Ryosuke Niwa 2022-05-31 18:59:25 PDT
(In reply to Ahmad Saleem from comment #6)
> If it is aligned with spec, should we close this? or is something needed
> here? Thanks!

There isn't really a spec to speak of here. Fixing this bug most likely require https://bugs.webkit.org/show_bug.cgi?id=220514 to be resolved first.
Comment 9 Ahmad Saleem 2023-02-18 15:39:56 PST
This one still fails with Live Range Selection API on WebKit ToT (260520@main). Changing to ‘New’.