Skip to content

Commit a1efadf

Browse files
authored
Put the same URL in all "Open" links (#2316)
I find that I use `runnow=1` often and then I use an open link to put a subcase in a new tab. In that new tab I want the same URL options.
1 parent f99f926 commit a1efadf

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/common/runtime/standalone.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ window.onbeforeunload = () => {
2424
return promptBeforeReload ? false : undefined;
2525
};
2626

27+
const kOpenTestLinkAltText = 'Open';
28+
2729
// The possible options for the tests.
2830
interface StandaloneOptions {
2931
runnow: boolean;
@@ -435,7 +437,7 @@ function makeTreeNodeHeaderHTML(
435437
onChange(true);
436438
};
437439

438-
const href = `?${worker ? 'worker&' : ''}${debug ? 'debug&' : ''}q=${n.query.toString()}`;
440+
const href = createSearchQuery([n.query.toString()]);
439441
if (onChange) {
440442
div.on('toggle', function (this) {
441443
onChange((this as HTMLDetailsElement).open);
@@ -458,8 +460,8 @@ function makeTreeNodeHeaderHTML(
458460
$('<a>')
459461
.addClass('nodelink')
460462
.attr('href', href)
461-
.attr('alt', 'Open')
462-
.attr('title', 'Open')
463+
.attr('alt', kOpenTestLinkAltText)
464+
.attr('title', kOpenTestLinkAltText)
463465
.appendTo(header);
464466
if ('testCreationStack' in n && n.testCreationStack) {
465467
$('<button>')
@@ -535,6 +537,18 @@ function prepareParams(params: Record<string, ParamValue>): string {
535537
return new URLSearchParams(pairs).toString();
536538
}
537539

540+
/**
541+
* Given a search query, generates a search parameter string
542+
* @param queries array of queries
543+
* @param params an optional existing search
544+
* @returns a search query string
545+
*/
546+
function createSearchQuery(queries: string[], params?: string) {
547+
params = params === undefined ? prepareParams(optionsToRecord(options)) : params;
548+
// Add in q separately to avoid escaping punctuation marks.
549+
return `?${params}${params ? '&' : ''}${queries.map(q => 'q=' + q).join('&')}`;
550+
}
551+
538552
void (async () => {
539553
const loader = new DefaultTestFileLoader();
540554

@@ -546,14 +560,17 @@ void (async () => {
546560
isFullCTS = qs.length === 1 && qs[0] === rootQuerySpec;
547561

548562
// Update the URL bar to match the exact current options.
549-
const updateURLWithCurrentOptions = () => {
550-
const search = prepareParams(optionsToRecord(options));
563+
const updateURLsWithCurrentOptions = () => {
564+
const params = prepareParams(optionsToRecord(options));
551565
let url = `${window.location.origin}${window.location.pathname}`;
552-
// Add in q separately to avoid escaping punctuation marks.
553-
url += `?${search}${search ? '&' : ''}${qs.map(q => 'q=' + q).join('&')}`;
566+
url += createSearchQuery(qs, params);
554567
window.history.replaceState(null, '', url.toString());
568+
document.querySelectorAll(`a[alt=${kOpenTestLinkAltText}]`).forEach(elem => {
569+
const a = elem as HTMLAnchorElement;
570+
const qs = new URLSearchParams(a.search).getAll('q');
571+
a.search = createSearchQuery(qs, params);
572+
});
555573
};
556-
updateURLWithCurrentOptions();
557574

558575
const addOptionsToPage = (options: StandaloneOptions, optionsInfos: StandaloneOptionsInfos) => {
559576
const optionsElem = $('table#options>tbody')[0];
@@ -565,14 +582,14 @@ void (async () => {
565582
.prop('checked', optionValues[optionName] as boolean)
566583
.on('change', function () {
567584
optionValues[optionName] = (this as HTMLInputElement).checked;
568-
updateURLWithCurrentOptions();
585+
updateURLsWithCurrentOptions();
569586
});
570587
};
571588

572589
const createSelect = (optionName: string, info: StandaloneOptionInfo) => {
573590
const select = $('<select>').on('change', function () {
574591
optionValues[optionName] = (this as HTMLInputElement).value;
575-
updateURLWithCurrentOptions();
592+
updateURLsWithCurrentOptions();
576593
});
577594
const currentValue = optionValues[optionName];
578595
for (const { value, description } of info.selectValueDescriptions!) {

0 commit comments

Comments
 (0)