Files
palemoon27/toolkit/devtools/debugger/utils.js
T
roytam1 8d1a041130 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1209252: add buttons to clear session and signaling logs. r=jib, r=sicking (39d0a4b57d)
- Bug 1197673 - Use float type for SetAudioOutputVolume. r=eitan (75eae2f441)
- Bug 1188099 - (Part 4) Introduce mochitest coverage for speech global queue. r=smaug r=kdavis (fd4c804c72)
- Bug 1226015 - Have child send __delete__ in speech synth request protocol, fixes race. r=smaug (c46532745a)
- Bug 1230533 - Cancel speech when controlling window goes away. r=smaug (3dae89ffaf)
- Bug 1237176 - skip dom/media/webspeech/synth/test/test_bfcache.html on Linux and Android for frequent timeouts (0d1eabb6fb)
- Bug 1237176 - actually disable test_bfcache.html on Android (1558e0bf39)
- Bug 1240871 - Don't allow implicit "async" in IPDL (r=mccr8,billm) (1db25ace3c)
- Bug 1000040 - Part 2: Implement EthernetManager; r=vicamo (82c01ca9a7)
- Bug 1000040 - Part 3: Test cases; r=vicamo (6a967cc5d9)
- Bug 1245241 - part 2 - remove TYPE_SYSV Shmems from IPDL. r=billm (6d6a4570ca)
- Bug 1245241 - part 3 - remove unused AdoptShmem from IPDL. r=billm (2762a13841)
- Bug 1107792: Don't ask for similar if the surface has an error, and more data in crash reports. r=milan (ead5108a93)
- Bug 1231881 - Remove NativeSurfaceType::CAIRO_SURFACE. r=bas. (c0f03e02f6)
- Bug 1241163 - Replace DrawTarget::CreateSourceSurfaceFromNativeSurface(CAIRO_CONTEXT) with Factory::CreateSourceSurfaceForCairoSurface. r=jrmuizel (06a96cdce6)
- Bug 1240177 - part 1 - add helper function to make Skia image info, r=jrmuizel (6a7d05aeb7)
- Bug 1240177 - part 2 - cleanups of DrawTargetSkia and SourceSurfaceSkia creation for GPU contexts, r=jrmuizel (3211ea9c0e)
- Bug 1240177 - part 3 - make DrawTargetSkia::OptimizeSourceSurface directly upload to GPU textures, r=jrmuizel (743d57a46c)
- Bug 1240177 - part 4 - avoid GPU readbacks in SourceSurfaceSkia::DrawTargetWillChange, r=jrmuizel (f568754717)
- Bug 1240177 - fix SourceSurfaceSkia::DrawTargetWillChange deepCopyTo usage. r=mattwoodrow (2e53d13ba3)
- Bug 1239152 Memset RGBX surfaces to opaque white. r=nical (977c7d5ede)
- Bug 1245241 - part 4 - move SharedMemorySysV details into nsShmImage. r=nical (0015d3caac)
- Bug 1245241 - Force the main thread to sync with the compositor when it tries to allocate insane amounts of shmems. r=lsalzman (7683d2afa7)
- Bug 1245241 - fix SharedMemoryBasic_android CloseHandle to null out handle. r=billm a=kwierso (9fa3f93233)
- Bug 1239152 - skip memset since XShm is already initialized to zero. r=mchang (477ecf2d9a)
- Bug 1227927 Part 1 - Make nsIFrame::PrincipalChildList a const function. r=bz (d9b690a1bc)
- Bug 1214377 - Hack to solve Thunderbird's need to detect style pre-wrap. r=roc a=me (a481ba9020)
- Bug 1214377 - Hack to solve Thunderbird's need to detect style pre-wrap. rs=ehsan on a CLOSED TREE (a15dfcaeb3)
- Bug 1240372 - Don't OOM crash in nsDocumentEncoder::EncodeToStringWithMaxLength() when the string buffer allocation fails; r=bzbarsky (76bed6a40a)
- Bug 1143570 - Part 1: Use an nsBlockInFlowLineIterator to determine whether a BR frame is visible or not; r=roc (99609f6fcd)
- Bug 1064843 part 2 - Make nsSubDocumentFrame inherit nsContainerFrame. r=dholbert (070202e396)
- Bug 1064843 part 3 - Make nsFormControlFrame inherit nsContainerFrame. r=dholbert (cee5125993)
- Bug 1064843 part 4 - Add a placeholder type for top layer. r=roc (e4e564b05b)
- Bug 1064843 part 5 - Ensure frames behave properly for unknown child list id passed into SetInitialChildList. r=dholbert (540f3da6a1)
- Bug 1064843 part 6 - Add backdrop frame list. r=dholbert (79f91fc815)
- Bug 1215365 - Update ua style sheet of fullscreen element. r=heycam (b44060a518)
- Bug 1235969 - Unprefix uses of -moz-transform, -moz-transform-origin and -moz-backface-visibility in UA style sheets. r=xidorn (c7a83cdcae)
- Bug 1064843 part 7 - Add backdrop pseudo-element and add related UA stylesheet. r=heycam (1d89afa54a)
- Bug 1064843 part 8 - Add frame class for backdrop frame. r=dbaron (9146be0073)
- Bug 1064843 part 9 - Change nsCSSFrameConstructor::CreatePlaceholderFrameFor to accept parent style context. r=dbaron (bc860e67f8)
- Bug 1064843 part 10 - Create and render backdrop frame for top layer frames. r=dbaron (1c6f165769)
- Bug 1064843 part 11 - Move checkWindowPureColor helper function from top-layer test to WindowSnapshot.js. r=roc (ef274dec50)
- Bug 1064843 part 12 - Add test for ::backdrop of fullscreen. r=dholbert (b978209868)
- Bug 1227927 Part 2 - Remove nsIFrame::GetFirstPrincipalChild(). r=mats (3f4ba2ef58)
- Bug 1227927 Part 3 - Use ranged-based for-loop to rewrite some simple loops in part 2. r=mats (6b22236653)
- Bug 1227927 Part 4 - Add comment to nsFrameList::GetLength() to warn it's O(n). r=mats (a08aa9185e)
- Bug 1101817 - Part 1: Remove usages of WeakMap.prototype.clear from Gecko. r=yzen,mak,yoric,gijs,jlongster (a992931bfe)
- Bug 1101817 - Part 3: Remove WeakMap.prototype.clear usages from Addons SDK. r=mossop (e81a8c3477)
- Bug 1101817 - Part 4: Remove WeakMap.prototype.clear from web-platform test. r=Ms2ger (1be3f645a2)
- Bug 1101817 - Part 6: Remove Weak{Map,Set}.prototype.clear. r=evilpie (7a0dbffdc9)
- Bug 1235152 - [css-break] Don't apply border/padding twice on inlines with box-decoration-break:clone and direction:rtl. r=roc (e6446262c1)
2023-08-21 10:59:29 +08:00

322 lines
9.3 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const XULUtils = {
/**
* Create <command> elements within `commandset` with event handlers
* bound to the `command` event
*
* @param commandset HTML Element
* A <commandset> element
* @param commands Object
* An object where keys specify <command> ids and values
* specify event handlers to be bound on the `command` event
*/
addCommands: function(commandset, commands) {
Object.keys(commands).forEach(name => {
let node = document.createElement('command');
node.id = name;
// XXX bug 371900: the command element must have an oncommand
// attribute as a string set by `setAttribute` for keys to use it
node.setAttribute('oncommand', ' ');
node.addEventListener('command', commands[name]);
commandset.appendChild(node);
});
}
};
// Used to detect minification for automatic pretty printing
const SAMPLE_SIZE = 50; // no of lines
const INDENT_COUNT_THRESHOLD = 5; // percentage
const CHARACTER_LIMIT = 250; // line character limit
/**
* Utility functions for handling sources.
*/
const SourceUtils = {
_labelsCache: new Map(), // Can't use WeakMaps because keys are strings.
_groupsCache: new Map(),
_minifiedCache: new WeakMap(),
/**
* Returns true if the specified url and/or content type are specific to
* javascript files.
*
* @return boolean
* True if the source is likely javascript.
*/
isJavaScript: function(aUrl, aContentType = "") {
return (aUrl && /\.jsm?$/.test(this.trimUrlQuery(aUrl))) ||
aContentType.contains("javascript");
},
/**
* Determines if the source text is minified by using
* the percentage indented of a subset of lines
*
* @return object
* A promise that resolves to true if source text is minified.
*/
isMinified: Task.async(function*(sourceClient) {
if (this._minifiedCache.has(sourceClient)) {
return this._minifiedCache.get(sourceClient);
}
let [, text] = yield DebuggerController.SourceScripts.getText(sourceClient);
let isMinified;
let lineEndIndex = 0;
let lineStartIndex = 0;
let lines = 0;
let indentCount = 0;
let overCharLimit = false;
// Strip comments.
text = text.replace(/\/\*[\S\s]*?\*\/|\/\/(.+|\n)/g, "");
while (lines++ < SAMPLE_SIZE) {
lineEndIndex = text.indexOf("\n", lineStartIndex);
if (lineEndIndex == -1) {
break;
}
if (/^\s+/.test(text.slice(lineStartIndex, lineEndIndex))) {
indentCount++;
}
// For files with no indents but are not minified.
if ((lineEndIndex - lineStartIndex) > CHARACTER_LIMIT) {
overCharLimit = true;
break;
}
lineStartIndex = lineEndIndex + 1;
}
isMinified =
((indentCount / lines) * 100) < INDENT_COUNT_THRESHOLD || overCharLimit;
this._minifiedCache.set(sourceClient, isMinified);
return isMinified;
}),
/**
* Clears the labels, groups and minify cache, populated by methods like
* SourceUtils.getSourceLabel or Source Utils.getSourceGroup.
* This should be done every time the content location changes.
*/
clearCache: function() {
this._labelsCache.clear();
this._groupsCache.clear();
this._minifiedCache = new WeakMap();
},
/**
* Gets a unique, simplified label from a source url.
*
* @param string aUrl
* The source url.
* @return string
* The simplified label.
*/
getSourceLabel: function(aUrl) {
let cachedLabel = this._labelsCache.get(aUrl);
if (cachedLabel) {
return cachedLabel;
}
let sourceLabel = null;
for (let name of Object.keys(KNOWN_SOURCE_GROUPS)) {
if (aUrl.startsWith(KNOWN_SOURCE_GROUPS[name])) {
sourceLabel = aUrl.substring(KNOWN_SOURCE_GROUPS[name].length);
}
}
if (!sourceLabel) {
sourceLabel = this.trimUrl(aUrl);
}
let unicodeLabel = NetworkHelper.convertToUnicode(unescape(sourceLabel));
this._labelsCache.set(aUrl, unicodeLabel);
return unicodeLabel;
},
/**
* Gets as much information as possible about the hostname and directory paths
* of an url to create a short url group identifier.
*
* @param string aUrl
* The source url.
* @return string
* The simplified group.
*/
getSourceGroup: function(aUrl) {
let cachedGroup = this._groupsCache.get(aUrl);
if (cachedGroup) {
return cachedGroup;
}
try {
// Use an nsIURL to parse all the url path parts.
var uri = Services.io.newURI(aUrl, null, null).QueryInterface(Ci.nsIURL);
} catch (e) {
// This doesn't look like a url, or nsIURL can't handle it.
return "";
}
let groupLabel = uri.prePath;
for (let name of Object.keys(KNOWN_SOURCE_GROUPS)) {
if (aUrl.startsWith(KNOWN_SOURCE_GROUPS[name])) {
groupLabel = name;
}
}
let unicodeLabel = NetworkHelper.convertToUnicode(unescape(groupLabel));
this._groupsCache.set(aUrl, unicodeLabel)
return unicodeLabel;
},
/**
* Trims the url by shortening it if it exceeds a certain length, adding an
* ellipsis at the end.
*
* @param string aUrl
* The source url.
* @param number aLength [optional]
* The expected source url length.
* @param number aSection [optional]
* The section to trim. Supported values: "start", "center", "end"
* @return string
* The shortened url.
*/
trimUrlLength: function(aUrl, aLength, aSection) {
aLength = aLength || SOURCE_URL_DEFAULT_MAX_LENGTH;
aSection = aSection || "end";
if (aUrl.length > aLength) {
switch (aSection) {
case "start":
return L10N.ellipsis + aUrl.slice(-aLength);
break;
case "center":
return aUrl.substr(0, aLength / 2 - 1) + L10N.ellipsis + aUrl.slice(-aLength / 2 + 1);
break;
case "end":
return aUrl.substr(0, aLength) + L10N.ellipsis;
break;
}
}
return aUrl;
},
/**
* Trims the query part or reference identifier of a url string, if necessary.
*
* @param string aUrl
* The source url.
* @return string
* The shortened url.
*/
trimUrlQuery: function(aUrl) {
let length = aUrl.length;
let q1 = aUrl.indexOf('?');
let q2 = aUrl.indexOf('&');
let q3 = aUrl.indexOf('#');
let q = Math.min(q1 != -1 ? q1 : length,
q2 != -1 ? q2 : length,
q3 != -1 ? q3 : length);
return aUrl.slice(0, q);
},
/**
* Trims as much as possible from a url, while keeping the label unique
* in the sources container.
*
* @param string | nsIURL aUrl
* The source url.
* @param string aLabel [optional]
* The resulting label at each step.
* @param number aSeq [optional]
* The current iteration step.
* @return string
* The resulting label at the final step.
*/
trimUrl: function(aUrl, aLabel, aSeq) {
if (!(aUrl instanceof Ci.nsIURL)) {
try {
// Use an nsIURL to parse all the url path parts.
aUrl = Services.io.newURI(aUrl, null, null).QueryInterface(Ci.nsIURL);
} catch (e) {
// This doesn't look like a url, or nsIURL can't handle it.
return aUrl;
}
}
if (!aSeq) {
let name = aUrl.fileName;
if (name) {
// This is a regular file url, get only the file name (contains the
// base name and extension if available).
// If this url contains an invalid query, unfortunately nsIURL thinks
// it's part of the file extension. It must be removed.
aLabel = aUrl.fileName.replace(/\&.*/, "");
} else {
// This is not a file url, hence there is no base name, nor extension.
// Proceed using other available information.
aLabel = "";
}
aSeq = 1;
}
// If we have a label and it doesn't only contain a query...
if (aLabel && aLabel.indexOf("?") != 0) {
// A page may contain multiple requests to the same url but with different
// queries. It is *not* redundant to show each one.
if (!DebuggerView.Sources.getItemForAttachment(e => e.label == aLabel)) {
return aLabel;
}
}
// Append the url query.
if (aSeq == 1) {
let query = aUrl.query;
if (query) {
return this.trimUrl(aUrl, aLabel + "?" + query, aSeq + 1);
}
aSeq++;
}
// Append the url reference.
if (aSeq == 2) {
let ref = aUrl.ref;
if (ref) {
return this.trimUrl(aUrl, aLabel + "#" + aUrl.ref, aSeq + 1);
}
aSeq++;
}
// Prepend the url directory.
if (aSeq == 3) {
let dir = aUrl.directory;
if (dir) {
return this.trimUrl(aUrl, dir.replace(/^\//, "") + aLabel, aSeq + 1);
}
aSeq++;
}
// Prepend the hostname and port number.
if (aSeq == 4) {
let host = aUrl.hostPort;
if (host) {
return this.trimUrl(aUrl, host + "/" + aLabel, aSeq + 1);
}
aSeq++;
}
// Use the whole url spec but ignoring the reference.
if (aSeq == 5) {
return this.trimUrl(aUrl, aUrl.specIgnoringRef, aSeq + 1);
}
// Give up.
return aUrl.spec;
}
};