Skip to content

Commit ca4baff

Browse files
committed
Fix option ingredients being overwriten during pageload
1 parent 9088561 commit ca4baff

3 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/web/HTMLIngredient.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class HTMLIngredient {
166166
id="${this.id}"
167167
tabindex="${this.tabIndex}"
168168
arg-name="${this.name}"
169+
data-target="${this.target}"
169170
${this.disabled ? "disabled" : ""}>`;
170171
for (i = 0; i < this.value.length; i++) {
171172
if ((m = this.value[i].name.match(/\[([a-z0-9 -()^]+)\]/i))) {

src/web/waiters/RecipeWaiter.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,11 +487,19 @@ class RecipeWaiter {
487487
* @param {HTMLElement} op
488488
*/
489489
triggerArgEvents(op) {
490-
// Trigger populateOption and argSelector events
490+
// Trigger argSelector events and populateOption events only where the target is empty.
491+
// When loading a saved recipe, arguments are populated before this method is called, so
492+
// re-triggering populateOption events would overwrite saved custom values with defaults.
493+
const args = op.querySelectorAll(".arg");
491494
const triggerableOptions = op.querySelectorAll(".populate-option, .arg-selector");
492495
const evt = new Event("change", {bubbles: true});
496+
493497
if (triggerableOptions.length) {
494498
for (const el of triggerableOptions) {
499+
if (el.classList.contains("populate-option")) {
500+
const target = args[el.getAttribute("data-target")];
501+
if (target && target.value !== "") continue;
502+
}
495503
el.dispatchEvent(evt);
496504
}
497505
}

tests/browser/03_recipe_load.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Regression tests for recipe loading behaviour.
3+
*
4+
* @author CyberChef contributors
5+
* @copyright Crown Copyright
6+
* @license Apache-2.0
7+
*/
8+
9+
const utils = require("./browserUtils.js");
10+
11+
module.exports = {
12+
before: browser => {
13+
browser
14+
.resizeWindow(1280, 800)
15+
.url(browser.launchUrl)
16+
.useCss()
17+
.waitForElementNotPresent("#preloader", 10000);
18+
},
19+
20+
"Recipe load preserves populated arguments": browser => {
21+
const inputFormat = "HH:mm:ss a MMM DD, YYYY ";
22+
const input = "10:20:30 pm Sep 26, 2019 ";
23+
24+
utils.loadRecipe(
25+
browser,
26+
"Translate DateTime Format",
27+
input,
28+
[
29+
"Standard date and time",
30+
inputFormat,
31+
"UTC",
32+
"DD/MM/YYYY HH:mm:ss",
33+
"UTC"
34+
]
35+
);
36+
37+
browser.execute(() => {
38+
return Array.from(document.querySelectorAll("#rec-list li.operation .arg"))
39+
.map(arg => arg.value);
40+
}, [], function({value}) {
41+
browser.expect(value[1]).to.equal(inputFormat);
42+
});
43+
},
44+
45+
after: browser => {
46+
browser.end();
47+
}
48+
};

0 commit comments

Comments
 (0)