@@ -11,7 +11,6 @@ namespace AngleSharp.Js
1111 using System . IO ;
1212 using System . Linq ;
1313 using System . Reflection ;
14- using System . Text . Json ;
1514
1615 sealed class EngineInstance
1716 {
@@ -123,24 +122,17 @@ public JsValue RunScript(String source, String type, JsValue context)
123122
124123 private JsValue LoadImportMap ( String source )
125124 {
126- JsImportMap importMap ;
127-
128- try
129- {
130- importMap = JsonSerializer . Deserialize < JsImportMap > ( source ) ;
131- }
132- catch ( JsonException )
133- {
134- importMap = null ;
135- }
125+ var importMap = _engine . Evaluate ( $ "JSON.parse('{ source } ')") . AsObject ( ) ;
136126
137127 // get list of imports based on any scoped imports for the current document path, and any global imports
138- var imports = new Dictionary < string , Uri > ( ) ;
128+ var moduleImports = new Dictionary < string , string > ( ) ;
139129 var documentPathName = Url . Create ( _documentUrl ) . PathName . ToLower ( ) ;
140130
141- if ( importMap ? . Scopes ? . Count > 0 )
131+ if ( importMap . TryGetValue ( "scopes" , out var scopes ) )
142132 {
143- var scopePaths = importMap . Scopes . Keys . OrderByDescending ( k => k . Length ) ;
133+ var scopesObj = scopes . AsObject ( ) ;
134+
135+ var scopePaths = scopesObj . GetOwnPropertyKeys ( ) . Select ( k => k . AsString ( ) ) . OrderByDescending ( k => k . Length ) ;
144136
145137 foreach ( var scopePath in scopePaths )
146138 {
@@ -149,32 +141,38 @@ private JsValue LoadImportMap(String source)
149141 continue ;
150142 }
151143
152- var scopeImports = importMap . Scopes [ scopePath ] ;
144+ var scopeImports = scopesObj [ scopePath ] . AsObject ( ) ;
145+
146+ var scopeImportImportSpecifiers = scopeImports . GetOwnPropertyKeys ( ) . Select ( k => k . AsString ( ) ) ;
153147
154- foreach ( var scopeImport in scopeImports )
148+ foreach ( var scopeImportSpecifier in scopeImportImportSpecifiers )
155149 {
156- if ( ! imports . ContainsKey ( scopeImport . Key ) )
150+ if ( ! moduleImports . ContainsKey ( scopeImportSpecifier ) )
157151 {
158- imports . Add ( scopeImport . Key , scopeImport . Value ) ;
152+ moduleImports . Add ( scopeImportSpecifier , scopeImports [ scopeImportSpecifier ] . AsString ( ) ) ;
159153 }
160154 }
161155 }
162156 }
163157
164- if ( importMap ? . Imports ? . Count > 0 )
158+ if ( importMap . TryGetValue ( "imports" , out var imports ) )
165159 {
166- foreach ( var globalImport in importMap . Imports )
160+ var importsObj = imports . AsObject ( ) ;
161+
162+ var importSpecifiers = importsObj . GetOwnPropertyKeys ( ) . Select ( k => k . AsString ( ) ) ;
163+
164+ foreach ( var importSpecifier in importSpecifiers )
167165 {
168- if ( ! imports . ContainsKey ( globalImport . Key ) )
166+ if ( ! moduleImports . ContainsKey ( importSpecifier ) )
169167 {
170- imports . Add ( globalImport . Key , globalImport . Value ) ;
168+ moduleImports . Add ( importSpecifier , importsObj [ importSpecifier ] . AsString ( ) ) ;
171169 }
172170 }
173171 }
174172
175- foreach ( var import in imports )
173+ foreach ( var import in moduleImports )
176174 {
177- var moduleContent = FetchModule ( import . Value ) ;
175+ var moduleContent = FetchModule ( new Uri ( import . Value , UriKind . RelativeOrAbsolute ) ) ;
178176
179177 ImportModule ( import . Key , moduleContent ) ;
180178 }
0 commit comments