2018-01-03 03:53:53 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-15 15:34:50 +00:00
|
|
|
import { assert } from './assert';
|
|
|
|
import { helper, debugError, PuppeteerEventListener } from './helper';
|
2020-06-18 14:53:23 +00:00
|
|
|
import Protocol from '../protocol';
|
2020-05-07 10:54:55 +00:00
|
|
|
import { CDPSession } from './Connection';
|
2018-01-03 03:53:53 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
import { EVALUATION_SCRIPT_URL } from './ExecutionContext';
|
2018-07-12 01:38:34 +00:00
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* The CoverageEntry class represents one entry of the coverage report.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface CoverageEntry {
|
|
|
|
/**
|
|
|
|
* The URL of the style sheet or script.
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
url: string;
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* The content of the style sheet or script.
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
text: string;
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* The covered range as start and end positions.
|
|
|
|
*/
|
2020-05-07 10:54:55 +00:00
|
|
|
ranges: Array<{ start: number; end: number }>;
|
2020-04-21 10:45:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* Set of configurable options for JS coverage.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface JSCoverageOptions {
|
|
|
|
/**
|
|
|
|
* Whether to reset coverage on every navigation.
|
|
|
|
*/
|
|
|
|
resetOnNavigation?: boolean;
|
|
|
|
/**
|
|
|
|
* Whether anonymous scripts generated by the page should be reported.
|
|
|
|
*/
|
|
|
|
reportAnonymousScripts?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set of configurable options for CSS coverage.
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface CSSCoverageOptions {
|
|
|
|
/**
|
|
|
|
* Whether to reset coverage on every navigation.
|
|
|
|
*/
|
|
|
|
resetOnNavigation?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Coverage class provides methods to gathers information about parts of
|
|
|
|
* JavaScript and CSS that were used by the page.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* To output coverage in a form consumable by {@link https://github.com/istanbuljs | Istanbul},
|
|
|
|
* see {@link https://github.com/istanbuljs/puppeteer-to-istanbul | puppeteer-to-istanbul}.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* An example of using JavaScript and CSS coverage to get percentage of initially
|
|
|
|
* executed code:
|
|
|
|
* ```js
|
|
|
|
* // Enable both JavaScript and CSS coverage
|
|
|
|
* await Promise.all([
|
|
|
|
* page.coverage.startJSCoverage(),
|
|
|
|
* page.coverage.startCSSCoverage()
|
|
|
|
* ]);
|
|
|
|
* // Navigate to page
|
|
|
|
* await page.goto('https://example.com');
|
|
|
|
* // Disable both JavaScript and CSS coverage
|
|
|
|
* const [jsCoverage, cssCoverage] = await Promise.all([
|
|
|
|
* page.coverage.stopJSCoverage(),
|
|
|
|
* page.coverage.stopCSSCoverage(),
|
|
|
|
* ]);
|
|
|
|
* let totalBytes = 0;
|
|
|
|
* let usedBytes = 0;
|
|
|
|
* const coverage = [...jsCoverage, ...cssCoverage];
|
|
|
|
* for (const entry of coverage) {
|
|
|
|
* totalBytes += entry.text.length;
|
|
|
|
* for (const range of entry.ranges)
|
|
|
|
* usedBytes += range.end - range.start - 1;
|
|
|
|
* }
|
|
|
|
* console.log(`Bytes used: ${usedBytes / totalBytes * 100}%`);
|
|
|
|
* ```
|
|
|
|
* @public
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
export class Coverage {
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
_jsCoverage: JSCoverage;
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
_cssCoverage: CSSCoverage;
|
2018-01-04 02:21:40 +00:00
|
|
|
|
2020-04-21 10:45:29 +00:00
|
|
|
constructor(client: CDPSession) {
|
2018-01-03 03:53:53 +00:00
|
|
|
this._jsCoverage = new JSCoverage(client);
|
2018-01-04 02:21:40 +00:00
|
|
|
this._cssCoverage = new CSSCoverage(client);
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @param options - defaults to
|
|
|
|
* `{ resetOnNavigation : true, reportAnonymousScripts : false }`
|
|
|
|
* @returns Promise that resolves when coverage is started.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* Anonymous scripts are ones that don't have an associated url. These are
|
|
|
|
* scripts that are dynamically created on the page using `eval` or
|
|
|
|
* `new Function`. If `reportAnonymousScripts` is set to `true`, anonymous
|
|
|
|
* scripts will have `__puppeteer_evaluation_script__` as their URL.
|
|
|
|
*/
|
|
|
|
async startJSCoverage(options: JSCoverageOptions = {}): Promise<void> {
|
2018-01-03 03:53:53 +00:00
|
|
|
return await this._jsCoverage.start(options);
|
|
|
|
}
|
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @returns Promise that resolves to the array of coverage reports for
|
|
|
|
* all scripts.
|
|
|
|
*
|
|
|
|
* @remarks
|
|
|
|
* JavaScript Coverage doesn't include anonymous scripts by default.
|
|
|
|
* However, scripts with sourceURLs are reported.
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
async stopJSCoverage(): Promise<CoverageEntry[]> {
|
2018-01-03 03:53:53 +00:00
|
|
|
return await this._jsCoverage.stop();
|
|
|
|
}
|
2018-01-04 02:21:40 +00:00
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @param options - defaults to `{ resetOnNavigation : true }`
|
|
|
|
* @returns Promise that resolves when coverage is started.
|
|
|
|
*/
|
|
|
|
async startCSSCoverage(options: CSSCoverageOptions = {}): Promise<void> {
|
2018-01-04 02:21:40 +00:00
|
|
|
return await this._cssCoverage.start(options);
|
|
|
|
}
|
|
|
|
|
2020-06-29 08:53:28 +00:00
|
|
|
/**
|
|
|
|
* @returns Promise that resolves to the array of coverage reports
|
|
|
|
* for all stylesheets.
|
|
|
|
* @remarks
|
|
|
|
* CSS Coverage doesn't include dynamically injected style tags
|
|
|
|
* without sourceURLs.
|
|
|
|
*/
|
2020-04-21 10:45:29 +00:00
|
|
|
async stopCSSCoverage(): Promise<CoverageEntry[]> {
|
2018-01-04 02:21:40 +00:00
|
|
|
return await this._cssCoverage.stop();
|
|
|
|
}
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class JSCoverage {
|
2020-04-21 10:45:29 +00:00
|
|
|
_client: CDPSession;
|
|
|
|
_enabled = false;
|
|
|
|
_scriptURLs = new Map<string, string>();
|
|
|
|
_scriptSources = new Map<string, string>();
|
|
|
|
_eventListeners: PuppeteerEventListener[] = [];
|
|
|
|
_resetOnNavigation = false;
|
|
|
|
_reportAnonymousScripts = false;
|
|
|
|
|
|
|
|
constructor(client: CDPSession) {
|
2018-01-03 03:53:53 +00:00
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
async start(
|
|
|
|
options: {
|
|
|
|
resetOnNavigation?: boolean;
|
|
|
|
reportAnonymousScripts?: boolean;
|
|
|
|
} = {}
|
|
|
|
): Promise<void> {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!this._enabled, 'JSCoverage is already enabled');
|
2018-11-12 20:59:21 +00:00
|
|
|
const {
|
|
|
|
resetOnNavigation = true,
|
2020-05-07 10:54:55 +00:00
|
|
|
reportAnonymousScripts = false,
|
2018-11-12 20:59:21 +00:00
|
|
|
} = options;
|
|
|
|
this._resetOnNavigation = resetOnNavigation;
|
|
|
|
this._reportAnonymousScripts = reportAnonymousScripts;
|
2018-01-03 03:53:53 +00:00
|
|
|
this._enabled = true;
|
|
|
|
this._scriptURLs.clear();
|
|
|
|
this._scriptSources.clear();
|
|
|
|
this._eventListeners = [
|
2020-05-07 10:54:55 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
this._client,
|
|
|
|
'Debugger.scriptParsed',
|
|
|
|
this._onScriptParsed.bind(this)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._client,
|
|
|
|
'Runtime.executionContextsCleared',
|
|
|
|
this._onExecutionContextsCleared.bind(this)
|
|
|
|
),
|
2018-01-03 03:53:53 +00:00
|
|
|
];
|
|
|
|
await Promise.all([
|
|
|
|
this._client.send('Profiler.enable'),
|
2020-05-07 10:54:55 +00:00
|
|
|
this._client.send('Profiler.startPreciseCoverage', {
|
|
|
|
callCount: false,
|
|
|
|
detailed: true,
|
|
|
|
}),
|
2018-01-03 03:53:53 +00:00
|
|
|
this._client.send('Debugger.enable'),
|
2020-05-07 10:54:55 +00:00
|
|
|
this._client.send('Debugger.setSkipAllPauses', { skip: true }),
|
2018-01-03 03:53:53 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:45:29 +00:00
|
|
|
_onExecutionContextsCleared(): void {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!this._resetOnNavigation) return;
|
2018-01-03 03:53:53 +00:00
|
|
|
this._scriptURLs.clear();
|
|
|
|
this._scriptSources.clear();
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
async _onScriptParsed(
|
|
|
|
event: Protocol.Debugger.scriptParsedPayload
|
|
|
|
): Promise<void> {
|
2018-07-12 01:38:34 +00:00
|
|
|
// Ignore puppeteer-injected scripts
|
2020-05-07 10:54:55 +00:00
|
|
|
if (event.url === EVALUATION_SCRIPT_URL) return;
|
2018-07-12 01:38:34 +00:00
|
|
|
// Ignore other anonymous scripts unless the reportAnonymousScripts option is true.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!event.url && !this._reportAnonymousScripts) return;
|
2018-01-03 03:53:53 +00:00
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
const response = await this._client.send('Debugger.getScriptSource', {
|
|
|
|
scriptId: event.scriptId,
|
|
|
|
});
|
2018-01-03 03:53:53 +00:00
|
|
|
this._scriptURLs.set(event.scriptId, event.url);
|
|
|
|
this._scriptSources.set(event.scriptId, response.scriptSource);
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2018-01-03 03:53:53 +00:00
|
|
|
// This might happen if the page has already navigated away.
|
2020-04-28 13:16:28 +00:00
|
|
|
debugError(error);
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:45:29 +00:00
|
|
|
async stop(): Promise<CoverageEntry[]> {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(this._enabled, 'JSCoverage is not enabled');
|
2018-01-03 03:53:53 +00:00
|
|
|
this._enabled = false;
|
2020-04-21 10:45:29 +00:00
|
|
|
|
|
|
|
const result = await Promise.all<
|
|
|
|
Protocol.Profiler.takePreciseCoverageReturnValue,
|
|
|
|
Protocol.Profiler.stopPreciseCoverageReturnValue,
|
|
|
|
Protocol.Profiler.disableReturnValue,
|
|
|
|
Protocol.Debugger.disableReturnValue
|
|
|
|
>([
|
2018-01-03 03:53:53 +00:00
|
|
|
this._client.send('Profiler.takePreciseCoverage'),
|
|
|
|
this._client.send('Profiler.stopPreciseCoverage'),
|
|
|
|
this._client.send('Profiler.disable'),
|
|
|
|
this._client.send('Debugger.disable'),
|
|
|
|
]);
|
2020-04-21 10:45:29 +00:00
|
|
|
|
2018-01-03 03:53:53 +00:00
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
|
|
|
|
const coverage = [];
|
2020-04-21 10:45:29 +00:00
|
|
|
const profileResponse = result[0];
|
|
|
|
|
2018-01-03 03:53:53 +00:00
|
|
|
for (const entry of profileResponse.result) {
|
2018-07-12 04:05:16 +00:00
|
|
|
let url = this._scriptURLs.get(entry.scriptId);
|
|
|
|
if (!url && this._reportAnonymousScripts)
|
|
|
|
url = 'debugger://VM' + entry.scriptId;
|
2018-01-03 03:53:53 +00:00
|
|
|
const text = this._scriptSources.get(entry.scriptId);
|
2020-05-07 10:54:55 +00:00
|
|
|
if (text === undefined || url === undefined) continue;
|
2018-01-03 03:53:53 +00:00
|
|
|
const flattenRanges = [];
|
2020-05-07 10:54:55 +00:00
|
|
|
for (const func of entry.functions) flattenRanges.push(...func.ranges);
|
2018-01-03 03:53:53 +00:00
|
|
|
const ranges = convertToDisjointRanges(flattenRanges);
|
2020-05-07 10:54:55 +00:00
|
|
|
coverage.push({ url, ranges, text });
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
return coverage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 02:21:40 +00:00
|
|
|
class CSSCoverage {
|
2020-04-21 10:45:29 +00:00
|
|
|
_client: CDPSession;
|
|
|
|
_enabled = false;
|
|
|
|
_stylesheetURLs = new Map<string, string>();
|
|
|
|
_stylesheetSources = new Map<string, string>();
|
|
|
|
_eventListeners: PuppeteerEventListener[] = [];
|
|
|
|
_resetOnNavigation = false;
|
|
|
|
_reportAnonymousScripts = false;
|
|
|
|
|
|
|
|
constructor(client: CDPSession) {
|
2018-01-04 02:21:40 +00:00
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
async start(options: { resetOnNavigation?: boolean } = {}): Promise<void> {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!this._enabled, 'CSSCoverage is already enabled');
|
2020-05-07 10:54:55 +00:00
|
|
|
const { resetOnNavigation = true } = options;
|
2018-11-12 20:59:21 +00:00
|
|
|
this._resetOnNavigation = resetOnNavigation;
|
2018-01-04 02:21:40 +00:00
|
|
|
this._enabled = true;
|
|
|
|
this._stylesheetURLs.clear();
|
|
|
|
this._stylesheetSources.clear();
|
|
|
|
this._eventListeners = [
|
2020-05-07 10:54:55 +00:00
|
|
|
helper.addEventListener(
|
|
|
|
this._client,
|
|
|
|
'CSS.styleSheetAdded',
|
|
|
|
this._onStyleSheet.bind(this)
|
|
|
|
),
|
|
|
|
helper.addEventListener(
|
|
|
|
this._client,
|
|
|
|
'Runtime.executionContextsCleared',
|
|
|
|
this._onExecutionContextsCleared.bind(this)
|
|
|
|
),
|
2018-01-04 02:21:40 +00:00
|
|
|
];
|
|
|
|
await Promise.all([
|
|
|
|
this._client.send('DOM.enable'),
|
|
|
|
this._client.send('CSS.enable'),
|
|
|
|
this._client.send('CSS.startRuleUsageTracking'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:45:29 +00:00
|
|
|
_onExecutionContextsCleared(): void {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!this._resetOnNavigation) return;
|
2018-01-04 02:21:40 +00:00
|
|
|
this._stylesheetURLs.clear();
|
|
|
|
this._stylesheetSources.clear();
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
async _onStyleSheet(
|
|
|
|
event: Protocol.CSS.styleSheetAddedPayload
|
|
|
|
): Promise<void> {
|
2018-01-04 02:21:40 +00:00
|
|
|
const header = event.header;
|
|
|
|
// Ignore anonymous scripts
|
2020-05-07 10:54:55 +00:00
|
|
|
if (!header.sourceURL) return;
|
2018-01-04 02:21:40 +00:00
|
|
|
try {
|
2020-05-07 10:54:55 +00:00
|
|
|
const response = await this._client.send('CSS.getStyleSheetText', {
|
|
|
|
styleSheetId: header.styleSheetId,
|
|
|
|
});
|
2018-01-04 02:21:40 +00:00
|
|
|
this._stylesheetURLs.set(header.styleSheetId, header.sourceURL);
|
|
|
|
this._stylesheetSources.set(header.styleSheetId, response.text);
|
2020-04-28 13:16:28 +00:00
|
|
|
} catch (error) {
|
2018-01-04 02:21:40 +00:00
|
|
|
// This might happen if the page has already navigated away.
|
2020-04-28 13:16:28 +00:00
|
|
|
debugError(error);
|
2018-01-04 02:21:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 10:45:29 +00:00
|
|
|
async stop(): Promise<CoverageEntry[]> {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(this._enabled, 'CSSCoverage is not enabled');
|
2018-01-04 02:21:40 +00:00
|
|
|
this._enabled = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
const ruleTrackingResponse = await this._client.send(
|
|
|
|
'CSS.stopRuleUsageTracking'
|
|
|
|
);
|
2018-10-16 23:55:17 +00:00
|
|
|
await Promise.all([
|
2018-01-04 02:21:40 +00:00
|
|
|
this._client.send('CSS.disable'),
|
|
|
|
this._client.send('DOM.disable'),
|
|
|
|
]);
|
|
|
|
helper.removeEventListeners(this._eventListeners);
|
|
|
|
|
2018-02-09 03:59:46 +00:00
|
|
|
// aggregate by styleSheetId
|
2018-01-04 02:21:40 +00:00
|
|
|
const styleSheetIdToCoverage = new Map();
|
|
|
|
for (const entry of ruleTrackingResponse.ruleUsage) {
|
|
|
|
let ranges = styleSheetIdToCoverage.get(entry.styleSheetId);
|
|
|
|
if (!ranges) {
|
|
|
|
ranges = [];
|
|
|
|
styleSheetIdToCoverage.set(entry.styleSheetId, ranges);
|
|
|
|
}
|
|
|
|
ranges.push({
|
|
|
|
startOffset: entry.startOffset,
|
|
|
|
endOffset: entry.endOffset,
|
|
|
|
count: entry.used ? 1 : 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const coverage = [];
|
|
|
|
for (const styleSheetId of this._stylesheetURLs.keys()) {
|
|
|
|
const url = this._stylesheetURLs.get(styleSheetId);
|
|
|
|
const text = this._stylesheetSources.get(styleSheetId);
|
2020-05-07 10:54:55 +00:00
|
|
|
const ranges = convertToDisjointRanges(
|
|
|
|
styleSheetIdToCoverage.get(styleSheetId) || []
|
|
|
|
);
|
|
|
|
coverage.push({ url, ranges, text });
|
2018-01-04 02:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return coverage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
function convertToDisjointRanges(
|
|
|
|
nestedRanges: Array<{ startOffset: number; endOffset: number; count: number }>
|
|
|
|
): Array<{ start: number; end: number }> {
|
2018-01-03 03:53:53 +00:00
|
|
|
const points = [];
|
|
|
|
for (const range of nestedRanges) {
|
2020-05-07 10:54:55 +00:00
|
|
|
points.push({ offset: range.startOffset, type: 0, range });
|
|
|
|
points.push({ offset: range.endOffset, type: 1, range });
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
// Sort points to form a valid parenthesis sequence.
|
|
|
|
points.sort((a, b) => {
|
|
|
|
// Sort with increasing offsets.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (a.offset !== b.offset) return a.offset - b.offset;
|
2018-01-03 03:53:53 +00:00
|
|
|
// All "end" points should go before "start" points.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (a.type !== b.type) return b.type - a.type;
|
2018-01-03 03:53:53 +00:00
|
|
|
const aLength = a.range.endOffset - a.range.startOffset;
|
|
|
|
const bLength = b.range.endOffset - b.range.startOffset;
|
|
|
|
// For two "start" points, the one with longer range goes first.
|
2020-05-07 10:54:55 +00:00
|
|
|
if (a.type === 0) return bLength - aLength;
|
2018-01-03 03:53:53 +00:00
|
|
|
// For two "end" points, the one with shorter range goes first.
|
|
|
|
return aLength - bLength;
|
|
|
|
});
|
|
|
|
|
|
|
|
const hitCountStack = [];
|
|
|
|
const results = [];
|
|
|
|
let lastOffset = 0;
|
|
|
|
// Run scanning line to intersect all ranges.
|
|
|
|
for (const point of points) {
|
2020-05-07 10:54:55 +00:00
|
|
|
if (
|
|
|
|
hitCountStack.length &&
|
|
|
|
lastOffset < point.offset &&
|
|
|
|
hitCountStack[hitCountStack.length - 1] > 0
|
|
|
|
) {
|
2018-01-03 03:53:53 +00:00
|
|
|
const lastResult = results.length ? results[results.length - 1] : null;
|
|
|
|
if (lastResult && lastResult.end === lastOffset)
|
|
|
|
lastResult.end = point.offset;
|
2020-05-07 10:54:55 +00:00
|
|
|
else results.push({ start: lastOffset, end: point.offset });
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
lastOffset = point.offset;
|
2020-05-07 10:54:55 +00:00
|
|
|
if (point.type === 0) hitCountStack.push(point.range.count);
|
|
|
|
else hitCountStack.pop();
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|
|
|
|
// Filter out empty ranges.
|
2020-05-07 10:54:55 +00:00
|
|
|
return results.filter((range) => range.end - range.start > 1);
|
2018-01-03 03:53:53 +00:00
|
|
|
}
|