Skip to content

Commit

Permalink
null-safety, update dependencies, linter, formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
rspilker committed Feb 11, 2022
1 parent 33266c3 commit f1ff2c2
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 79 deletions.
2 changes: 1 addition & 1 deletion lib/src/api/typedefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:htmlwhitelist/htmlwhitelist.dart';
/// Returns `true` if the given [tag] and [attributes] are accepted.
///
/// [attributes] contains all attribute names and values of the source tag.
typedef bool Filter(String tag, Map<String, String> attributes);
typedef bool Filter(String tag, Map<String, String?> attributes);

/// Returns `true` if the given [name] matches.
typedef bool Matcher(String name);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/api/uris.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import 'package:htmlwhitelist/htmlwhitelist.dart';

/// Contains some utility functions to inspect uris.
class Uris {
Uris._();

static final String _test1 = 'http://x.y/';
static final String _test2 = 'ftp://y.x/';
static final Uri _testUri1 = Uri.parse(_test1);
static final Uri _testUri2 = Uri.parse(_test2);

Uris._();

/// Returns `true` if [uri] contains a relative path.
///
/// A path is relative when, if it gets resolved by a different path,
Expand Down Expand Up @@ -77,7 +77,7 @@ class Uris {
/// - when the uri is resolved from any of the [allowed] uris, its path starts
/// with that allowed uri.
static Filter external(String attribute, {Iterable<String> allowed}) {
static Filter external(String attribute, {Iterable<String>? allowed}) {
var allowedUris =
allowed == null ? const <String>[] : new List<String>.from(allowed);
return (t, o) {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/api/whitelist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import '../impl/whitelistimpl.dart';
/// Defines the rules for what tags, attribute names and attribute values
/// are allowed in a piece of HTML.
abstract class Whitelist {
Whitelist._();

/// No tags allowed, only text nodes.
static final Whitelist none = WhitelistImpl.none;

Expand Down Expand Up @@ -88,8 +90,6 @@ abstract class Whitelist {
when: Uris.hasAllowedScheme('src', ['http', 'https', 'data']))
.attributes('img', ['align', 'alt', 'height', 'title', 'width']);

Whitelist._();

/// Creates a new Whitelist with additional allowed [tags].
///
/// The [tags] can be one of the following types:
Expand All @@ -100,7 +100,7 @@ abstract class Whitelist {
///
/// If [when] is provided, the tag will only be allowed if [when]
/// applies. Only if [tags] matches will [when] be invoked.
Whitelist tags(dynamic tags, {Filter when});
Whitelist tags(dynamic tags, {Filter? when});

/// Creates a new Whitelist with additional allowed [attributes] for the
/// given [tags] that will be copied from the source.
Expand All @@ -114,7 +114,7 @@ abstract class Whitelist {
/// If [when] is provided, the attribute will only be copied if [when]
/// applies. Only if both [tags] and [attributes] match will
/// [when] be invoked.
Whitelist attributes(dynamic tags, dynamic attributes, {Filter when});
Whitelist attributes(dynamic tags, dynamic attributes, {Filter? when});

/// Creates a new Whitelist with generated attributes for the
/// given [tags].
Expand All @@ -130,8 +130,8 @@ abstract class Whitelist {
///
/// If [when] is provided, the generator will only be invoked if [when]
/// applies. Only if [tags] matches will [when] be invoked.
Whitelist extraAttributes(dynamic tags, AttributeGenerator generator,
{Filter when});
Whitelist extraAttributes(dynamic tags, AttributeGenerator? generator,
{Filter? when});

/// Returns a safe copy of the [contents] after
/// applying the rules of this Whitelist.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/impl/attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import 'package:htmlwhitelist/htmlwhitelist.dart';

class Attribute {
Attribute(this._tags, this._generator, this._when);

final Matcher _tags;
final AttributeGenerator _generator;
final Filter _when;

Attribute(this._tags, this._generator, this._when);

void generate(String tag, Map<String, String> attributes,
AttributeCollector collector) {
if (_tags(tag) && _when(tag, attributes)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/impl/cleanerimpl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import 'collector.dart';
import 'tag.dart';

class CleanerImpl implements Cleaner {
CleanerImpl(this._tags, this._attributes);

final List<Tag> _tags;
final List<Attribute> _attributes;

CleanerImpl(this._tags, this._attributes);

@override
DocumentFragment safeCopy(Node node) {
var document = new Document();
Expand All @@ -31,7 +31,7 @@ class CleanerImpl implements Cleaner {
var newTarget = target;
var originalAttributes =
new Map<String, String>.unmodifiable(_toStringMap(node.attributes));
var tag = node.localName;
var tag = node.localName!;
if (_tagAllowed(tag, originalAttributes)) {
newTarget = _copy(document, tag, originalAttributes);
target.append(newTarget);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/impl/collector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Collector implements AttributeCollector {
}

@override
void append(String name, String value, {String separator}) {
void append(String name, String value, {String? separator}) {
_checkNameAndValue(name, value);
var previous = _values[name];
if (previous == null) {
Expand All @@ -32,7 +32,7 @@ class Collector implements AttributeCollector {
}

@override
void prepend(String name, String value, {String separator}) {
void prepend(String name, String value, {String? separator}) {
_checkNameAndValue(name, value);
var previous = _values[name];
if (previous == null) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/impl/tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import 'package:htmlwhitelist/htmlwhitelist.dart';

class Tag {
Tag(this._matcher, this._when);

final Matcher _matcher;
final Filter _when;

Tag(this._matcher, this._when);

bool allowed(String tag, Map<String, String> attributes) =>
_matcher(tag) && _when(tag, attributes);
}
16 changes: 8 additions & 8 deletions lib/src/impl/whitelistimpl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ import 'cleanerimpl.dart';
import 'tag.dart';

class WhitelistImpl implements Whitelist {
WhitelistImpl._(this._tags, this._attributes);

static final AttributeGenerator _noOp = (t, a, g) {};

static final Whitelist none = new WhitelistImpl._(const [], const []);

final List<Tag> _tags;
final List<Attribute> _attributes;
Cleaner _cleaner;

WhitelistImpl._(this._tags, this._attributes);
Cleaner? _cleaner;

@override
Whitelist tags(dynamic tags, {Filter when}) => new WhitelistImpl._(
Whitelist tags(dynamic tags, {Filter? when}) => new WhitelistImpl._(
(new List.from(_tags)..add(new Tag(_toMatcher(tags), when ?? always))),
_attributes);

@override
Whitelist attributes(dynamic tags, dynamic attributes, {Filter when}) =>
Whitelist attributes(dynamic tags, dynamic attributes, {Filter? when}) =>
extraAttributes(
tags, _attributeCopier(_toMatcher(attributes), when ?? always));

@override
Whitelist extraAttributes(dynamic tags, AttributeGenerator generator,
{Filter when}) =>
Whitelist extraAttributes(dynamic tags, AttributeGenerator? generator,
{Filter? when}) =>
new WhitelistImpl._(
_tags,
(new List.from(_attributes)
Expand All @@ -49,7 +49,7 @@ class WhitelistImpl implements Whitelist {
if (_cleaner == null) {
_cleaner = new CleanerImpl(_tags, _attributes);
}
return _cleaner;
return _cleaner!;
}

Matcher _toMatcher(dynamic matcher) {
Expand Down
11 changes: 4 additions & 7 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ description: >
The main purpose is to convert html from an untrusted source
to a version that's safe for rendering.
authors:
- Roel Spilker <[email protected]>

homepage: https://github.com/topdesk/dart-html-whitelist
documentation:

environment:
sdk: '>=1.21.0 <3.0.0'
sdk: '>=2.14.0 <3.0.0'

dependencies:
html: '^0.13.2+2'
html: ^0.15.0

dev_dependencies:
test: '^1.3.0'
test: any
lints: 1.0.1
41 changes: 4 additions & 37 deletions test/collector_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ void main() {
..['foo'] = 'foo'),
'<x foo="foo"></x>');
});

test('`null` name throws ArgumentError', () {
expect(() => a()[null] = 'bar', throwsArgumentError);
});

test('`null` value throws ArgumentError', () {
expect(() => a()['foo'] = null, throwsArgumentError);
});
});

group('ifAbsent(String name, String value)', () {
Expand All @@ -57,17 +49,12 @@ void main() {
});

test('works only once per attribute', () {
expect(string(a()..ifAbsent('foo', 'bar')..ifAbsent('foo', 'foo')),
expect(
string(a()
..ifAbsent('foo', 'bar')
..ifAbsent('foo', 'foo')),
'<x foo="bar"></x>');
});

test('`null` name throws ArgumentError', () {
expect(() => a().ifAbsent(null, 'bar'), throwsArgumentError);
});

test('`null` value throws ArgumentError', () {
expect(() => a().ifAbsent('foo', null), throwsArgumentError);
});
});

group('append(String name, String value)', () {
Expand All @@ -91,14 +78,6 @@ void main() {
..append('foo', 'foo')),
'<x foo="bar foo foo"></x>');
});

test('`null` name throws ArgumentError', () {
expect(() => a().append(null, 'bar'), throwsArgumentError);
});

test('`null` value throws ArgumentError', () {
expect(() => a().append('foo', null), throwsArgumentError);
});
});

group('append(String name, String value, {String separator})', () {
Expand Down Expand Up @@ -145,14 +124,6 @@ void main() {
..prepend('foo', 'foo')),
'<x foo="foo foo bar"></x>');
});

test('`null` name throws ArgumentError', () {
expect(() => a().prepend(null, 'bar'), throwsArgumentError);
});

test('`null` value throws ArgumentError', () {
expect(() => a().prepend('foo', null), throwsArgumentError);
});
});

group('prepend(String name, String value, {String separator})', () {
Expand Down Expand Up @@ -199,10 +170,6 @@ void main() {
..remove('bar')),
'<x foo="bar"></x>');
});

test('`null` name throws ArgumentError', () {
expect(() => a().remove(null), throwsArgumentError);
});
});

group('fill(Element element))', () {
Expand Down
16 changes: 8 additions & 8 deletions test/whitelist_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ void main() {

final basic = simpleText +
' <a href="#">a</a><blockquote>blockquote</blockquote><br>'
'<cite>cite</cite><code>code</code><dl><dt>dt</dt><dd>dd</dd></dl>'
'<kbd>kbd</kbd><ol><li>olli</li></ol><p>p</p><pre>pre</pre><q>q</q>'
'<samp>samp</samp><small>small</small><span>span</span>'
'<strike>strike</strike><sub>sub</sub><sup>sup</sup>'
'<ul><li>ulli</li></ul><var>var</var> ';
'<cite>cite</cite><code>code</code><dl><dt>dt</dt><dd>dd</dd></dl>'
'<kbd>kbd</kbd><ol><li>olli</li></ol><p>p</p><pre>pre</pre><q>q</q>'
'<samp>samp</samp><small>small</small><span>span</span>'
'<strike>strike</strike><sub>sub</sub><sup>sup</sup>'
'<ul><li>ulli</li></ul><var>var</var> ';

final basicWithImages = basic + ' <img> ';

Expand All @@ -26,7 +26,7 @@ void main() {
final basicWithImagesContents = ' ' + tooMuchContents;

final basicContents = ' ablockquotecitecodedtddkbdollippreqsamp'
'smallspanstrikesubsupullivar ' +
'smallspanstrikesubsupullivar ' +
basicWithImagesContents;

final textContents = ' bemistrongu ' + basicContents;
Expand Down Expand Up @@ -242,7 +242,7 @@ void main() {
Whitelist.none
.tags(anyTag)
.attributes('a', 'href',
when: (t, a) => a['href'].startsWith('f'))
when: (t, a) => a['href']!.startsWith('f'))
.safeCopy('<a href="foo">foo</a><a href="#">hash</a>'),
'<a href="foo">foo</a><a>hash</a>');
});
Expand All @@ -260,7 +260,7 @@ void main() {
Whitelist.none
.tags(anyTag)
.attributes(anyTag, 'href',
when: (t, a) => a['href'].startsWith('f'))
when: (t, a) => a['href']!.startsWith('f'))
.safeCopy('<a href="foo">foo</a><a class="bar">bar</a>'),
'<a href="foo">foo</a><a>bar</a>');
});
Expand Down

0 comments on commit f1ff2c2

Please sign in to comment.