Skip to content

Remove <base href> and emit absolute hrefs instead #2089

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

Closed
wants to merge 2 commits into from
Closed
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
48 changes: 26 additions & 22 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,31 +237,26 @@ class Dartdoc extends PackageBuilder {

// This is extracted to save memory during the check; be careful not to hang
// on to anything referencing the full file and doc tree.
Tuple2<Iterable<String>, String> _getStringLinksAndHref(String fullPath) {
File file = File("$fullPath");
Iterable<String> _getStringLinks(String fullPath) {
File file = File(fullPath);
if (!file.existsSync()) {
return null;
}
Document doc = parse(file.readAsBytesSync());
Element base = doc.querySelector('base');
String baseHref;
if (base != null) {
baseHref = base.attributes['href'];
}
List<Element> links = doc.querySelectorAll('a');
List<String> stringLinks = links
.map((link) => link.attributes['href'])
.where((href) => href != null)
.toList();

return Tuple2(stringLinks, baseHref);
return stringLinks;
}

void _doSearchIndexCheck(
PackageGraph packageGraph, String origin, Set<String> visited) {
String fullPath = path.joinAll([origin, 'index.json']);
String indexPath = path.joinAll([origin, 'index.html']);
File file = File("$fullPath");
File file = File(fullPath);
if (!file.existsSync()) {
return null;
}
Expand All @@ -275,7 +270,17 @@ class Dartdoc extends PackageBuilder {
found.add(indexPath);
for (Map<String, dynamic> entry in jsonData) {
if (entry.containsKey('href')) {
String entryPath = path.joinAll([origin, entry['href']]);
String href = entry['href'];
var fullPath;
if (path.isAbsolute(href)) {
// Assume absolute hrefs belong to us and should match a generated
// file. However path.join() will ignore parts before an absolute
// one, so make the href relative by prefixing a dot.
fullPath = path.join(origin, '.$href');
} else {
fullPath = path.join(origin, href);
}
String entryPath = path.normalize(fullPath);
if (!visited.contains(entryPath)) {
_warn(packageGraph, PackageWarning.brokenLink, entryPath,
path.normalize(origin),
Expand All @@ -301,8 +306,8 @@ class Dartdoc extends PackageBuilder {
fullPath = path.normalize(fullPath);
}

Tuple2 stringLinksAndHref = _getStringLinksAndHref(fullPath);
if (stringLinksAndHref == null) {
Iterable<String> stringLinks = _getStringLinks(fullPath);
if (stringLinks == null) {
_warn(packageGraph, PackageWarning.brokenLink, pathToCheck,
path.normalize(origin),
referredFrom: source);
Expand All @@ -313,8 +318,6 @@ class Dartdoc extends PackageBuilder {
return null;
}
visited.add(fullPath);
Iterable<String> stringLinks = stringLinksAndHref.item1;
String baseHref = stringLinksAndHref.item2;

// Prevent extremely large stacks by storing the paths we are using
// here instead -- occasionally, very large jobs have overflowed
Expand All @@ -334,17 +337,18 @@ class Dartdoc extends PackageBuilder {

if (uri == null || !uri.hasAuthority && !uri.hasFragment) {
var full;
if (baseHref != null) {
full = '${path.dirname(pathToCheck)}/$baseHref/$href';
if (path.isAbsolute(href)) {
// Assume absolute hrefs belong to us and should match a generated
// file. However path.join() will ignore parts before an absolute
// one, so make the href relative by prefixing a dot.
full = path.join(origin, '.$href');
} else {
full = '${path.dirname(pathToCheck)}/$href';
full = path.join(origin, href);
}
var newPathToCheck = path.normalize(full);
String newFullPath = path.joinAll([origin, newPathToCheck]);
newFullPath = path.normalize(newFullPath);
if (!visited.contains(newFullPath)) {
toVisit.add(Tuple2(newPathToCheck, newFullPath));
visited.add(newFullPath);
if (!visited.contains(newPathToCheck)) {
toVisit.add(Tuple2(href, newPathToCheck));
visited.add(newPathToCheck);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/model/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class Package extends LibraryContainer
}
});
if (!_baseHref.endsWith('/')) _baseHref = '${_baseHref}/';
} else if (documentedWhere == DocumentLocation.local) {
_baseHref = '/';
} else {
_baseHref = '';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/render/model_element_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ModelElementRendererHtml extends ModelElementRenderer {
z-index:100000;
background-position: center;
background-repeat: no-repeat;
background-image: url(static-assets/play_button.svg);">
background-image: url(/static-assets/play_button.svg);">
</div>
<video id="$uniqueId"
style="width:${width}px; height:${height}px;"
Expand Down
8 changes: 4 additions & 4 deletions lib/templates/_footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
</footer>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="static-assets/typeahead.bundle.min.js"></script>
<script src="static-assets/highlight.pack.js"></script>
<script src="static-assets/URI.js"></script>
<script src="static-assets/script.js"></script>
<script src="/static-assets/typeahead.bundle.min.js"></script>
<script src="/static-assets/highlight.pack.js"></script>
<script src="/static-assets/URI.js"></script>
<script src="/static-assets/script.js"></script>
<!-- footer placeholder -->

</body>
Expand Down
10 changes: 3 additions & 7 deletions lib/templates/_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
{{ #relCanonicalPrefix }}
<link rel="canonical" href="{{{relCanonicalPrefix}}}/{{{self.href}}}">
{{ /relCanonicalPrefix}}
{{#htmlBase}}
<!-- required because all the links are pseudo-absolute -->
<base href="{{{htmlBase}}}">
{{/htmlBase}}

<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:500,400i,400,300|Source+Sans+Pro:400,300,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="static-assets/github.css">
<link rel="stylesheet" href="static-assets/styles.css">
<link rel="icon" href="static-assets/favicon.png">
<link rel="stylesheet" href="/static-assets/github.css">
<link rel="stylesheet" href="/static-assets/styles.css">
<link rel="icon" href="/static-assets/favicon.png">
<!-- header placeholder -->
</head>

Expand Down
4 changes: 2 additions & 2 deletions test/model_special_cases_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void main() {
hashCode.inheritance.any((c) => c.name == 'Interceptor'), isTrue);
// If EventTarget really does start implementing hashCode, this will
// fail.
expect(hashCode.href, equals('dart-core/Object/hashCode.html'));
expect(hashCode.href, equals('/dart-core/Object/hashCode.html'));
expect(
hashCode.canonicalEnclosingContainer, equals(objectModelElement));
expect(
Expand Down Expand Up @@ -387,7 +387,7 @@ void main() {
test('sdk library have formatted names', () {
expect(dartAsyncLib.name, 'dart:async');
expect(dartAsyncLib.dirName, 'dart-async');
expect(dartAsyncLib.href, 'dart-async/dart-async-library.html');
expect(dartAsyncLib.href, '/dart-async/dart-async-library.html');
});
});

Expand Down
Loading