Skip to content

Commit

Permalink
Migrate client code to package:web
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough committed Dec 20, 2023
1 parent 4c12f81 commit 1ed1ac1
Show file tree
Hide file tree
Showing 10 changed files with 5,731 additions and 4,465 deletions.
9,659 changes: 5,458 additions & 4,201 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.

6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A non-interactive HTML documentation generator for Dart source code
repository: https://github.com/dart-lang/dartdoc

environment:
sdk: ^3.0.0
sdk: ^3.2.0

dependencies:
analyzer: ^6.3.0
Expand All @@ -25,14 +25,14 @@ dependencies:
dev_dependencies:
async: ^2.11.0
dart_style: ^2.3.1
http: ">=0.13.6 <2.0.0"
js: ^0.6.7
http: ^1.1.2
lints: ^3.0.0
matcher: ^0.12.15
test: ^1.24.2
test_descriptor: ^2.0.1
test_process: ^2.0.3
test_reflective_loader: ^0.2.2
web: ^0.4.0

executables:
dartdoc: null
68 changes: 39 additions & 29 deletions web/docs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// 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 'package:http/http.dart' as http show get;
import 'package:web/web.dart';

import 'highlight.dart' as highlight;
import 'search.dart' as search;
Expand All @@ -18,18 +19,19 @@ void main() {
}

void initializeSidebars() {
final body = document.querySelector('body');
final body = document.querySelector('body') as HTMLElement?;
if (body == null) {
return;
}
final dataUsingBaseHref = body.dataset['using-base-href'];

final dataUsingBaseHref = body.getAttribute('data-using-base-href');
if (dataUsingBaseHref == null) {
// This should never happen.
return;
}
var baseHref = '';
if (dataUsingBaseHref != 'true') {
final dataBaseHref = body.dataset['base-href'];
final dataBaseHref = body.getAttribute('data-base-href');
if (dataBaseHref == null) {
return;
}
Expand All @@ -39,46 +41,54 @@ void initializeSidebars() {
if (mainContent == null) {
return;
}
final aboveSidebarPath = mainContent.dataset['above-sidebar'];
final aboveSidebarPath = mainContent.getAttribute('data-above-sidebar');
final leftSidebar = document.querySelector('#dartdoc-sidebar-left-content');
final sanitizer = _SidebarNodeTreeSanitizer(baseHref);
if (aboveSidebarPath != null &&
aboveSidebarPath.isNotEmpty &&
leftSidebar != null) {
HttpRequest.getString('$baseHref$aboveSidebarPath').then((content) {
leftSidebar.setInnerHtml(content, treeSanitizer: sanitizer);
http.get(Uri.parse('$baseHref$aboveSidebarPath')).then((response) {
if (response.statusCode == 200) {
final content = document.createElement('template')
..innerHTML = response.body;
_updateLinks(baseHref, content);
leftSidebar.appendChild(content);
}
});
}
final belowSidebarPath = mainContent.dataset['below-sidebar'];
final belowSidebarPath = mainContent.getAttribute('data-below-sidebar');
final rightSidebar = document.querySelector('#dartdoc-sidebar-right');
if (belowSidebarPath != null &&
belowSidebarPath.isNotEmpty &&
rightSidebar != null) {
HttpRequest.getString('$baseHref$belowSidebarPath').then((content) {
rightSidebar.setInnerHtml(content, treeSanitizer: sanitizer);
http.get(Uri.parse('$baseHref$belowSidebarPath')).then((response) {
if (response.statusCode == 200) {
final content = document.createElement('template')
..innerHTML = response.body;
_updateLinks(baseHref, content);
rightSidebar.appendChild(content);
}
});
}
}

/// 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');
}
/// Recurses down a DOM tree to adjust the links in a newly loaded sidebar
/// if "base href" is not being used.
void _updateLinks(String baseHref, Node node) {
if (node is Element && node.nodeName == 'A') {
final hrefString = node.getAttribute('href');
if (hrefString != null) {
final href = Uri.parse(hrefString);
if (!href.isAbsolute) {
node.setAttribute('href', '$baseHref$hrefString');
}
}
node.childNodes.forEach(sanitizeTree);
}

final children = node.childNodes;
for (var childIndex = 0; childIndex < children.length; childIndex += 1) {
final child = children.item(childIndex);
if (child != null) {
_updateLinks(baseHref, child);
}
}
}
4 changes: 2 additions & 2 deletions web/highlight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// 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 'package:js/js.dart';
import 'dart:js_interop';

void init() {
highlight?.highlightAll();
}

@JS()
@staticInterop
class HighlightJs {}
final class HighlightJs {}

extension HighlightJsExtension on HighlightJs {
external void highlightAll();
Expand Down
Loading

0 comments on commit 1ed1ac1

Please sign in to comment.