Bug 94107

Summary: Refactoring: move EventHandler::targetNode into HitTestResult
Product: WebKit Reporter: Iain Merrick <husky>
Component: New BugsAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: abarth, aboxhall, allan.jensen, dglazkov, eric, husky, rniwa, webkit.review.bot
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch none

Description Iain Merrick 2012-08-15 07:17:55 PDT
Refactoring: move EventHandler::targetNode into HitTestResult
Comment 1 Iain Merrick 2012-08-15 07:23:09 PDT
Created attachment 158566 [details]
Patch
Comment 2 Iain Merrick 2012-08-15 07:26:33 PDT
Unfortunately had to modify lots of platform-specific files. Relying on EWS to catch any mistakes.

There are several places where we call targetNode() repeatedly; seems like it'd be worth calling it once and caching the result. I have *not* done so in this patch but let me know if you'd like me to.
Comment 3 Adam Barth 2012-08-15 08:56:48 PDT
Comment on attachment 158566 [details]
Patch

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

> Source/WebCore/rendering/HitTestResult.cpp:808
> +    Node* node = innerNode();
> +    if (!node)
> +        return 0;
> +    if (node->inDocument())
> +        return node;
> +
> +    Element* element = node->parentElement();
> +    if (element && element->inDocument())
> +        return element;

I'm trying to understanding how this is different from innerNode.  In the context of an event, it makes sense to speak about the targetNode of an event, but outside the context of an event the word "target" doesn't mean that much.

Specifically, how can a node not be inDocument but its parentElement be inDocument?

> Source/WebCore/rendering/HitTestResult.h:180
> +    Node* targetNode() const;

I would move this function next to innerNode() so that it's clear that there is a choice between the two.
Comment 4 Iain Merrick 2012-08-15 10:00:36 PDT
Comment on attachment 158566 [details]
Patch

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

>> Source/WebCore/rendering/HitTestResult.cpp:808
>> +        return element;
> 
> I'm trying to understanding how this is different from innerNode.  In the context of an event, it makes sense to speak about the targetNode of an event, but outside the context of an event the word "target" doesn't mean that much.
> 
> Specifically, how can a node not be inDocument but its parentElement be inDocument?

That's a great question, and I don't know! I'm trying to understand this code by teasing apart the bits that look unnecessarily tangled-up.

Does inDocument() refer to the shadow DOM?

>> Source/WebCore/rendering/HitTestResult.h:180
>> +    Node* targetNode() const;
> 
> I would move this function next to innerNode() so that it's clear that there is a choice between the two.

Good idea, will do.
Comment 5 Dimitri Glazkov (Google) 2012-08-15 10:12:32 PDT
(In reply to comment #4)
> (From update of attachment 158566 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=158566&action=review
> 
> >> Source/WebCore/rendering/HitTestResult.cpp:808
> >> +        return element;
> > 
> > I'm trying to understanding how this is different from innerNode.  In the context of an event, it makes sense to speak about the targetNode of an event, but outside the context of an event the word "target" doesn't mean that much.
> > 
> > Specifically, how can a node not be inDocument but its parentElement be inDocument?
> 
> That's a great question, and I don't know! I'm trying to understand this code by teasing apart the bits that look unnecessarily tangled-up.
> 
> Does inDocument() refer to the shadow DOM?

No. It's a bit on a node that tracks whether a node is in a document tree (shadow DOM elements are  considered in-document for this purpose). It answer the question: is the node floating in space, or can you reach the document node by traversing its ancestor?

> 
> >> Source/WebCore/rendering/HitTestResult.h:180
> >> +    Node* targetNode() const;
> > 
> > I would move this function next to innerNode() so that it's clear that there is a choice between the two.
> 
> Good idea, will do.
Comment 6 Dimitri Glazkov (Google) 2012-08-15 10:16:32 PDT
Comment on attachment 158566 [details]
Patch

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

>>>> Source/WebCore/rendering/HitTestResult.cpp:808
>>>> +        return element;
>>> 
>>> I'm trying to understanding how this is different from innerNode.  In the context of an event, it makes sense to speak about the targetNode of an event, but outside the context of an event the word "target" doesn't mean that much.
>>> 
>>> Specifically, how can a node not be inDocument but its parentElement be inDocument?
>> 
>> That's a great question, and I don't know! I'm trying to understand this code by teasing apart the bits that look unnecessarily tangled-up.
>> 
>> Does inDocument() refer to the shadow DOM?
> 
> No. It's a bit on a node that tracks whether a node is in a document tree (shadow DOM elements are  considered in-document for this purpose). It answer the question: is the node floating in space, or can you reach the document node by traversing its ancestor?

Ah, the difference is that the innerNode can be a non-Element for hit-testing purposes. For events, it has to be an element.
Comment 7 Dimitri Glazkov (Google) 2012-08-15 10:17:43 PDT
Comment on attachment 158566 [details]
Patch

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

>>>>> Source/WebCore/rendering/HitTestResult.cpp:808
>>>>> +        return element;
>>>> 
>>>> I'm trying to understanding how this is different from innerNode.  In the context of an event, it makes sense to speak about the targetNode of an event, but outside the context of an event the word "target" doesn't mean that much.
>>>> 
>>>> Specifically, how can a node not be inDocument but its parentElement be inDocument?
>>> 
>>> That's a great question, and I don't know! I'm trying to understand this code by teasing apart the bits that look unnecessarily tangled-up.
>>> 
>>> Does inDocument() refer to the shadow DOM?
>> 
>> No. It's a bit on a node that tracks whether a node is in a document tree (shadow DOM elements are  considered in-document for this purpose). It answer the question: is the node floating in space, or can you reach the document node by traversing its ancestor?
> 
> Ah, the difference is that the innerNode can be a non-Element for hit-testing purposes. For events, it has to be an element.

I don't know why we're checking for element->inDocument there though. I think we should just zap that.
Comment 8 Iain Merrick 2012-08-15 10:21:15 PDT
(In reply to comment #7)
> > Ah, the difference is that the innerNode can be a non-Element for hit-testing purposes. For events, it has to be an element.

Could we give this a better name, then? innerElement?

> I don't know why we're checking for element->inDocument there though. I think we should just zap that.

So just "return element ? element : node;"?
Comment 9 Dimitri Glazkov (Google) 2012-08-15 10:25:59 PDT
Comment on attachment 158566 [details]
Patch

I am actually just totally confused about what this code does. Let me dig up some history...
Comment 10 Iain Merrick 2012-08-15 10:27:13 PDT
(In reply to comment #9)
> (From update of attachment 158566 [details])
> I am actually just totally confused about what this code does. Let me dig up some history...

Glad it's not just me. Thanks for your patience. :)
Comment 11 Dimitri Glazkov (Google) 2012-08-15 10:28:35 PDT
First off, it looks like you're reverting the change that Alice made: https://bugs.webkit.org/show_bug.cgi?id=57921
Comment 12 Dimitri Glazkov (Google) 2012-08-15 10:34:38 PDT
The archeology sheds no light. I think we should do this:

1) work on this patch
2) file a bug to refurbish targetNode function and fix it.

Also -- why exactly do you need targetNode?
Comment 13 Iain Merrick 2012-08-15 10:38:45 PDT
(In reply to comment #11)
> First off, it looks like you're reverting the change that Alice made: https://bugs.webkit.org/show_bug.cgi?id=57921

Hmm, true. I'd argue that this is a different way of doing the same thing (moving the method from MouseEventWithHitTestResults -> HitTestResult, instead of MouseEventWithHitTestResults -> EventHandler); and now that WebFrameImpl is calling this same method, HitTestResult is a more natural location.

Please feel free to push back if you think this refactoring isn't worthwhile. But it looks to me (coming to this code fresh and not fully understanding it yet) like EventHandler has accumulated some cruft.
Comment 14 Iain Merrick 2012-08-15 10:41:30 PDT
(In reply to comment #12)
> The archeology sheds no light. I think we should do this:
> 
> 1) work on this patch
> 2) file a bug to refurbish targetNode function and fix it.
> 
> Also -- why exactly do you need targetNode?

My main focus here is unforking some Android-specific relating to text selection -- see https://bugs.webkit.org/show_bug.cgi?id=93998

I figured it was worth trying to clean up the campground a little at the same time. (Our Android changes are a bit hacky and need cleaning up anyway.)
Comment 15 Dimitri Glazkov (Google) 2012-08-15 10:57:33 PDT
(In reply to comment #13)
> (In reply to comment #11)
> > First off, it looks like you're reverting the change that Alice made: https://bugs.webkit.org/show_bug.cgi?id=57921
> 
> Hmm, true. I'd argue that this is a different way of doing the same thing (moving the method from MouseEventWithHitTestResults -> HitTestResult, instead of MouseEventWithHitTestResults -> EventHandler); and now that WebFrameImpl is calling this same method, HitTestResult is a more natural location.
> 
> Please feel free to push back if you think this refactoring isn't worthwhile. But it looks to me (coming to this code fresh and not fully understanding it yet) like EventHandler has accumulated some cruft.

No, I think this makes sense.
Comment 16 Dimitri Glazkov (Google) 2012-08-15 11:02:54 PDT
Comment on attachment 158566 [details]
Patch

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

I am ok with this. Ryosuke, any comments before we land?

> Source/WebCore/rendering/HitTestResult.cpp:798
> +Node* HitTestResult::targetNode() const

Let's file a separate bug to refurbish this.
Comment 17 Ryosuke Niwa 2012-08-15 11:32:39 PDT
I don't think "targetNode" is a descriptive name. The whole point of this function is to preferring an attached node when the inner node is detached from the document. To make it even more ad-hoc, it only moves up one level in the tree. The function name should at least reflect that.
Comment 18 WebKit Review Bot 2012-08-15 15:14:21 PDT
Comment on attachment 158566 [details]
Patch

Clearing flags on attachment: 158566

Committed r125715: <http://trac.webkit.org/changeset/125715>
Comment 19 WebKit Review Bot 2012-08-15 15:14:27 PDT
All reviewed patches have been landed.  Closing bug.