Skip to content

Commit 11abbbc

Browse files
authored
Do not throw on malformed UTF-8 encodings. (#1890)
1 parent 7930d89 commit 11abbbc

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/src/package_meta.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
library dartdoc.package_meta;
66

7+
import 'dart:convert';
78
import 'dart:io';
89

910
import 'package:analyzer/dart/element/element.dart';
@@ -14,6 +15,7 @@ import 'package:yaml/yaml.dart';
1415
import 'logging.dart';
1516

1617
Map<String, PackageMeta> _packageMetaCache = {};
18+
Encoding utf8AllowMalformed = new Utf8Codec(allowMalformed: true);
1719

1820
Directory get defaultSdkDir {
1921
Directory sdkDir = new File(Platform.resolvedExecutable).parent.parent;
@@ -212,7 +214,7 @@ class FileContents {
212214
factory FileContents(File file) =>
213215
file == null ? null : new FileContents._(file);
214216

215-
String get contents => file.readAsStringSync();
217+
String get contents => file.readAsStringSync(encoding: utf8AllowMalformed);
216218

217219
bool get isMarkdown => file.path.toLowerCase().endsWith('.md');
218220

test/package_meta_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ library dartdoc.package_utils_test;
77
import 'dart:io';
88

99
import 'package:dartdoc/src/package_meta.dart';
10+
import 'package:path/path.dart' as pathLib;
1011
import 'package:test/test.dart';
1112

1213
void main() {
@@ -23,6 +24,14 @@ void main() {
2324
});
2425
});
2526

27+
group('PackageMeta for the test package', () {
28+
PackageMeta p = new PackageMeta.fromDir(new Directory(pathLib.join(Directory.current.path, 'testing', 'test_package')));
29+
30+
test('readme with corrupt UTF-8 loads without throwing', () {
31+
expect(p.getReadmeContents().contents, contains('Here is some messed up UTF-8.\nÐf'));
32+
});
33+
});
34+
2635
group('PackageMeta.fromDir for this package', () {
2736
PackageMeta p = new PackageMeta.fromDir(Directory.current);
2837

testing/test_package/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
This is an amazing package.
77

8+
## Here is some messed up UTF-8.
9+
ÐfуÃzƒÂ‘‰
810

911
## Examples
1012

0 commit comments

Comments
 (0)