Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate client code to package:web #3610

Merged
merged 7 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,725 changes: 2,690 additions & 4,035 deletions lib/resources/docs.dart.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/resources/docs.dart.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
path: ^1.8.3
pub_semver: ^2.1.4
source_span: ^1.10.0
web: ^0.5.1
yaml: ^3.1.2

dev_dependencies:
Expand Down
89 changes: 2 additions & 87 deletions web/docs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:html';

import 'highlight.dart' as highlight;
import 'search.dart' as search;
import 'sidenav.dart' as sidenav;
import 'sidebars.dart' as sidebars;
import 'theme.dart' as theme;
import 'web_interop.dart';

void main() {
initializeSidebars();
sidebars.init();
search.init();
sidenav.init();
highlight.init();
theme.init();
}

void initializeSidebars() {
final body = document.body;
if (body == null) {
return;
}
final dataUsingBaseHref = body.getAttribute('data-using-base-href');
if (dataUsingBaseHref == null) {
// This should never happen.
return;
}
final String baseHref;
if (dataUsingBaseHref != 'true') {
final dataBaseHref = body.getAttribute('data-base-href');
if (dataBaseHref == null) {
return;
}
baseHref = dataBaseHref;
} else {
baseHref = '';
}

final mainContent = document.getElementById('dartdoc-main-content');
if (mainContent == null) {
return;
}
final sanitizer = _SidebarNodeTreeSanitizer(baseHref);

void loadSidebar(String? sidebarPath, Element? sidebarElement) {
if (sidebarPath == null || sidebarPath.isEmpty || sidebarElement == null) {
return;
}

window.fetch('$baseHref$sidebarPath').then((response) async {
final fetchResponse = response as FetchResponse;
if (response.status != 200) {
final errorAnchor = (document.createElement('a') as AnchorElement)
..href = 'https://dart.dev/tools/dart-doc#troubleshoot'
..text = 'Failed to load sidebar. '
'Visit dart.dev for help troubleshooting.';
sidebarElement.append(errorAnchor);
return;
}

final content = await fetchResponse.text;

sidebarElement.setInnerHtml(content, treeSanitizer: sanitizer);
});
}

final aboveSidebarPath = mainContent.getAttribute('data-above-sidebar');
final leftSidebar = document.getElementById('dartdoc-sidebar-left-content');
loadSidebar(aboveSidebarPath, leftSidebar);

final belowSidebarPath = mainContent.getAttribute('data-below-sidebar');
final rightSidebar = document.getElementById('dartdoc-sidebar-right');
loadSidebar(belowSidebarPath, rightSidebar);
}

/// A permissive sanitizer that allows external links (e.g. to api.dart.dev) and
/// adjusts the links in a newly loaded sidebar, if "base href" is not being
/// used.
class _SidebarNodeTreeSanitizer implements NodeTreeSanitizer {
final String baseHref;

_SidebarNodeTreeSanitizer(this.baseHref);

@override
void sanitizeTree(Node node) {
if (node is Element && node.nodeName == 'A') {
final hrefString = node.attributes['href'];
if (hrefString != null) {
final href = Uri.parse(hrefString);
if (!href.isAbsolute) {
node.setAttribute('href', '$baseHref$hrefString');
}
}
}
node.childNodes.forEach(sanitizeTree);
}
}
Loading