@@ -35,7 +35,8 @@ export interface TypeFile {
3535 * model can write its own JSON but cannot compress it (h5p-offline-player#206). The value runs to
3636 * the end of the fragment, since JSON text holds `&` and `+`, and it is percent-decoded as far as
3737 * it can be (`percentDecoded`). Base64url never holds `{` or `%`, so the forms cannot be mistaken.
38- * Its text is returned even where it is not JSON: a weak model's JSON slips, and text that can
38+ * Up to three braces or brackets left open at its end are closed (`withClosers`). Its text is
39+ * returned even where it is still not JSON: a weak model's JSON slips, and text that can
3940 * be read goes in the box, where the build lists where the JSON breaks, for the chat to fix, as
4041 * for a source pasted in. Only a compressed source that cannot be read is a damaged link.
4142 *
@@ -49,7 +50,9 @@ export async function sourceFromFragment(hash: string): Promise<string | null> {
4950 if ( isJson ( text ) ) return text
5051 // A link set in a sentence may end in what follows it, as `}.`: the JSON is up to its last brace.
5152 const upToBrace = text . slice ( 0 , text . lastIndexOf ( '}' ) + 1 )
52- return isJson ( upToBrace ) ? upToBrace : text
53+ if ( isJson ( upToBrace ) ) return upToBrace
54+ const closed = withClosers ( text )
55+ return closed !== null && isJson ( closed ) ? closed : text
5356 }
5457 const value = new URLSearchParams ( fragment ) . get ( 'source' )
5558 if ( ! value ) return null
@@ -72,6 +75,33 @@ export async function sourceFromFragment(hash: string): Promise<string | null> {
7275 throw new Error ( 'The link’s #source= is neither JSON nor deflated JSON in base64url' )
7376}
7477
78+ /**
79+ * The JSON with the braces and brackets still open at its end closed, up to three of them, or
80+ * `null` where it ends inside a string or has more open. A model that writes a source on one line
81+ * loses count of its closers: Haiku left one `}` out of a True/False's 600 characters
82+ * (h5p-offline-player#206). What is closed at the end is only where the source ends, and the
83+ * build still checks every field of it.
84+ */
85+ function withClosers ( text : string ) : string | null {
86+ const open : string [ ] = [ ]
87+ let inString = false
88+ let escaped = false
89+ for ( const char of text ) {
90+ if ( inString ) {
91+ if ( escaped ) escaped = false
92+ else if ( char === '\\' ) escaped = true
93+ else if ( char === '"' ) inString = false
94+ } else if ( char === '"' ) inString = true
95+ else if ( char === '{' ) open . push ( '}' )
96+ else if ( char === '[' ) open . push ( ']' )
97+ else if ( char === '}' || char === ']' ) {
98+ if ( open . pop ( ) !== char ) return null
99+ }
100+ }
101+ if ( inString || ! open . length || open . length > 3 ) return null
102+ return text . trimEnd ( ) + open . reverse ( ) . join ( '' )
103+ }
104+
75105/**
76106 * Percent-decodes JSON a model wrote into a link, as far as it can: each run of escapes is decoded
77107 * where it is UTF-8 and kept as written where it is not, and a `%` that starts no escape is a `%`.
0 commit comments