@@ -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
4149function 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