Skip to content

Do not throw on malformed UTF-8 encodings #1890

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

Merged
merged 2 commits into from
Jan 8, 2019
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
4 changes: 3 additions & 1 deletion lib/src/package_meta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

library dartdoc.package_meta;

import 'dart:convert';
import 'dart:io';

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

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

Directory get defaultSdkDir {
Directory sdkDir = new File(Platform.resolvedExecutable).parent.parent;
Expand Down Expand Up @@ -212,7 +214,7 @@ class FileContents {
factory FileContents(File file) =>
file == null ? null : new FileContents._(file);

String get contents => file.readAsStringSync();
String get contents => file.readAsStringSync(encoding: utf8AllowMalformed);

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

Expand Down
9 changes: 9 additions & 0 deletions test/package_meta_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ library dartdoc.package_utils_test;
import 'dart:io';

import 'package:dartdoc/src/package_meta.dart';
import 'package:path/path.dart' as pathLib;
import 'package:test/test.dart';

void main() {
Expand All @@ -23,6 +24,14 @@ void main() {
});
});

group('PackageMeta for the test package', () {
PackageMeta p = new PackageMeta.fromDir(new Directory(pathLib.join(Directory.current.path, 'testing', 'test_package')));

test('readme with corrupt UTF-8 loads without throwing', () {
expect(p.getReadmeContents().contents, contains('Here is some messed up UTF-8.\nÐf'));
});
});

group('PackageMeta.fromDir for this package', () {
PackageMeta p = new PackageMeta.fromDir(Directory.current);

Expand Down
2 changes: 2 additions & 0 deletions testing/test_package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

This is an amazing package.

## Here is some messed up UTF-8.
Ðfу�z�‘‰

## Examples

Expand Down