Bug 199182

Summary: Web Inspector: Elements: allow nodes to be copied and pasted
Product: WebKit Reporter: Devin Rousso <hi>
Component: Web InspectorAssignee: Devin Rousso <hi>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue, hi, inspector-bugzilla-changes, mattbaker, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: All   
OS: All   
Bug Depends on:    
Bug Blocks: 199396, 199397    
Attachments:
Description Flags
Patch
none
Patch none

Description Devin Rousso 2019-06-24 20:40:01 PDT
I would expect this to copy the HTML markup (copy) and create a new node (paste) right after the selected node with the copied markup.

This can be achieved right now by right-clicking on a node, selecting "Copy > HTML", right-clicking on the "destination" node, selecting "Add > Next Sibling", and pasting.
Comment 1 Devin Rousso 2019-06-24 23:59:04 PDT
Created attachment 372819 [details]
Patch
Comment 2 Matt Baker 2019-07-01 20:37:12 PDT
(In reply to Devin Rousso from comment #1)
> Created attachment 372819 [details]
> Patch

I never knew I wanted this until testing this patch. Repeatedly hitting paste to keep adding nodes is great.

Do we want to select the pasted node automatically? I was a little surprised after hitting paste and not seeing the selection change.
Comment 3 Matt Baker 2019-07-01 20:38:21 PDT
(In reply to Matt Baker from comment #2)
> (In reply to Devin Rousso from comment #1)
> > Created attachment 372819 [details]
> > Patch
> 
> I never knew I wanted this until testing this patch. Repeatedly hitting
> paste to keep adding nodes is great.
> 
> Do we want to select the pasted node automatically? I was a little surprised
> after hitting paste and not seeing the selection change.

Just checked, and Finder selects the newly pasted items in this case.
Comment 4 Matt Baker 2019-07-01 20:46:42 PDT
Comment on attachment 372819 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=372819&action=review

r-, because of the copy-paste error in WI._paste.

You can add this in a follow up if you want, but I noticed this doesn't work with comment nodes.

> Source/WebInspectorUI/UserInterface/Base/Main.js:2629
> +    if (selection.isCollapsed && !WI.isEventTargetAnEditableField(event)) {

Decrease indenting with early return:

if (!selection.isCollapsed || WI.isTargetAnEditableField(event))
    return;

> Source/WebInspectorUI/UserInterface/Base/Main.js:2631
> +        if (focusedCopyHandler && typeof focusedCopyHandler.handlePasteEvent === "function") {

Was this copied from WI._copy? The comment above looks wrong too.

> Source/WebInspectorUI/UserInterface/Base/Main.js:2649
> +        return;

Not needed.
Comment 5 Devin Rousso 2019-07-01 23:39:46 PDT
Comment on attachment 372819 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=372819&action=review

>> Source/WebInspectorUI/UserInterface/Base/Main.js:2629
>> +    if (selection.isCollapsed && !WI.isEventTargetAnEditableField(event)) {
> 
> Decrease indenting with early return:
> 
> if (!selection.isCollapsed || WI.isTargetAnEditableField(event))
>     return;

This was mainly done to have a consistent style with `WI._copy`, but there's no need for it really.  I'll change it.

>> Source/WebInspectorUI/UserInterface/Base/Main.js:2631
>> +        if (focusedCopyHandler && typeof focusedCopyHandler.handlePasteEvent === "function") {
> 
> Was this copied from WI._copy? The comment above looks wrong too.

Oops. :|
Comment 6 Devin Rousso 2019-07-01 23:48:12 PDT
(In reply to Matt Baker from comment #4)
> You can add this in a follow up if you want, but I noticed this doesn't work with comment nodes.
You mean pasting when a comment node is focused?  Interesting 🤔

This is because `insertAdjacentHTML` only exists on Element nodes, which a Comment is not.  Perhaps we can be smarter with `insertAdjacentHTML` to adjust the `position` based on the type of node (e.g. use the parent or next sibling if the selected node is a comment).

<https://webkit.org/b/199396>
Comment 7 Devin Rousso 2019-07-02 00:01:54 PDT
(In reply to Matt Baker from comment #2)
> Do we want to select the pasted node automatically? I was a little surprised after hitting paste and not seeing the selection change.
Yes, we probably should, but there's a lot of work that would need to be done to properly ensure that we select the node.  Right now, `insertAdjacentHTML` doesn't send any information back to the frontend about the id (or any other data) of the newly added node(s).  As such, we'd basically have to wait for the DOM tree to update, and select the proper node as such.  I think this may be safer/simpler to address as a followup.  <https://webkit.org/b/199397>
Comment 8 Devin Rousso 2019-07-02 00:03:57 PDT
Created attachment 373306 [details]
Patch
Comment 9 Matt Baker 2019-07-02 08:47:02 PDT
Comment on attachment 373306 [details]
Patch

r=me
Comment 10 WebKit Commit Bot 2019-07-02 09:18:15 PDT
Comment on attachment 373306 [details]
Patch

Clearing flags on attachment: 373306

Committed r247054: <https://trac.webkit.org/changeset/247054>
Comment 11 WebKit Commit Bot 2019-07-02 09:18:16 PDT
All reviewed patches have been landed.  Closing bug.
Comment 12 Radar WebKit Bug Importer 2019-07-02 09:19:18 PDT
<rdar://problem/52527047>