@@ -22,7 +22,7 @@ function nomatchTextElement(string) {
22
22
return span ;
23
23
}
24
24
25
- function matchExample ( match , example ) {
25
+ function matchExample ( match , isIncomplete , text , example ) {
26
26
var text = example . innerText ;
27
27
var start = match . index ;
28
28
var stop = match . index + match [ 0 ] . length ;
@@ -34,14 +34,23 @@ function matchExample(match, example) {
34
34
example . appendChild ( nomatchTextElement ( textBefore ) ) ;
35
35
example . appendChild ( matchTextElement ( textMatch ) ) ;
36
36
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
+
38
46
example . classList . remove ( "nomatch" ) ;
39
47
}
40
48
41
49
function unmatchExample ( example ) {
42
50
var text = example . innerText ;
43
51
example . innerHTML = "" ;
44
52
example . appendChild ( nomatchTextElement ( text ) ) ;
53
+ example . classList . remove ( "incompletematch" ) ;
45
54
example . classList . remove ( "match" ) ;
46
55
example . classList . add ( "nomatch" ) ;
47
56
}
@@ -57,10 +66,13 @@ function watchExpression(playfield, examples, regex, message) {
57
66
return null ;
58
67
}
59
68
}
60
- function getReferenceRegex ( ) {
69
+ function getReferenceInfo ( ) {
61
70
var reference = regex . getAttribute ( "reference" ) ;
62
71
try {
63
- return RegExp ( reference ) ;
72
+ return {
73
+ "shouldMatchWholeLine" : reference . startsWith ( '^' ) && reference . endsWith ( '$' ) ,
74
+ "regex" : RegExp ( reference )
75
+ } ;
64
76
} catch ( err ) {
65
77
message . innerHTML = translateReferenceWrongErrorMessage ( err . message ) ;
66
78
return null ;
@@ -69,9 +81,9 @@ function watchExpression(playfield, examples, regex, message) {
69
81
function check ( ) {
70
82
updateExperiment ( ) ;
71
83
playfield . classList . remove ( "success" ) ;
72
- var reference = getReferenceRegex ( ) ;
84
+ var referenceInfo = getReferenceInfo ( ) ;
73
85
var exp = getExpression ( ) ;
74
- if ( ! exp || ! reference ) {
86
+ if ( ! exp || ! referenceInfo ) {
75
87
return ;
76
88
}
77
89
message . innerHTML = "" ;
@@ -83,7 +95,13 @@ function watchExpression(playfield, examples, regex, message) {
83
95
var example = example_list [ i ] ;
84
96
var text = example . innerText ;
85
97
// 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
+ }
87
105
if ( shouldNotMatch ) {
88
106
example . classList . add ( "fail" ) ;
89
107
example . classList . remove ( "ok" ) ;
@@ -94,7 +112,12 @@ function watchExpression(playfield, examples, regex, message) {
94
112
// check the match
95
113
var match = exp . exec ( text ) ;
96
114
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 ) ;
98
121
} else {
99
122
unmatchExample ( example ) ;
100
123
}
@@ -130,7 +153,7 @@ function watchExpression(playfield, examples, regex, message) {
130
153
var match = exp . exec ( text ) ;
131
154
if ( match ) {
132
155
experiment . classList . add ( "match" ) ;
133
- matchExample ( match , content ) ;
156
+ matchExample ( match , false , text , content ) ;
134
157
} else {
135
158
experiment . classList . add ( "nomatch" ) ;
136
159
unmatchExample ( content ) ;
0 commit comments