Bug 213104

Summary: Document.currentScript does not work for SVGScriptElements
Product: WebKit Reporter: Sam Weinig <sam>
Component: New BugsAssignee: Sam Weinig <sam>
Status: RESOLVED FIXED    
Severity: Normal CC: berto, cdumez, cgarcia, darin, esprehn+autocc, ews-watchlist, gustavo, kangil.han, kondapallykalyan, rniwa, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch ysuzuki: review+

Description Sam Weinig 2020-06-11 16:49:24 PDT
Document.currentScript does not work for SVGScriptElements
Comment 1 Sam Weinig 2020-06-11 16:51:04 PDT
Document.currentScript is specified to work with both HTMLScriptElements and SVGScriptElements (https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript) but out implementation only works with HTMLScriptElements.
Comment 2 Sam Weinig 2020-06-11 17:02:30 PDT
This is manifest by the failing WPT test html/dom/documents/dom-tree-accessors/Document.currentScript.html
Comment 3 Sam Weinig 2020-06-11 17:02:37 PDT Comment hidden (obsolete)
Comment 4 Sam Weinig 2020-06-11 17:32:54 PDT Comment hidden (obsolete)
Comment 5 EWS Watchlist 2020-06-11 17:33:34 PDT Comment hidden (obsolete)
Comment 6 Sam Weinig 2020-06-11 20:13:39 PDT
Created attachment 401702 [details]
Patch
Comment 7 Yusuke Suzuki 2020-06-11 20:42:10 PDT
Comment on attachment 401702 [details]
Patch

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

r=me

> Source/WebKit/WebProcess/InjectedBundle/API/gtk/DOM/WebKitDOMDocumentGtk.cpp:1913
> +        return NULL;

nullptr?

> Source/WebKitLegacy/mac/DOM/DOMDocument.mm:409
> +        return NULL;

nil?
Comment 8 Sam Weinig 2020-06-12 07:28:45 PDT
Committed r262945: <https://trac.webkit.org/changeset/262945>
Comment 9 Radar WebKit Bug Importer 2020-06-12 07:29:14 PDT
<rdar://problem/64296452>
Comment 10 Darin Adler 2020-06-12 09:40:08 PDT
Comment on attachment 401702 [details]
Patch

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

> Source/WebCore/dom/Document.h:1040
> +    void pushCurrentScript(Element*);

This should take Element&.

> Source/WebCore/dom/Document.h:1792
> +    Vector<RefPtr<Element>> m_currentScriptStack;

This should be Vector<Ref<Element>>.

> Source/WebKit/WebProcess/InjectedBundle/API/gtk/DOM/WebKitDOMDocumentGtk.cpp:1911
> +    WebCore::Element* element = item->currentScript();

auto would make this better.

> Source/WebKitLegacy/mac/DOM/DOMDocument.mm:407
> +    WebCore::Element* element = IMPL->currentScript();

auto would make this better.
Comment 11 Darin Adler 2020-06-12 10:03:24 PDT
Sorry, didn’t notice my comments were post-commit.
Comment 12 Yusuke Suzuki 2020-06-12 10:40:34 PDT
Comment on attachment 401702 [details]
Patch

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

>> Source/WebCore/dom/Document.h:1040
>> +    void pushCurrentScript(Element*);
> 
> This should take Element&.

This needs to be Element* since this can take `nullptr` in meaningful way.
If the module script is executed (<script type="module">), currentScript needs to be `null` JS value.
This is represented by `nullptr` here.
Comment 13 Sam Weinig 2020-06-12 10:56:50 PDT
(In reply to Darin Adler from comment #10)
> Comment on attachment 401702 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=401702&action=review
> 
> > Source/WebCore/dom/Document.h:1040
> > +    void pushCurrentScript(Element*);
> 
> This should take Element&.
> 
> > Source/WebCore/dom/Document.h:1792
> > +    Vector<RefPtr<Element>> m_currentScriptStack;
> 
> This should be Vector<Ref<Element>>.

Unfortunately, null is used in a meaningful way in this stack, so it can't use references here.
Comment 14 Darin Adler 2020-06-12 10:57:14 PDT
Comment on attachment 401702 [details]
Patch

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

>>>> Source/WebCore/dom/Document.h:1040
>>>> +    void pushCurrentScript(Element*);
>>> 
>>> This should take Element&.
>> 
>> This needs to be Element* since this can take `nullptr` in meaningful way.
>> If the module script is executed (<script type="module">), currentScript needs to be `null` JS value.
>> This is represented by `nullptr` here.
> 
> Unfortunately, null is used in a meaningful way in this stack, so it can't use references here.

So you’re saying that when executing a module script we explicitly push a nullptr? OK, got it.
Comment 15 Sam Weinig 2020-06-12 10:57:46 PDT
(In reply to Darin Adler from comment #11)
> Sorry, didn’t notice my comments were post-commit.

Oh, please don't ever worry about that. I think some of my best comments are post-commit!
Comment 16 Sam Weinig 2020-06-12 10:58:25 PDT
(In reply to Darin Adler from comment #14)
> Comment on attachment 401702 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=401702&action=review
> 
> >>>> Source/WebCore/dom/Document.h:1040
> >>>> +    void pushCurrentScript(Element*);
> >>> 
> >>> This should take Element&.
> >> 
> >> This needs to be Element* since this can take `nullptr` in meaningful way.
> >> If the module script is executed (<script type="module">), currentScript needs to be `null` JS value.
> >> This is represented by `nullptr` here.
> > 
> > Unfortunately, null is used in a meaningful way in this stack, so it can't use references here.
> 
> So you’re saying that when executing a module script we explicitly push a
> nullptr? OK, got it.

Yeah. The code is:

`m_document.pushCurrentScript(shouldPushNullForCurrentScript ? nullptr : &scriptElement.element());`
Comment 17 Yusuke Suzuki 2020-06-12 11:12:11 PDT
Comment on attachment 401702 [details]
Patch

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

>>>>>> Source/WebCore/dom/Document.h:1040
>>>>>> +    void pushCurrentScript(Element*);
>>>>> 
>>>>> This should take Element&.
>>>> 
>>>> This needs to be Element* since this can take `nullptr` in meaningful way.
>>>> If the module script is executed (<script type="module">), currentScript needs to be `null` JS value.
>>>> This is represented by `nullptr` here.
>>> 
>>> Unfortunately, null is used in a meaningful way in this stack, so it can't use references here.
>> 
>> So you’re saying that when executing a module script we explicitly push a nullptr? OK, got it.
> 
> Yeah. The code is:
> 
> `m_document.pushCurrentScript(shouldPushNullForCurrentScript ? nullptr : &scriptElement.element());`

Yes. And this is specified behavior :)