-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Use styleSheetAdded and styleSheetRemoved, replaces getAllStylesheets #7008
Changes from all 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,25 @@ | |
| /** | ||
| * 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; | ||
|
|
||
| /** @type {boolean} */ | ||
| var _getAllStyleSheetsNotFound = false; | ||
|
|
||
| /** | ||
| * Create a canonicalized version of the given URL, stripping off query strings and hashes. | ||
|
|
@@ -49,34 +57,31 @@ 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 | ||
| /** | ||
| * Get a style sheet for a url | ||
| * @param {string} url | ||
| * @return {CSS.CSSStyleSheetHeader} | ||
| */ | ||
| 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) { | ||
|
|
@@ -87,37 +92,112 @@ define(function CSSAgent(require, exports, module) { | |
| return urls; | ||
| } | ||
|
|
||
| /** Reload a CSS style sheet from a document | ||
| /** | ||
| * Reload a CSS style sheet from a document | ||
| * @param {Document} document | ||
| * @return {jQuery.Promise} | ||
| */ | ||
| function reloadCSSForDocument(doc) { | ||
| var style = styleForURL(doc.url); | ||
| console.assert(style, "Style Sheet for document not loaded: " + doc.url); | ||
| Inspector.CSS.setStyleSheetText(style.styleSheetId, doc.getText()); | ||
| return Inspector.CSS.setStyleSheetText(style.styleSheetId, doc.getText()); | ||
| } | ||
|
|
||
| /** Empties a CSS style sheet given a document that has been deleted | ||
| /** | ||
| * Empties a CSS style sheet given a document that has been deleted | ||
| * @param {Document} document | ||
| * @return {jQuery.Promise} | ||
| */ | ||
| function clearCSSForDocument(doc) { | ||
| var style = styleForURL(doc.url); | ||
| console.assert(style, "Style Sheet for document not loaded: " + doc.url); | ||
| Inspector.CSS.setStyleSheetText(style.styleSheetId, ""); | ||
| return Inspector.CSS.setStyleSheetText(style.styleSheetId, ""); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * @param {jQuery.Event} event | ||
| * @param {header: CSSStyleSheetHeader} | ||
| */ | ||
| function _styleSheetAdded(event, res) { | ||
| 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, res.header]); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * @param {jQuery.Event} event | ||
| * @param {styleSheetId: StyleSheetId} | ||
| */ | ||
| function _styleSheetRemoved(event, res) { | ||
| var url = _styleSheetIdToUrl[res.styleSheetId], | ||
| header = url && _urlToStyle[url]; | ||
|
|
||
| if (url) { | ||
| delete _urlToStyle[url]; | ||
| } | ||
|
|
||
| delete _styleSheetIdToUrl[res.styleSheetId]; | ||
|
|
||
| $(exports).triggerHandler("styleSheetRemoved", [url, header]); | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Attempt to use deleted API CSS.getAllStyleSheets | ||
| * @param {jQuery.Event} event | ||
| * @param {frameId: Network.FrameId} | ||
| */ | ||
| function _onFrameStoppedLoading(event, res) { | ||
| // Manually fire getAllStyleSheets since it will be removed from | ||
| // Inspector.json in a future update | ||
| Inspector.send("CSS", "getAllStyleSheets").done(function (res) { | ||
| res.headers.forEach(function (header) { | ||
| // _styleSheetAdded will ignore duplicates | ||
| _styleSheetAdded(null, { header: header }); | ||
| }); | ||
| }).fail(function (err) { | ||
| // Disable getAllStyleSheets if the first call fails | ||
| _getAllStyleSheetsNotFound = (err.code === -32601); | ||
|
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. Maybe write to console.log if it's some other error code?
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. We do this already in the |
||
| $(Inspector.Page).off("frameStoppedLoading.CSSAgent", _onFrameStoppedLoading); | ||
| }); | ||
| } | ||
|
|
||
| /** Enable the domain */ | ||
| function enable() { | ||
|
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. Allow |
||
| return Inspector.CSS.enable(); | ||
| } | ||
|
|
||
| /** Initialize the agent */ | ||
| function load() { | ||
| _load = new $.Deferred(); | ||
| $(Inspector.Page).on("loadEventFired.CSSAgent", _onLoadEventFired); | ||
| return _load.promise(); | ||
| $(Inspector.Page).on("frameNavigated.CSSAgent", _onFrameNavigated); | ||
| $(Inspector.CSS).on("styleSheetAdded.CSSAgent", _styleSheetAdded); | ||
| $(Inspector.CSS).on("styleSheetRemoved.CSSAgent", _styleSheetRemoved); | ||
|
|
||
| // getAllStyleSheets was deleted beginning with Chrome 34 | ||
| if (!_getAllStyleSheetsNotFound) { | ||
| $(Inspector.Page).on("frameStoppedLoading.CSSAgent", _onFrameStoppedLoading); | ||
| } | ||
| } | ||
|
|
||
| /** Clean up */ | ||
| function unload() { | ||
| $(Inspector.Page).off(".CSSAgent"); | ||
| $(Inspector.CSS).off(".CSSAgent"); | ||
| } | ||
|
|
||
| // Export public functions | ||
| exports.enable = enable; | ||
| exports.styleForURL = styleForURL; | ||
| exports.getStylesheetURLs = getStylesheetURLs; | ||
| exports.reloadCSSForDocument = reloadCSSForDocument; | ||
|
|
||
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