Skip to content

Commit ca83c21

Browse files
Merge pull request #121 from moormaster/fix-check-if-full-line-matches
fix: enforce full line to match when reference regex has the form ^...$
2 parents 4bcf7e1 + 3abcf4b commit ca83c21

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

_data/localization.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@
8989
"de": "✓ gefunden",
9090
"sv": "✓ matchade",
9191
"pt-br": "✓ corresponde"
92-
92+
},
93+
"matchedIncompletely": {
94+
"en": "(✓) matched, but whole line should match",
95+
"de": "(✓) gefunden, aber die ganze Zeile soll passen",
96+
"sv": "(✓) matchade, men hela raden ska matcha",
97+
"pt-br": "(✓) corresponde, mas toda a linha deve corresponder"
9398
},
9499
"shouldNotMatch": {
95100
"en": "✓ matched, but should not match",

_layouts/tutorial.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
content: "{{ site.data.localization.hint.hidden[page.lang] }}";
3636
}
3737

38+
.playfield.verbose .incompletematch.ok:after {
39+
content: "{{ site.data.localization.match.matchedIncompletely[page.lang] }}";
40+
}
41+
3842
.playfield.verbose .match.ok:after {
3943
content: "{{ site.data.localization.match.matched[page.lang] }}";
4044
}

css/playfield.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151

5252
.playfield .nomatch {
5353
}
54+
.playfield .incompletematch.ok {
55+
background-color: {{ site.data.colors.fails }};
56+
}
5457
.playfield .nomatch.ok {
5558
background-color: {{ site.data.colors.fails }};
5659
}

de/05-02.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Der nächste Ausdruck soll die Zahlen 20 bis 29 finden aber er tut es nicht.
66
Magst du ihn ändern, dass er nur die Zahlen zwischen 20 und 29 erkennt?
77
</p>
8-
<div class="playfield">
9-
<input type="text" value="[20-29]" class="regex" reference="2[0-9]">
8+
<div class="playfield verbose">
9+
<input type="text" value="[20-29]" class="regex" reference="^2[0-9]$">
1010
<div class="message"></div>
1111
<ul class="examples">
1212
<li>12</li>

en/05-02.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
The following expression shall find the numbers 20 to 29 but it has mistakes.
66
Would you like to change it so it matches only the number from 20 to 29?
77
</p>
8-
<div class="playfield">
9-
<input type="text" value="[20-29]" class="regex" reference="2[0-9]">
8+
<div class="playfield verbose">
9+
<input type="text" value="[20-29]" class="regex" reference="^2[0-9]$">
1010
<div class="message"></div>
1111
<ul class="examples">
1212
<li>12</li>

js/playfield.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function nomatchTextElement(string) {
2222
return span;
2323
}
2424

25-
function matchExample(match, example) {
25+
function matchExample(match, isIncomplete, text, example) {
2626
var text = example.innerText;
2727
var start = match.index;
2828
var stop = match.index + match[0].length;
@@ -34,14 +34,23 @@ function matchExample(match, example) {
3434
example.appendChild(nomatchTextElement(textBefore));
3535
example.appendChild(matchTextElement(textMatch));
3636
example.appendChild(nomatchTextElement(textAfter));
37-
example.classList.add("match");
37+
38+
if (!isIncomplete) {
39+
example.classList.remove("incompletematch");
40+
example.classList.add("match");
41+
} else {
42+
example.classList.remove("match");
43+
example.classList.add("incompletematch");
44+
}
45+
3846
example.classList.remove("nomatch");
3947
}
4048

4149
function unmatchExample(example) {
4250
var text = example.innerText;
4351
example.innerHTML = "";
4452
example.appendChild(nomatchTextElement(text));
53+
example.classList.remove("incompletematch");
4554
example.classList.remove("match");
4655
example.classList.add("nomatch");
4756
}
@@ -57,10 +66,13 @@ function watchExpression(playfield, examples, regex, message) {
5766
return null;
5867
}
5968
}
60-
function getReferenceRegex() {
69+
function getReferenceInfo() {
6170
var reference = regex.getAttribute("reference");
6271
try {
63-
return RegExp(reference);
72+
return {
73+
"shouldMatchWholeLine": reference.startsWith('^') && reference.endsWith('$'),
74+
"regex": RegExp(reference)
75+
};
6476
} catch (err) {
6577
message.innerHTML = translateReferenceWrongErrorMessage(err.message);
6678
return null;
@@ -69,9 +81,9 @@ function watchExpression(playfield, examples, regex, message) {
6981
function check() {
7082
updateExperiment();
7183
playfield.classList.remove("success");
72-
var reference = getReferenceRegex();
84+
var referenceInfo = getReferenceInfo();
7385
var exp = getExpression();
74-
if (!exp || !reference) {
86+
if (!exp || !referenceInfo) {
7587
return;
7688
}
7789
message.innerHTML = "";
@@ -83,7 +95,13 @@ function watchExpression(playfield, examples, regex, message) {
8395
var example = example_list[i];
8496
var text = example.innerText;
8597
// determine if it should match
86-
var shouldNotMatch = reference.exec(text) ? false : true;
98+
var shouldNotMatch;
99+
if (!referenceInfo.shouldMatchWholeLine) {
100+
shouldNotMatch = referenceInfo.regex.exec(text) ? false : true;
101+
} else {
102+
var matches = text.match(referenceInfo.regex);
103+
shouldNotMatch = matches?.length > 0 && matches[0] == text ? false : true;
104+
}
87105
if (shouldNotMatch) {
88106
example.classList.add("fail");
89107
example.classList.remove("ok");
@@ -94,7 +112,15 @@ function watchExpression(playfield, examples, regex, message) {
94112
// check the match
95113
var match = exp.exec(text);
96114
if (match) {
97-
matchExample(match, example);
115+
var matches = text.match(exp);
116+
var isIncomplete =
117+
referenceInfo.shouldMatchWholeLine
118+
&& matches?.length > 0 && matches[0] != text;
119+
120+
matchExample(match, isIncomplete, text, example);
121+
122+
// enforce overall result to fail if any match is incomplete
123+
match = !isIncomplete;
98124
} else {
99125
unmatchExample(example);
100126
}
@@ -130,7 +156,7 @@ function watchExpression(playfield, examples, regex, message) {
130156
var match = exp.exec(text);
131157
if (match) {
132158
experiment.classList.add("match");
133-
matchExample(match, content);
159+
matchExample(match, false, text, content);
134160
} else {
135161
experiment.classList.add("nomatch");
136162
unmatchExample(content);

0 commit comments

Comments
 (0)