|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -import 'dart:html'; |
6 |
| - |
7 | 5 | import 'highlight.dart' as highlight;
|
8 | 6 | import 'search.dart' as search;
|
9 |
| -import 'sidenav.dart' as sidenav; |
| 7 | +import 'sidebars.dart' as sidebars; |
10 | 8 | import 'theme.dart' as theme;
|
11 |
| -import 'web_interop.dart'; |
12 | 9 |
|
13 | 10 | void main() {
|
14 |
| - initializeSidebars(); |
| 11 | + sidebars.init(); |
15 | 12 | search.init();
|
16 |
| - sidenav.init(); |
17 | 13 | highlight.init();
|
18 | 14 | theme.init();
|
19 | 15 | }
|
20 |
| - |
21 |
| -void initializeSidebars() { |
22 |
| - final body = document.body; |
23 |
| - if (body == null) { |
24 |
| - return; |
25 |
| - } |
26 |
| - final dataUsingBaseHref = body.getAttribute('data-using-base-href'); |
27 |
| - if (dataUsingBaseHref == null) { |
28 |
| - // This should never happen. |
29 |
| - return; |
30 |
| - } |
31 |
| - final String baseHref; |
32 |
| - if (dataUsingBaseHref != 'true') { |
33 |
| - final dataBaseHref = body.getAttribute('data-base-href'); |
34 |
| - if (dataBaseHref == null) { |
35 |
| - return; |
36 |
| - } |
37 |
| - baseHref = dataBaseHref; |
38 |
| - } else { |
39 |
| - baseHref = ''; |
40 |
| - } |
41 |
| - |
42 |
| - final mainContent = document.getElementById('dartdoc-main-content'); |
43 |
| - if (mainContent == null) { |
44 |
| - return; |
45 |
| - } |
46 |
| - final sanitizer = _SidebarNodeTreeSanitizer(baseHref); |
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); |
67 |
| - }); |
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); |
77 |
| -} |
78 |
| - |
79 |
| -/// A permissive sanitizer that allows external links (e.g. to api.dart.dev) and |
80 |
| -/// adjusts the links in a newly loaded sidebar, if "base href" is not being |
81 |
| -/// used. |
82 |
| -class _SidebarNodeTreeSanitizer implements NodeTreeSanitizer { |
83 |
| - final String baseHref; |
84 |
| - |
85 |
| - _SidebarNodeTreeSanitizer(this.baseHref); |
86 |
| - |
87 |
| - @override |
88 |
| - void sanitizeTree(Node node) { |
89 |
| - if (node is Element && node.nodeName == 'A') { |
90 |
| - final hrefString = node.attributes['href']; |
91 |
| - if (hrefString != null) { |
92 |
| - final href = Uri.parse(hrefString); |
93 |
| - if (!href.isAbsolute) { |
94 |
| - node.setAttribute('href', '$baseHref$hrefString'); |
95 |
| - } |
96 |
| - } |
97 |
| - } |
98 |
| - node.childNodes.forEach(sanitizeTree); |
99 |
| - } |
100 |
| -} |
0 commit comments