@@ -11,7 +11,6 @@ namespace AngleSharp.Js
11
11
using System . IO ;
12
12
using System . Linq ;
13
13
using System . Reflection ;
14
- using System . Text . Json ;
15
14
16
15
sealed class EngineInstance
17
16
{
@@ -123,24 +122,17 @@ public JsValue RunScript(String source, String type, JsValue context)
123
122
124
123
private JsValue LoadImportMap ( String source )
125
124
{
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 ( ) ;
136
126
137
127
// 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 > ( ) ;
139
129
var documentPathName = Url . Create ( _documentUrl ) . PathName . ToLower ( ) ;
140
130
141
- if ( importMap ? . Scopes ? . Count > 0 )
131
+ if ( importMap . TryGetValue ( "scopes" , out var scopes ) )
142
132
{
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 ) ;
144
136
145
137
foreach ( var scopePath in scopePaths )
146
138
{
@@ -149,32 +141,38 @@ private JsValue LoadImportMap(String source)
149
141
continue ;
150
142
}
151
143
152
- var scopeImports = importMap . Scopes [ scopePath ] ;
144
+ var scopeImports = scopesObj [ scopePath ] . AsObject ( ) ;
145
+
146
+ var scopeImportImportSpecifiers = scopeImports . GetOwnPropertyKeys ( ) . Select ( k => k . AsString ( ) ) ;
153
147
154
- foreach ( var scopeImport in scopeImports )
148
+ foreach ( var scopeImportSpecifier in scopeImportImportSpecifiers )
155
149
{
156
- if ( ! imports . ContainsKey ( scopeImport . Key ) )
150
+ if ( ! moduleImports . ContainsKey ( scopeImportSpecifier ) )
157
151
{
158
- imports . Add ( scopeImport . Key , scopeImport . Value ) ;
152
+ moduleImports . Add ( scopeImportSpecifier , scopeImports [ scopeImportSpecifier ] . AsString ( ) ) ;
159
153
}
160
154
}
161
155
}
162
156
}
163
157
164
- if ( importMap ? . Imports ? . Count > 0 )
158
+ if ( importMap . TryGetValue ( "imports" , out var imports ) )
165
159
{
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 )
167
165
{
168
- if ( ! imports . ContainsKey ( globalImport . Key ) )
166
+ if ( ! moduleImports . ContainsKey ( importSpecifier ) )
169
167
{
170
- imports . Add ( globalImport . Key , globalImport . Value ) ;
168
+ moduleImports . Add ( importSpecifier , importsObj [ importSpecifier ] . AsString ( ) ) ;
171
169
}
172
170
}
173
171
}
174
172
175
- foreach ( var import in imports )
173
+ foreach ( var import in moduleImports )
176
174
{
177
- var moduleContent = FetchModule ( import . Value ) ;
175
+ var moduleContent = FetchModule ( new Uri ( import . Value , UriKind . RelativeOrAbsolute ) ) ;
178
176
179
177
ImportModule ( import . Key , moduleContent ) ;
180
178
}
0 commit comments