@@ -144,7 +144,7 @@ module.exports = {
144144 'text/turtle' : true ,
145145 'application/rdf+xml' : true ,
146146 'application/xhtml+xml' : true , // For RDFa?
147- // 'text/html': true,
147+ 'text/html' : true , // For data island
148148 // 'application/sparql-update': true,
149149 'application/json' : true ,
150150 'application/ld+json' : true
@@ -157,8 +157,11 @@ module.exports = {
157157 * @param {Integer } caretPos - the poisition starting at zero
158158 * @credit https://stackoverflow.com/questions/512528/set-keyboard-caret-position-in-html-textbox
159159 */
160- function setCaretPosition ( elem , caretPos ) {
160+ function setCaretPosition ( elem , cause ) {
161161 if ( elem != null ) {
162+ if ( cause . characterInFile === - 1 && cause . lineNo ) cause . lineNo += 1
163+ const pos = cause . lineNo ? elem . value . split ( '\n' , cause . lineNo ) . join ( '\n' ) . length : 0
164+ let caretPos = pos + cause . characterInFile
162165 if ( elem . createTextRange ) {
163166 const range = elem . createTextRange ( )
164167 range . move ( 'character' , caretPos )
@@ -172,16 +175,40 @@ module.exports = {
172175 }
173176 }
174177
178+ function HTMLDataIsland ( data ) {
179+ let dataIslandContentType = ''
180+ let dataIsland = ''
181+ let pos = 0
182+ const scripts = data . split ( '</script' )
183+ if ( scripts && scripts . length ) {
184+ for ( let script of scripts ) {
185+ script = '<script' + script . split ( '<script' ) [ 1 ] + '</script>'
186+ const RDFType = [ 'text/turtle' , 'text/n3' , 'application/ld+json' , 'application/rdf+xml' ]
187+ const contentType = RDFType . find ( type => script . includes ( `type="${ type } "` ) )
188+ if ( contentType ) {
189+ dataIsland = script . replace ( / ^ < s c r i p t ( .* ?) > / gms, '' ) . replace ( / < \/ s c r i p t > $ / gms, '' )
190+ dataIslandContentType = contentType
191+ break
192+ }
193+ }
194+ }
195+ return [ dataIsland , dataIslandContentType ]
196+ }
197+
175198 function checkSyntax ( data , contentType , base ) {
176199 if ( ! parseable [ contentType ] ) return true // don't check things we don't understand
200+ if ( contentType = 'text/html' ) {
201+ [ data , contentType , pos ] = HTMLDataIsland ( data )
202+ if ( ! contentType ) return true
203+ }
177204 try {
178205 statusRow . innerHTML = ''
179206 contentType === 'application/json' ? JSON . parse ( data ) : $rdf . parse ( data , kb , base , contentType )
180207 } catch ( e ) {
181208 statusRow . appendChild ( UI . widgets . errorMessageBlock ( dom , e ) )
182209 for ( let cause = e ; ( cause = cause . cause ) ; cause ) {
183210 if ( cause . characterInFile ) {
184- setCaretPosition ( textArea , e . characterInFile )
211+ setCaretPosition ( textArea , cause )
185212 }
186213 }
187214 return false
0 commit comments