tripwire-e2e 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +201 -169
- package/operators/_util.js +14 -1
- package/operators/dead-control.js +23 -23
- package/operators/hide-control.js +3 -3
- package/operators/index.js +81 -77
- package/operators/label-swap.js +1 -1
- package/operators/missing-row-action.js +24 -24
- package/operators/rendered-item-drop.js +31 -0
- package/operators/rendered-item-dup.js +20 -0
- package/operators/rendered-number-drift.js +24 -0
- package/package.json +1 -1
- package/src/agent.js +1 -1
- package/src/browser/mutate.js +156 -113
- package/src/browser/recorder.js +102 -80
- package/src/cli.js +41 -37
- package/src/commands/check.js +24 -7
- package/src/commands/coverage.js +110 -0
- package/src/commands/doctor.js +28 -0
- package/src/commands/fix.js +13 -4
- package/src/commands/run.js +370 -310
- package/src/commands/sabotage.js +85 -74
- package/src/kane.js +158 -150
- package/src/mutant.js +26 -26
- package/src/orphans.js +40 -0
- package/src/profile.js +13 -1
- package/src/proxy.js +292 -289
- package/src/report.js +183 -182
package/operators/index.js
CHANGED
|
@@ -1,77 +1,81 @@
|
|
|
1
|
-
// The operator registry and mutant selection.
|
|
2
|
-
import numberDrift from './number-drift.js';
|
|
3
|
-
import itemDrop from './item-drop.js';
|
|
4
|
-
import itemDup from './item-dup.js';
|
|
5
|
-
import fieldSwap from './field-swap.js';
|
|
6
|
-
import staleRead from './stale-read.js';
|
|
7
|
-
import wrongOwner from './wrong-owner.js';
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
import
|
|
17
|
-
import
|
|
18
|
-
import
|
|
19
|
-
import
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
1
|
+
// The operator registry and mutant selection.
|
|
2
|
+
import numberDrift from './number-drift.js';
|
|
3
|
+
import itemDrop from './item-drop.js';
|
|
4
|
+
import itemDup from './item-dup.js';
|
|
5
|
+
import fieldSwap from './field-swap.js';
|
|
6
|
+
import staleRead from './stale-read.js';
|
|
7
|
+
import wrongOwner from './wrong-owner.js';
|
|
8
|
+
import renderedNumberDrift from './rendered-number-drift.js';
|
|
9
|
+
import renderedItemDrop from './rendered-item-drop.js';
|
|
10
|
+
import renderedItemDup from './rendered-item-dup.js';
|
|
11
|
+
import silentFail from './silent-fail.js';
|
|
12
|
+
import fakeSuccess from './fake-success.js';
|
|
13
|
+
import doubleApply from './double-apply.js';
|
|
14
|
+
import validationOff from './validation-off.js';
|
|
15
|
+
import authDrop from './auth-drop.js';
|
|
16
|
+
import hideControl from './hide-control.js';
|
|
17
|
+
import deadControl from './dead-control.js';
|
|
18
|
+
import labelSwap from './label-swap.js';
|
|
19
|
+
import textChange from './text-change.js';
|
|
20
|
+
import wrongLink from './wrong-link.js';
|
|
21
|
+
import missingRowAction from './missing-row-action.js';
|
|
22
|
+
import agentProposed from './agent-proposed.js';
|
|
23
|
+
|
|
24
|
+
export const OPERATORS = [
|
|
25
|
+
numberDrift, itemDrop, itemDup, fieldSwap, staleRead, wrongOwner, renderedNumberDrift, renderedItemDrop, renderedItemDup,
|
|
26
|
+
silentFail, fakeSuccess, doubleApply, validationOff, authDrop,
|
|
27
|
+
hideControl, deadControl, labelSwap, textChange, wrongLink, missingRowAction,
|
|
28
|
+
agentProposed
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
// Generate every candidate, merge duplicates (same id from several tests), drop excluded equivalents.
|
|
32
|
+
export function generateAll(profile, { dir = '.tripwire', only, excluded = new Set() } = {}) {
|
|
33
|
+
const byId = new Map();
|
|
34
|
+
const counts = {};
|
|
35
|
+
for (const op of OPERATORS) {
|
|
36
|
+
if (only && !only.includes(op.name) && !only.includes(op.family)) continue;
|
|
37
|
+
let made = [];
|
|
38
|
+
try { made = op.generate(profile, { dir }); } catch (err) { made = []; counts[op.name] = { error: err.message }; continue; }
|
|
39
|
+
counts[op.name] = { generated: made.length };
|
|
40
|
+
for (const m of made) {
|
|
41
|
+
if (!m.touches_tests.length) continue;
|
|
42
|
+
if (byId.has(m.id)) {
|
|
43
|
+
const prev = byId.get(m.id);
|
|
44
|
+
prev.touches_tests = [...new Set([...prev.touches_tests, ...m.touches_tests])].sort();
|
|
45
|
+
} else byId.set(m.id, m);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const all = [...byId.values()];
|
|
49
|
+
return { candidates: all.filter((m) => !excluded.has(m.id)), excluded: all.filter((m) => excluded.has(m.id)), counts };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Pick up to max mutants, spread across families, operators and tests (round robin), saboteur first.
|
|
53
|
+
export function select(candidates, max) {
|
|
54
|
+
if (!max || candidates.length <= max) return candidates;
|
|
55
|
+
const groups = new Map();
|
|
56
|
+
for (const m of candidates) {
|
|
57
|
+
const k = m.operator;
|
|
58
|
+
if (!groups.has(k)) groups.set(k, []);
|
|
59
|
+
groups.get(k).push(m);
|
|
60
|
+
}
|
|
61
|
+
// within an operator, alternate between the tests that are touched so no single test dominates
|
|
62
|
+
for (const [k, list] of groups) {
|
|
63
|
+
const byTest = new Map();
|
|
64
|
+
for (const m of list) { const t = m.touches_tests[0]; if (!byTest.has(t)) byTest.set(t, []); byTest.get(t).push(m); }
|
|
65
|
+
const queues = [...byTest.values()];
|
|
66
|
+
const spread = [];
|
|
67
|
+
while (queues.some((q) => q.length)) for (const q of queues) if (q.length) spread.push(q.shift());
|
|
68
|
+
groups.set(k, spread);
|
|
69
|
+
}
|
|
70
|
+
// Saboteur mutants are domain rules written from the acceptance criteria: all of them run first.
|
|
71
|
+
const picked = (groups.get('agent-proposed') || []).splice(0, max);
|
|
72
|
+
const order = [...groups.keys()].filter((k) => k !== 'agent-proposed');
|
|
73
|
+
while (picked.length < max && order.some((k) => groups.get(k).length)) {
|
|
74
|
+
for (const k of order) {
|
|
75
|
+
if (picked.length >= max) break;
|
|
76
|
+
const g = groups.get(k);
|
|
77
|
+
if (g.length) picked.push(g.shift());
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return picked;
|
|
81
|
+
}
|
package/operators/label-swap.js
CHANGED
|
@@ -7,7 +7,7 @@ export default {
|
|
|
7
7
|
family: 'interface',
|
|
8
8
|
summary: 'Two button labels on the same page are exchanged.',
|
|
9
9
|
generate(profile) {
|
|
10
|
-
const buttons = controls(profile).filter((c) => c.role === 'button' && c.text.length <= 40 && !c.pattern);
|
|
10
|
+
const buttons = controls(profile).filter((c) => c.role === 'button' && c.text.length <= 40 && !c.pattern && !c.text.startsWith('['));
|
|
11
11
|
const out = new Map();
|
|
12
12
|
for (const a of buttons) {
|
|
13
13
|
for (const b of buttons) {
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
// Interface: the edit/delete/cancel action is missing from one row of a list or table.
|
|
2
|
-
import { controls, mutant } from './_util.js';
|
|
3
|
-
import { domMutant } from './hide-control.js';
|
|
4
|
-
|
|
5
|
-
export default {
|
|
6
|
-
name: 'missing-row-action',
|
|
7
|
-
family: 'interface',
|
|
8
|
-
summary: 'The delete or edit action is removed from one row.',
|
|
9
|
-
generate(profile) {
|
|
10
|
-
// Row actions are buttons, or links that read like an action (a product name link is data).
|
|
11
|
-
const ACTION = /^(edit|delete|remove|cancel|view|open|details|archive|approve|reject|undo|rename|manage)\b/i;
|
|
12
|
-
return controls(profile)
|
|
13
|
-
.filter((c) => c.inRow && (c.role === 'button' || (c.role === 'link' && ACTION.test(c.text))) && c.text.length <= 30)
|
|
14
|
-
.map((c) => mutant({
|
|
15
|
-
id: `missing-row-action:"${c.text}"`,
|
|
16
|
-
family: 'interface', operator: 'missing-row-action',
|
|
17
|
-
match: { document: true },
|
|
18
|
-
action: domMutant('remove-row-action', c),
|
|
19
|
-
touches_tests: c.tests,
|
|
20
|
-
describe: `The first row
|
|
21
|
-
expect: `The test checks that each row offers "${c.text}"`
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
};
|
|
1
|
+
// Interface: the edit/delete/cancel action is missing from one row of a list or table.
|
|
2
|
+
import { controls, mutant, labelOf } from './_util.js';
|
|
3
|
+
import { domMutant } from './hide-control.js';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
name: 'missing-row-action',
|
|
7
|
+
family: 'interface',
|
|
8
|
+
summary: 'The delete or edit action is removed from one row.',
|
|
9
|
+
generate(profile) {
|
|
10
|
+
// Row actions are buttons, or links that read like an action (a product name link is data).
|
|
11
|
+
const ACTION = /^(edit|delete|remove|cancel|view|open|details|archive|approve|reject|undo|rename|manage)\b/i;
|
|
12
|
+
return controls(profile)
|
|
13
|
+
.filter((c) => c.inRow && (c.role === 'button' || (c.role === 'link' && ACTION.test(c.text))) && c.text.length <= 30)
|
|
14
|
+
.map((c) => mutant({
|
|
15
|
+
id: `missing-row-action:"${c.text}"`,
|
|
16
|
+
family: 'interface', operator: 'missing-row-action',
|
|
17
|
+
match: { document: true },
|
|
18
|
+
action: domMutant('remove-row-action', c),
|
|
19
|
+
touches_tests: c.tests,
|
|
20
|
+
describe: `The first row loses ${labelOf({ ...c, row: undefined })}`,
|
|
21
|
+
expect: `The test checks that each row offers "${c.text}"`
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Data, in the page: a list row the user saw disappears. The in-page form of item-drop, for apps
|
|
2
|
+
// whose lists never travel as JSON (local state, server-rendered pages).
|
|
3
|
+
import { controls, mutant, targetOf } from './_util.js';
|
|
4
|
+
|
|
5
|
+
export function rowLabels(profile) {
|
|
6
|
+
const seen = new Set();
|
|
7
|
+
return controls(profile).filter((c) => {
|
|
8
|
+
if (!c.inRow || !(c.role === 'text' || c.role === 'heading') || c.pattern) return false;
|
|
9
|
+
if (!/[A-Za-z]{2}/.test(c.text) || c.text.startsWith('[') || c.text.length > 60) return false;
|
|
10
|
+
if (seen.has(c.text)) return false;
|
|
11
|
+
seen.add(c.text);
|
|
12
|
+
return true;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default {
|
|
17
|
+
name: 'rendered-item-drop',
|
|
18
|
+
family: 'data',
|
|
19
|
+
summary: 'A list row the user saw disappears from the page.',
|
|
20
|
+
generate(profile) {
|
|
21
|
+
return rowLabels(profile).slice(0, 4).map((c) => mutant({
|
|
22
|
+
id: `rendered-item-drop:"${c.text}"`,
|
|
23
|
+
family: 'data', operator: 'rendered-item-drop',
|
|
24
|
+
match: { document: true },
|
|
25
|
+
action: { type: 'dom', dom: { op: 'hide-row', target: targetOf(c) } },
|
|
26
|
+
touches_tests: c.tests,
|
|
27
|
+
describe: `The "${c.text}" row is missing from the list`,
|
|
28
|
+
expect: 'The test checks which rows are listed, or how many'
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Data, in the page: a list row the user saw is shown twice. The in-page form of item-dup.
|
|
2
|
+
import { mutant, targetOf } from './_util.js';
|
|
3
|
+
import { rowLabels } from './rendered-item-drop.js';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
name: 'rendered-item-dup',
|
|
7
|
+
family: 'data',
|
|
8
|
+
summary: 'A list row the user saw appears twice on the page.',
|
|
9
|
+
generate(profile) {
|
|
10
|
+
return rowLabels(profile).slice(0, 4).map((c) => mutant({
|
|
11
|
+
id: `rendered-item-dup:"${c.text}"`,
|
|
12
|
+
family: 'data', operator: 'rendered-item-dup',
|
|
13
|
+
match: { document: true },
|
|
14
|
+
action: { type: 'dom', dom: { op: 'dup-row', target: targetOf(c) } },
|
|
15
|
+
touches_tests: c.tests,
|
|
16
|
+
describe: `The "${c.text}" row appears twice in the list`,
|
|
17
|
+
expect: 'The test checks how many rows are listed'
|
|
18
|
+
}));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Data, in the page: a number the user sees (a count, an amount) is off by one, changed in the
|
|
2
|
+
// rendered page itself. Covers apps whose data never travels as JSON: server-rendered pages,
|
|
3
|
+
// local state, websockets. On JSON apps it complements number-drift.
|
|
4
|
+
import { controls, mutant, targetOf } from './_util.js';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
name: 'rendered-number-drift',
|
|
8
|
+
family: 'data',
|
|
9
|
+
summary: 'A number shown on the page (a count, an amount) is off by one, changed in the page itself.',
|
|
10
|
+
generate(profile) {
|
|
11
|
+
return controls(profile)
|
|
12
|
+
.filter((c) => c.role === 'text' && (c.pattern || /\d/.test(c.text)) && c.text.length <= 40)
|
|
13
|
+
.slice(0, 8)
|
|
14
|
+
.map((c) => mutant({
|
|
15
|
+
id: `rendered-number-drift:"${c.text}"`,
|
|
16
|
+
family: 'data', operator: 'rendered-number-drift',
|
|
17
|
+
match: { document: true },
|
|
18
|
+
action: { type: 'dom', dom: { op: 'drift-number', target: targetOf(c) } },
|
|
19
|
+
touches_tests: c.tests,
|
|
20
|
+
describe: `The number in "${c.ctx || c.text}" is off by one`,
|
|
21
|
+
expect: 'The test checks the exact number shown, not just that the element is there'
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tripwire-e2e",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Mutation testing for Kane CLI browser suites: a proxy breaks the app one realistic way at a time, Kane replays the suite, and survivors go to your coding agent.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/agent.js
CHANGED
|
@@ -17,7 +17,7 @@ export function runAgent(name, prompt, { cwd = process.cwd(), timeoutMs = 600000
|
|
|
17
17
|
const { cmd, args, raw } = agentCommand(name);
|
|
18
18
|
return new Promise((resolve) => {
|
|
19
19
|
const shell = process.platform === 'win32' || raw;
|
|
20
|
-
const child = spawn(shell ? [cmd, ...args].join(' ') : cmd, shell ? [] : args, { cwd, shell, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true });
|
|
20
|
+
const child = spawn(shell ? [cmd, ...args].join(' ') : cmd, shell ? [] : args, { cwd, shell, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true, env: { ...process.env, TRIPWIRE_AGENT: '1' } });
|
|
21
21
|
let out = '';
|
|
22
22
|
let err = '';
|
|
23
23
|
child.stdout.on('data', (c) => (out += c));
|
package/src/browser/mutate.js
CHANGED
|
@@ -1,113 +1,156 @@
|
|
|
1
|
-
// Injected by the Tripwire proxy when an interface mutant is armed.
|
|
2
|
-
// window.__TW_MUTANT = { id, op, target: { role, text, count? }, ... } is set just before this script.
|
|
3
|
-
// The change is re-applied whenever the page re-renders, and the proxy is told once it really fired.
|
|
4
|
-
(function () {
|
|
5
|
-
var M = window.__TW_MUTANT;
|
|
6
|
-
if (!M || window.__twMutating) return;
|
|
7
|
-
window.__twMutating = true;
|
|
8
|
-
var fired = false;
|
|
9
|
-
var clean = function (s) { return (s || '').replace(/\s+/g, ' ').trim(); };
|
|
10
|
-
var ROLE_SEL = {
|
|
11
|
-
button: 'button, [role=button], input[type=submit], input[type=button]',
|
|
12
|
-
link: 'a[href]',
|
|
13
|
-
heading: 'h1, h2, h3',
|
|
14
|
-
text: '[role=status], .status, .badge, .tag, strong, p, span, td, dd, h1, h2, h3'
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
fired
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
el.
|
|
51
|
-
report();
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
'
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
el.
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
1
|
+
// Injected by the Tripwire proxy when an interface mutant is armed.
|
|
2
|
+
// window.__TW_MUTANT = { id, op, target: { role, text, count? }, ... } is set just before this script.
|
|
3
|
+
// The change is re-applied whenever the page re-renders, and the proxy is told once it really fired.
|
|
4
|
+
(function () {
|
|
5
|
+
var M = window.__TW_MUTANT;
|
|
6
|
+
if (!M || window.__twMutating) return;
|
|
7
|
+
window.__twMutating = true;
|
|
8
|
+
var fired = false;
|
|
9
|
+
var clean = function (s) { return (s || '').replace(/\s+/g, ' ').trim(); };
|
|
10
|
+
var ROLE_SEL = {
|
|
11
|
+
button: 'button, [role=button], input[type=submit], input[type=button]',
|
|
12
|
+
link: 'a[href]',
|
|
13
|
+
heading: 'h1, h2, h3',
|
|
14
|
+
text: '[role=status], .status, .badge, .tag, strong, label, p, span, td, dd, h1, h2, h3',
|
|
15
|
+
checkbox: 'input[type=checkbox]'
|
|
16
|
+
};
|
|
17
|
+
function report() {
|
|
18
|
+
if (fired) return;
|
|
19
|
+
fired = true;
|
|
20
|
+
try { navigator.sendBeacon('/_tw/fired', new Blob([JSON.stringify({ id: M.id })], { type: 'application/json' })); } catch (e) { /* ignore */ }
|
|
21
|
+
}
|
|
22
|
+
function find(target) {
|
|
23
|
+
// "[tag.class]" targets a control that has no visible text
|
|
24
|
+
var anon = /^\[([a-z0-9-]+)(?:\.([\w-]+))?\]$/i.exec(target.text || '');
|
|
25
|
+
if (anon) {
|
|
26
|
+
var els = Array.prototype.slice.call(document.querySelectorAll(anon[1] + (anon[2] ? '.' + anon[2] : '')));
|
|
27
|
+
return target.count ? (els[target.count - 1] ? [els[target.count - 1]] : []) : els;
|
|
28
|
+
}
|
|
29
|
+
var all = Array.prototype.slice.call(document.querySelectorAll(ROLE_SEL[target.role] || ROLE_SEL.text));
|
|
30
|
+
var re = target.pattern ? new RegExp(target.pattern) : null;
|
|
31
|
+
var hits = all.filter(function (el) {
|
|
32
|
+
var t = clean(el.innerText || el.value);
|
|
33
|
+
return re ? re.test(t) : t === target.text;
|
|
34
|
+
});
|
|
35
|
+
if (target.role === 'text') hits = hits.filter(function (el) { return !hits.some(function (o) { return o !== el && el.contains(o); }); });
|
|
36
|
+
if (target.count) return hits[target.count - 1] ? [hits[target.count - 1]] : [];
|
|
37
|
+
return hits;
|
|
38
|
+
}
|
|
39
|
+
function setText(el, text) {
|
|
40
|
+
if (el.tagName === 'INPUT') { el.value = text; return; }
|
|
41
|
+
// keep child elements (icons, badges) when the label is a single text node
|
|
42
|
+
var nodes = Array.prototype.filter.call(el.childNodes, function (n) { return n.nodeType === 3 && clean(n.nodeValue); });
|
|
43
|
+
if (nodes.length === 1 && el.children.length) nodes[0].nodeValue = text; else el.textContent = text;
|
|
44
|
+
}
|
|
45
|
+
var ops = {
|
|
46
|
+
'hide': function () {
|
|
47
|
+
find(M.target).forEach(function (el) {
|
|
48
|
+
if (el.dataset.twDone) return;
|
|
49
|
+
el.dataset.twDone = '1';
|
|
50
|
+
el.style.setProperty('display', 'none', 'important');
|
|
51
|
+
report();
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
'dead': function () {
|
|
55
|
+
find(M.target).forEach(function (el) {
|
|
56
|
+
if (el.dataset.twDone) return;
|
|
57
|
+
el.dataset.twDone = '1';
|
|
58
|
+
report();
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
'swap-labels': function () {
|
|
62
|
+
var a = find(M.target), b = find(M.other);
|
|
63
|
+
a.forEach(function (el) { if (!el.dataset.twDone) { el.dataset.twDone = '1'; setText(el, M.other.text); report(); } });
|
|
64
|
+
b.forEach(function (el) { if (!el.dataset.twDone) { el.dataset.twDone = '1'; setText(el, M.target.text); report(); } });
|
|
65
|
+
},
|
|
66
|
+
'replace-text': function () {
|
|
67
|
+
find(M.target).forEach(function (el) {
|
|
68
|
+
if (el.dataset.twDone) return;
|
|
69
|
+
el.dataset.twDone = '1';
|
|
70
|
+
setText(el, M.replacement);
|
|
71
|
+
report();
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
'relink': function () {
|
|
75
|
+
find(M.target).forEach(function (el) {
|
|
76
|
+
if (el.dataset.twDone) return;
|
|
77
|
+
el.dataset.twDone = '1';
|
|
78
|
+
el.setAttribute('href', M.href);
|
|
79
|
+
report();
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
// A number shown on the page is off by one (works without any JSON API: SSR, local state).
|
|
83
|
+
// Re-applied after every re-render, but never twice to the same rendered value.
|
|
84
|
+
'drift-number': function () {
|
|
85
|
+
find(M.target).forEach(function (el) {
|
|
86
|
+
var t = clean(el.innerText || el.value);
|
|
87
|
+
if (el.dataset.twWrote === t) return;
|
|
88
|
+
// +1 unit, keeping the decimals ("2" -> "3", "18.00" -> "19.00")
|
|
89
|
+
var next = t.replace(/\d+(?:\.\d+)?/, function (n) {
|
|
90
|
+
return (Number(n) + 1).toFixed((n.split('.')[1] || '').length);
|
|
91
|
+
});
|
|
92
|
+
if (next === t) return;
|
|
93
|
+
setText(el, next);
|
|
94
|
+
el.dataset.twWrote = clean(el.innerText || el.value);
|
|
95
|
+
report();
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
// A list row the user saw disappears, or shows twice (the in-page form of item-drop/item-dup).
|
|
99
|
+
'hide-row': function () {
|
|
100
|
+
var el = find(M.target)[0];
|
|
101
|
+
var row = el && el.closest('li, tr');
|
|
102
|
+
if (!row || row.dataset.twDone) return;
|
|
103
|
+
row.dataset.twDone = '1';
|
|
104
|
+
row.style.setProperty('display', 'none', 'important');
|
|
105
|
+
report();
|
|
106
|
+
},
|
|
107
|
+
'dup-row': function () {
|
|
108
|
+
var el = find(M.target)[0];
|
|
109
|
+
var row = el && el.closest('li, tr');
|
|
110
|
+
if (!row || row.dataset.twDone) return;
|
|
111
|
+
row.dataset.twDone = '1';
|
|
112
|
+
var copy = row.cloneNode(true);
|
|
113
|
+
row.parentNode.insertBefore(copy, row.nextSibling);
|
|
114
|
+
report();
|
|
115
|
+
},
|
|
116
|
+
'remove-row-action': function () {
|
|
117
|
+
// the same lookup as everywhere else (text, pattern or "[tag.class]"), kept to the first row
|
|
118
|
+
var matching = find({ role: M.target.role, text: M.target.text, pattern: M.target.pattern });
|
|
119
|
+
var rows = document.querySelectorAll('tr, li');
|
|
120
|
+
for (var i = 0; i < rows.length; i++) {
|
|
121
|
+
var els = matching.filter(function (el) { return rows[i].contains(el); });
|
|
122
|
+
if (!els.length) continue;
|
|
123
|
+
if (rows[i].dataset.twDone) return;
|
|
124
|
+
rows[i].dataset.twDone = '1';
|
|
125
|
+
els.forEach(function (el) { el.remove(); });
|
|
126
|
+
report();
|
|
127
|
+
return; // first matching row only
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
if (M.op === 'dead') {
|
|
132
|
+
// swallow clicks on the dead control before the app's own handlers run
|
|
133
|
+
var swallow = function (ev) {
|
|
134
|
+
var el = ev.target && ev.target.closest && ev.target.closest('[data-tw-done]');
|
|
135
|
+
if (el && find(M.target).indexOf(el) !== -1) { ev.preventDefault(); ev.stopImmediatePropagation(); }
|
|
136
|
+
};
|
|
137
|
+
window.addEventListener('click', swallow, true);
|
|
138
|
+
window.addEventListener('submit', function (ev) {
|
|
139
|
+
var btn = ev.submitter;
|
|
140
|
+
if (btn && btn.dataset && btn.dataset.twDone) { ev.preventDefault(); ev.stopImmediatePropagation(); }
|
|
141
|
+
}, true);
|
|
142
|
+
}
|
|
143
|
+
var apply = ops[M.op];
|
|
144
|
+
if (!apply) return;
|
|
145
|
+
var busy = false;
|
|
146
|
+
var run = function () {
|
|
147
|
+
if (busy) return;
|
|
148
|
+
busy = true;
|
|
149
|
+
try { apply(); } finally { busy = false; }
|
|
150
|
+
};
|
|
151
|
+
var start = function () {
|
|
152
|
+
run();
|
|
153
|
+
new MutationObserver(run).observe(document.documentElement, { childList: true, subtree: true, characterData: true });
|
|
154
|
+
};
|
|
155
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start); else start();
|
|
156
|
+
})();
|