WebKit icon indicating copy to clipboard operation
WebKit copied to clipboard

Web Inspector: Links to inline style sheets don't work

Open the-chenergy opened this issue 1 year ago โ€ข 3 comments

94b243ff86650d9671ecef23733bbe2e19276df1

Web Inspector: Links to inline style sheets don't work
rdar://122866054
https://bugs.webkit.org/show_bug.cgi?id=268818

Reviewed by NOBODY (OOPS!).

Add a feature to the inspector's frontend that, if an inline/internal
CSS rule (one that lives within an HTML <style> element) is created
dynamically with JavaScript, the style details panel should link
that rule to its owner <style> element in the DOM tree in the Elements
tab.

The backend now includes the relevant data for a CSS header that links
a style sheet to its owner <style> element if one exists.

The frontend now takes this data and renders a link to that <style>
element as a CSS rule's source URL when appropriate.

NOTE:
- This new feature currently does not always work as it suffers from
  https://webkit.org/b/213499, where during the construction of a
  CSSStyleSheetHeader protocol object and trying to retrieve the node ID
  of the style sheet's owner node, the document element might not have
  been instrumented to have a bound ID. (See https://github.com/WebKit/WebKit/blob/e9235eeac4b213cb15e87a5c1b2a97bd27826195/Source/WebCore/inspector/agents/InspectorDOMAgent.cpp#L596)
- In order to successfully use this feature, the user must open the web
  inspector and then do a hard-refresh (Option-Command-R). I have yet
  figured out what side effect hard-refresh had that made this
  work.

* Source/JavaScriptCore/inspector/protocol/CSS.json:
  - Add the ownerNodeId field to help link the style sheet to its owner
    HTML <style> element.
  - The isInline field's value did not match what its name suggested,
    and the frontend did not make use of this special meaning either.
    This commit fixes that so isInline always reflect whether the style
    sheet is inline/internal.
  - The startLine and startColumn fields had values of 0 if the style
    sheet was created dynamically, but 0 could be a meaningful value
    (indicating that the source was indeed at line 0). This commit makes
    them optional fields, so a missing value from these fields can
    inform that the style sheet was created dynamically.

* Source/WebCore/dom/InlineStyleSheetOwner.cpp:
(WebCore::InlineStyleSheetOwner::InlineStyleSheetOwner):
  - Use `TextPosition::belowRangePosition` to indicate that the style
    sheet is created dynamically, as the default value `TextPosition()`
    has a legit meaning (line 0, column 0).

* Source/WebCore/inspector/InspectorStyleSheet.h:
* Source/WebCore/inspector/InspectorStyleSheet.cpp:
(WebCore::InspectorStyleSheet::buildObjectForStyleSheetInfo):
  - Adapt to the new protocol for CSSStyleSheetHeader.
  - Since the CSSStyleSheetHeader now includes an ownerNodeId field,
    the InspectorStyleSheet needs help from the InspectorDOMAgent to
    retrieve the owner node's ID. Parametrize a helper function that
    returns a node's ID to achieve this.

* Source/WebCore/inspector/agents/InspectorCSSAgent.cpp:
(WebCore::InspectorCSSAgent::setActiveStyleSheetsForDocument):
(WebCore::InspectorCSSAgent::getAllStyleSheets):
  - Lend help to InspectorStyleSheet to retrieve the style sheet owner
    node's ID.

* Source/WebInspectorUI/UserInterface/Controllers/CSSManager.js:
(WI.CSSManager.prototype.preferredInspectorStyleSheetForFrame):
(WI.CSSManager.prototype.styleSheetAdded):
(WI.CSSManager.prototype._fetchInfoForAllStyleSheets):
  - Adapt to the new protocol for CSSStyleSheetHeader by recording
    the new ownerNodeId field.

* Source/WebInspectorUI/UserInterface/Models/CSSStyleSheet.js:
(WI.CSSStyleSheet):
  - Initialize the new field ownerNodeId to record the style sheet's
    owner HTML <style> element, if one exists from an update with
    `updateInfo()`.
  - Change the default values of startLineNumber and startColumn to NaN,
    which indicate the style sheet was created dynamically.

(WI.CSSStyleSheet.prototype.get ownerNodeId):
(WI.CSSStyleSheet.prototype.updateInfo):
  - Add support for the new field ownerNodeId.

* Source/WebInspectorUI/UserInterface/Views/StyleOriginView.js:
(WI.StyleOriginView.prototype.update):
(WI.StyleOriginView):
  - If a style sheet is created dynamically, render the URL link that
    links to the owner HTML <style> element within the DOM tree instead.
  - (The `(node ?)` text is there as a placeholder for now, which helps
    with debugging and demonstrating the issue described above in the
    NOTE section.)

https://github.com/WebKit/WebKit/commit/94b243ff86650d9671ecef23733bbe2e19276df1

Misc iOS, tvOS & watchOS macOS Linux Windows
โŒ ๐Ÿงช style โœ… ๐Ÿ›  ios โœ… ๐Ÿ›  mac โœ… ๐Ÿ›  wpe โœ… ๐Ÿ›  wincairo
โœ… ๐Ÿงช bindings โœ… ๐Ÿ›  ios-sim โœ… ๐Ÿ›  mac-AS-debug โœ… ๐Ÿงช wpe-wk2
โœ… ๐Ÿงช webkitperl โœ… ๐Ÿงช ios-wk2 โœ… ๐Ÿงช api-mac โœ… ๐Ÿงช api-wpe
โœ… ๐Ÿงช ios-wk2-wpt โœ… ๐Ÿงช mac-wk1 โœ… ๐Ÿ›  gtk
โœ… ๐Ÿ›  ๐Ÿงช jsc โœ… ๐Ÿงช api-ios โœ… ๐Ÿงช mac-wk2 โŒ ๐Ÿงช gtk-wk2
โœ… ๐Ÿ›  ๐Ÿงช jsc-arm64 โœ… ๐Ÿ›  tv โŒ ๐Ÿงช mac-AS-debug-wk2 โœ… ๐Ÿงช api-gtk
โœ… ๐Ÿ›  tv-sim โœ… ๐Ÿ›  jsc-armv7
โœ… ๐Ÿ›  watch โœ… ๐Ÿงช jsc-armv7-tests
โœ… ๐Ÿ›  watch-sim

the-chenergy avatar Feb 29 '24 22:02 the-chenergy

Video demo of behavior AFTER this commit:

  • The style details panel now links CSS rules that are part of an inline/internal style sheet to their owner <style> elements.
  • Due to this bug, the link currently requires a hard-reload of the webpage with the inspector open (!#R) to have an effect.
  • This new behavior (when working) is the same as what Chrome does.

https://github.com/WebKit/WebKit/assets/44916353/8941191c-9bdd-4e3a-8db4-092e03a06cd7


Video demo of behavior BEFORE this commit:

https://github.com/WebKit/WebKit/assets/44916353/c0089010-686f-430f-84ca-162ca3106280


The demo webpage I used in the videos: dynamic-stylesheet-demo.zip

the-chenergy avatar Feb 29 '24 23:02 the-chenergy