@@ -8,56 +8,72 @@ import 'highlight.dart' as highlight;
8
8
import 'search.dart' as search;
9
9
import 'sidenav.dart' as sidenav;
10
10
import 'theme.dart' as theme;
11
+ import 'web_interop.dart' ;
11
12
12
13
void main () {
13
- highlight.init ();
14
14
initializeSidebars ();
15
- sidenav.init ();
16
15
search.init ();
16
+ sidenav.init ();
17
+ highlight.init ();
17
18
theme.init ();
18
19
}
19
20
20
21
void initializeSidebars () {
21
- final body = document.querySelector ( ' body' ) ;
22
+ final body = document.body;
22
23
if (body == null ) {
23
24
return ;
24
25
}
25
- final dataUsingBaseHref = body.dataset[ ' using-base-href'] ;
26
+ final dataUsingBaseHref = body.getAttribute ( 'data- using-base-href') ;
26
27
if (dataUsingBaseHref == null ) {
27
28
// This should never happen.
28
29
return ;
29
30
}
30
- var baseHref = '' ;
31
+ final String baseHref ;
31
32
if (dataUsingBaseHref != 'true' ) {
32
- final dataBaseHref = body.dataset[ ' base-href'] ;
33
+ final dataBaseHref = body.getAttribute ( 'data- base-href') ;
33
34
if (dataBaseHref == null ) {
34
35
return ;
35
36
}
36
37
baseHref = dataBaseHref;
38
+ } else {
39
+ baseHref = '' ;
37
40
}
38
- final mainContent = document.querySelector ('#dartdoc-main-content' );
41
+
42
+ final mainContent = document.getElementById ('dartdoc-main-content' );
39
43
if (mainContent == null ) {
40
44
return ;
41
45
}
42
- final aboveSidebarPath = mainContent.dataset['above-sidebar' ];
43
- final leftSidebar = document.querySelector ('#dartdoc-sidebar-left-content' );
44
46
final sanitizer = _SidebarNodeTreeSanitizer (baseHref);
45
- if (aboveSidebarPath != null &&
46
- aboveSidebarPath.isNotEmpty &&
47
- leftSidebar != null ) {
48
- HttpRequest .getString ('$baseHref $aboveSidebarPath ' ).then ((content) {
49
- leftSidebar.setInnerHtml (content, treeSanitizer: sanitizer);
50
- });
51
- }
52
- final belowSidebarPath = mainContent.dataset['below-sidebar' ];
53
- final rightSidebar = document.querySelector ('#dartdoc-sidebar-right' );
54
- if (belowSidebarPath != null &&
55
- belowSidebarPath.isNotEmpty &&
56
- rightSidebar != null ) {
57
- HttpRequest .getString ('$baseHref $belowSidebarPath ' ).then ((content) {
58
- rightSidebar.setInnerHtml (content, treeSanitizer: sanitizer);
47
+
48
+ void loadSidebar (String ? sidebarPath, Element ? sidebarElement) {
49
+ if (sidebarPath == null || sidebarPath.isEmpty || sidebarElement == null ) {
50
+ return ;
51
+ }
52
+
53
+ window.fetch ('$baseHref $sidebarPath ' ).then ((response) async {
54
+ final fetchResponse = response as FetchResponse ;
55
+ if (response.status != 200 ) {
56
+ final errorAnchor = (document.createElement ('a' ) as AnchorElement )
57
+ ..href = 'https://dart.dev/tools/dart-doc#troubleshoot'
58
+ ..text = 'Failed to load sidebar. '
59
+ 'Visit dart.dev for help troubleshooting.' ;
60
+ sidebarElement.append (errorAnchor);
61
+ return ;
62
+ }
63
+
64
+ final content = await fetchResponse.text;
65
+
66
+ sidebarElement.setInnerHtml (content, treeSanitizer: sanitizer);
59
67
});
60
68
}
69
+
70
+ final aboveSidebarPath = mainContent.getAttribute ('data-above-sidebar' );
71
+ final leftSidebar = document.getElementById ('dartdoc-sidebar-left-content' );
72
+ loadSidebar (aboveSidebarPath, leftSidebar);
73
+
74
+ final belowSidebarPath = mainContent.getAttribute ('data-below-sidebar' );
75
+ final rightSidebar = document.getElementById ('dartdoc-sidebar-right' );
76
+ loadSidebar (belowSidebarPath, rightSidebar);
61
77
}
62
78
63
79
/// A permissive sanitizer that allows external links (e.g. to api.dart.dev) and
0 commit comments