-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Use styleSheetAdded and styleSheetRemoved, replaces getAllStylesheets #7008
Changes from 3 commits
04b015e
e2ef6eb
0905ca4
02e2b10
ab40ce0
d2a8a82
87bb739
24d3cf2
8af9aee
24a3969
cbe4b09
190f437
d2b33f5
4ac3e07
00dde93
85b5d8c
d1a6752
3cee9fe
ac80eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,17 +28,22 @@ | |
| /** | ||
| * CSSAgent keeps track of loaded style sheets and allows reloading them | ||
| * from a {Document}. | ||
| * | ||
| * CSSAgent dispatches styleSheetAdded and styleSheetRemoved events, passing | ||
| * the URL for the added/removed style sheet. | ||
| */ | ||
|
|
||
| define(function CSSAgent(require, exports, module) { | ||
| "use strict"; | ||
|
|
||
| require("thirdparty/path-utils/path-utils.min"); | ||
|
|
||
| var Inspector = require("LiveDevelopment/Inspector/Inspector"); | ||
|
|
||
| var _load; // {$.Deferred} load promise | ||
| var _urlToStyle; // {url -> loaded} style definition | ||
| /** @type {Object.<string, CSS.CSSStyleSheetHeader>} */ | ||
| var _urlToStyle; | ||
|
|
||
| /** @type {Object.<string, string>} */ | ||
| var _styleSheetIdToUrl; | ||
|
|
||
| /** | ||
| * Create a canonicalized version of the given URL, stripping off query strings and hashes. | ||
|
|
@@ -49,34 +54,29 @@ define(function CSSAgent(require, exports, module) { | |
| return PathUtils.parseUrl(url).hrefNoSearch; | ||
| } | ||
|
|
||
| // WebInspector Event: Page.loadEventFired | ||
| function _onLoadEventFired(event, res) { | ||
| // res = {timestamp} | ||
| /** | ||
| * @private | ||
| * WebInspector Event: Page.frameNavigated | ||
| * @param {jQuery.Event} event | ||
| * @param {frame: Frame} res | ||
| */ | ||
| function _onFrameNavigated(event, res) { | ||
| // Clear maps when navigating to a new page | ||
| _urlToStyle = {}; | ||
| Inspector.CSS.enable().done(function () { | ||
| Inspector.CSS.getAllStyleSheets(function onGetAllStyleSheets(res) { | ||
| var i, header; | ||
| for (i in res.headers) { | ||
| header = res.headers[i]; | ||
| _urlToStyle[_canonicalize(header.sourceURL)] = header; | ||
| } | ||
| _load.resolve(); | ||
| }); | ||
| }); | ||
| _styleSheetIdToUrl = {}; | ||
| } | ||
|
|
||
| /** Get a style sheet for a url | ||
| * @param {string} url | ||
| */ | ||
| function styleForURL(url) { | ||
| if (_urlToStyle) { | ||
| return _urlToStyle[_canonicalize(url)]; | ||
| } | ||
|
|
||
| return null; | ||
| return _urlToStyle[_canonicalize(url)]; | ||
| } | ||
|
|
||
| /** Get a list of all loaded stylesheet files by URL */ | ||
| /** | ||
| * @deprecated Use styleSheetAdded and styleSheetRemoved events | ||
| * Get a list of all loaded stylesheet files by URL | ||
| */ | ||
| function getStylesheetURLs() { | ||
| var urls = [], url; | ||
| for (url in _urlToStyle) { | ||
|
|
@@ -104,17 +104,79 @@ define(function CSSAgent(require, exports, module) { | |
| console.assert(style, "Style Sheet for document not loaded: " + doc.url); | ||
| Inspector.CSS.setStyleSheetText(style.styleSheetId, ""); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * @param {jQuery.Event} event | ||
| * @param {header: CSSStyleSheetHeader} | ||
| */ | ||
| function _styleSheetAdded(event, res) { | ||
| if (!_urlToStyle) { | ||
| return; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the initialization:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| var url = _canonicalize(res.header.sourceURL), | ||
| existing = _urlToStyle[url]; | ||
|
|
||
| // detect duplicates | ||
| if (existing && existing.styleSheetId === res.header.styleSheetId) { | ||
| return; | ||
| } | ||
|
|
||
| _urlToStyle[url] = res.header; | ||
| _styleSheetIdToUrl[res.header.styleSheetId] = url; | ||
|
|
||
| $(exports).triggerHandler("styleSheetAdded", [url]); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * @param {jQuery.Event} event | ||
| * @param {styleSheetId: StyleSheetId} | ||
| */ | ||
| function _styleSheetRemoved(event, res) { | ||
| if (!_urlToStyle) { | ||
| return; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer necessary to check for existence of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
|
||
| var url = _styleSheetIdToUrl[res.styleSheetId]; | ||
|
|
||
| if (url) { | ||
| delete _urlToStyle[url]; | ||
| } | ||
|
|
||
| delete _styleSheetIdToUrl[res.styleSheetId]; | ||
|
|
||
| $(exports).triggerHandler("styleSheetRemoved", [url]); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Attempt to use deleted API CSS.getAllStyleSheets | ||
| * @param {jQuery.Event} event | ||
| * @param {frameId: Network.FrameId} | ||
| */ | ||
| function _onFrameStoppedLoading(event, res) { | ||
| // _styleSheetAdded will ignore duplicates | ||
| Inspector.CSS.getAllStyleSheets(function (res) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this dump out an error in the console if the API doesn't exist? I wonder if we should bother doing this, since the events seem to have been around for awhile, and I don't think we need to support people who don't regularly update Chrome. (Although I guess we don't know exactly which version they were introduced in.)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will yes. We can suppress a subset of protocol errors, particularly this one where the method is not defined. The basic set of error codes are defined here http://www.jsonrpc.org/specification#error_object. I can't find any documentation for Chrome that defines implementation-specific error codes. |
||
| res.headers.forEach(function (header) { | ||
| _styleSheetAdded(null, { header: header }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** Initialize the agent */ | ||
| function load() { | ||
| _load = new $.Deferred(); | ||
| $(Inspector.Page).on("loadEventFired.CSSAgent", _onLoadEventFired); | ||
| return _load.promise(); | ||
| $(Inspector.Page).on("frameNavigated.CSSAgent", _onFrameNavigated); | ||
| $(Inspector.Page).on("frameStoppedLoading.CSSAgent", _onFrameStoppedLoading); | ||
| $(Inspector.CSS).on("styleSheetAdded.CSSAgent", _styleSheetAdded); | ||
| $(Inspector.CSS).on("styleSheetRemoved.CSSAgent", _styleSheetRemoved); | ||
| } | ||
|
|
||
| /** Clean up */ | ||
| function unload() { | ||
| $(Inspector.Page).off(".CSSAgent"); | ||
| $(Inspector.CSS).off(".CSSAgent"); | ||
| } | ||
|
|
||
| // Export public functions | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since code no longer checks
if (_urlToStyle)then_urlToStyleshould be initialized to_urlToStyle = {}on line 43 to be safe. Same for_styleSheetIdToUrl.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed