Skip to content

Commit 9cb7d73

Browse files
committed
iss1757 - Pass back 'ERROR' from extractors if nothing found.
1 parent aae546d commit 9cb7d73

10 files changed

Lines changed: 50 additions & 62 deletions

File tree

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Extractor: finalfunction
22
// Scans blocks (bottom-up) for the last code_inline or asciimath_block line
3-
// matching f(x) = <expr>, then sets answerEl.value to the extracted expression.
4-
export default function finalfunction(raw, answerEl, blocks) {
3+
// matching f(x) = <expr>, then returns the extracted expression.
4+
export default function finalfunction(raw, blocks) {
55
const fxPattern = /^f\(x\)\s*=\s*/;
66
const fxPatternRaw = /^`*f\(x\)\s*=\s*/;
77

@@ -11,24 +11,20 @@ export default function finalfunction(raw, answerEl, blocks) {
1111
if (block.type === 'code_inline') {
1212
const trimmed = block.raw.trim();
1313
if (fxPattern.test(trimmed)) {
14-
answerEl.value = trimmed.replace(fxPattern, '');
15-
answerEl.dispatchEvent(new Event('change'));
16-
return;
14+
return trimmed.replace(fxPattern, '');
1715
}
1816
}
1917
if (block.type === 'asciimath_block') {
2018
const lines = block.raw.split(/\r?\n/);
2119
for (let j = lines.length - 1; j >= 0; j--) {
2220
const trimmed = lines[j].trim();
2321
if (fxPattern.test(trimmed)) {
24-
answerEl.value = trimmed.replace(fxPattern, '');
25-
answerEl.dispatchEvent(new Event('change'));
26-
return;
22+
return trimmed.replace(fxPattern, '');
2723
}
2824
}
2925
}
3026
}
31-
return;
27+
return 'ERROR';
3228
}
3329

3430
// Fallback: raw-text parsing when blocks are unavailable.
@@ -37,9 +33,8 @@ export default function finalfunction(raw, answerEl, blocks) {
3733
for (const line of lines) {
3834
const trimmed = line.trim();
3935
if (fxPatternRaw.test(trimmed)) {
40-
answerEl.value = trimmed.replace(/^`*f\(x\)\s*=\s*|`+$/g, '');
41-
answerEl.dispatchEvent(new Event('change'));
42-
return;
36+
return trimmed.replace(/^`*f\(x\)\s*=\s*|`+$/g, '');
4337
}
4438
}
39+
return 'ERROR';
4540
}

corsscripts/ascii/extractors/lastblock.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,27 @@
22
// Returns the raw content of the last code_inline, or the full trimmed content
33
// of the last asciimath_block, in document order.
44
// Falls back to the final non-empty line of raw when no blocks are available.
5-
export default function lastblock(raw, answerEl, blocks) {
5+
export default function lastblock(raw, blocks) {
66
if (blocks && blocks.length > 0) {
77
for (let i = blocks.length - 1; i >= 0; i--) {
88
const block = blocks[i];
99
if (block.type === 'code_inline') {
10-
answerEl.value = block.raw;
11-
answerEl.dispatchEvent(new Event('change'));
12-
return;
10+
return block.raw;
1311
}
1412
if (block.type === 'asciimath_block') {
15-
answerEl.value = block.raw;
16-
answerEl.dispatchEvent(new Event('change'));
17-
return;
13+
return block.raw;
1814
}
1915
}
20-
return;
16+
return 'ERROR';
2117
}
2218

2319
// Fallback: send the final non-empty line when blocks are unavailable.
2420
const lines = raw.split(/\r?\n/);
2521
for (let i = lines.length - 1; i >= 0; i--) {
2622
const trimmed = lines[i].trim();
2723
if (trimmed !== '') {
28-
answerEl.value = trimmed;
29-
answerEl.dispatchEvent(new Event('change'));
30-
return;
24+
return trimmed;
3125
}
3226
}
27+
return 'ERROR';
3328
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Extractor: lastcalc
2-
// Sets answerEl to the raw content of the last calculation block.
3-
export default function lastcalc(raw, answerEl, blocks) {
2+
// Returns the raw content of the last calculation block.
3+
export default function lastcalc(raw, blocks) {
44
if (blocks) {
55
for (let i = blocks.length - 1; i >= 0; i--) {
66
if (blocks[i].type === 'calculation') {
7-
answerEl.value = blocks[i].raw;
8-
answerEl.dispatchEvent(new Event('change'));
9-
return;
7+
return blocks[i].raw;
108
}
119
}
1210
}
11+
return 'ERROR';
1312
}

corsscripts/ascii/extractors/lastexpr.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,33 @@
22
// Returns the raw content of the last code_inline, or the last non-empty line
33
// of the last asciimath_block, in document order.
44
// Falls back to the final non-empty line of raw when no blocks are available.
5-
export default function lastexpr(raw, answerEl, blocks) {
5+
export default function lastexpr(raw, blocks) {
66
if (blocks && blocks.length > 0) {
77
for (let i = blocks.length - 1; i >= 0; i--) {
88
const block = blocks[i];
99
if (block.type === 'code_inline') {
10-
answerEl.value = block.raw;
11-
answerEl.dispatchEvent(new Event('change'));
12-
return;
10+
return block.raw;
1311
}
1412
if (block.type === 'asciimath_block') {
1513
const lines = block.raw.split(/\r?\n/);
1614
for (let j = lines.length - 1; j >= 0; j--) {
1715
const trimmed = lines[j].trim();
1816
if (trimmed !== '') {
19-
answerEl.value = trimmed;
20-
answerEl.dispatchEvent(new Event('change'));
21-
return;
17+
return trimmed;
2218
}
2319
}
2420
}
2521
}
26-
return;
22+
return 'ERROR';
2723
}
2824

2925
// Fallback: send the final non-empty line when blocks are unavailable.
3026
const lines = raw.split(/\r?\n/);
3127
for (let i = lines.length - 1; i >= 0; i--) {
3228
const trimmed = lines[i].trim();
3329
if (trimmed !== '') {
34-
answerEl.value = trimmed;
35-
answerEl.dispatchEvent(new Event('change'));
36-
return;
30+
return trimmed;
3731
}
3832
}
33+
return 'ERROR';
3934
}

corsscripts/ascii/extractors/regexall.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// [[extractor targetinput="ans2" type="regexall" regex="^f\\(x\\)\\s*=\\s*" /]]
33
// Searches the entire raw input for all lines matching entry.regex and returns
44
// a JSON object of the form {"matches":[...]} set as answerEl.value.
5-
export default function regexall(raw, answerEl, blocks, entry) {
5+
export default function regexall(raw, blocks, entry) {
66
if (!entry || !entry.regex) {
77
return;
88
}
@@ -16,6 +16,8 @@ export default function regexall(raw, answerEl, blocks, entry) {
1616
}
1717
}
1818

19-
answerEl.value = JSON.stringify({ matches });
20-
answerEl.dispatchEvent(new Event('change'));
19+
if (matches.length === 0) {
20+
return 'ERROR';
21+
}
22+
return JSON.stringify({ matches });
2123
}

corsscripts/ascii/extractors/regexmatch.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// there can be any amount of whitespace around the equals. Returns 'f(x) = expr'.
55
// Scans blocks (bottom-up) for the last code_inline or asciimath_block line
66
// matching entry.regex, then sets answerEl.value to the matched string.
7-
export default function regexmatch(raw, answerEl, blocks, entry) {
7+
export default function regexmatch(raw, blocks, entry) {
88
if (!entry || !entry.regex) {
99
return;
1010
}
@@ -16,24 +16,20 @@ export default function regexmatch(raw, answerEl, blocks, entry) {
1616
if (block.type === 'code_inline') {
1717
const trimmed = block.raw.trim();
1818
if (pattern.test(trimmed)) {
19-
answerEl.value = trimmed;
20-
answerEl.dispatchEvent(new Event('change'));
21-
return;
19+
return trimmed;
2220
}
2321
}
2422
if (block.type === 'asciimath_block') {
2523
const lines = block.raw.split(/\r?\n/);
2624
for (let j = lines.length - 1; j >= 0; j--) {
2725
const trimmed = lines[j].trim();
2826
if (pattern.test(trimmed)) {
29-
answerEl.value = trimmed;
30-
answerEl.dispatchEvent(new Event('change'));
31-
return;
27+
return trimmed;
3228
}
3329
}
3430
}
3531
}
36-
return;
32+
return 'ERROR';
3733
}
3834

3935
// Fallback: raw-text parsing when blocks are unavailable.
@@ -42,9 +38,8 @@ export default function regexmatch(raw, answerEl, blocks, entry) {
4238
for (const line of lines) {
4339
const trimmed = line.trim();
4440
if (pattern.test(trimmed)) {
45-
answerEl.value = trimmed;
46-
answerEl.dispatchEvent(new Event('change'));
47-
return;
41+
return trimmed;
4842
}
4943
}
44+
return 'ERROR';
5045
}

0 commit comments

Comments
 (0)