From 910f6d703ed0193318fb095be09e8ddf1bec3e05 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Fri, 26 Jan 2024 09:15:50 -0800 Subject: [PATCH] Update lints, require Dart 3.2 (#236) --- .github/workflows/test-package.yml | 2 +- CHANGELOG.md | 1 + lib/dom.dart | 2 +- lib/dom_parsing.dart | 26 +++++++---------- lib/parser.dart | 46 ++++++++++++++---------------- lib/src/constants.dart | 25 ++++++---------- lib/src/css_class_set.dart | 2 +- lib/src/html_input_stream.dart | 16 ++++------- lib/src/query_selector.dart | 33 +++++++++------------ lib/src/token.dart | 8 +++--- lib/src/tokenizer.dart | 2 +- lib/src/treebuilder.dart | 4 +-- pubspec.yaml | 6 ++-- 13 files changed, 75 insertions(+), 98 deletions(-) diff --git a/.github/workflows/test-package.yml b/.github/workflows/test-package.yml index aa34b6b..8e69bc4 100644 --- a/.github/workflows/test-package.yml +++ b/.github/workflows/test-package.yml @@ -32,7 +32,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - sdk: [2.19.0, stable, dev] + sdk: [3.2, stable, dev] steps: - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 - uses: dart-lang/setup-dart@ca7e6fee45ffbd82b555a7ebfc236d2c86439f5b diff --git a/CHANGELOG.md b/CHANGELOG.md index fa79fbc..c0d6cb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.15.5-wip +- Require Dart `3.2`. ## 0.15.4 diff --git a/lib/dom.dart b/lib/dom.dart index 5b8f8c9..733362a 100644 --- a/lib/dom.dart +++ b/lib/dom.dart @@ -112,7 +112,7 @@ mixin _NonElementParentNode implements _ParentNode { // common methods from these: // http://dom.spec.whatwg.org/#interface-document // http://dom.spec.whatwg.org/#element -abstract class _ElementAndDocument implements _ParentNode { +abstract mixin class _ElementAndDocument implements _ParentNode { // TODO(jmesserly): could be faster, should throw on invalid tag/class names. List getElementsByTagName(String localName) => diff --git a/lib/dom_parsing.dart b/lib/dom_parsing.dart index 35c8dc7..5cf3cab 100644 --- a/lib/dom_parsing.dart +++ b/lib/dom_parsing.dart @@ -12,22 +12,16 @@ export 'html_escape.dart'; /// A simple tree visitor for the DOM nodes. class TreeVisitor { void visit(Node node) { - switch (node.nodeType) { - case Node.ELEMENT_NODE: - return visitElement(node as Element); - case Node.TEXT_NODE: - return visitText(node as Text); - case Node.COMMENT_NODE: - return visitComment(node as Comment); - case Node.DOCUMENT_FRAGMENT_NODE: - return visitDocumentFragment(node as DocumentFragment); - case Node.DOCUMENT_NODE: - return visitDocument(node as Document); - case Node.DOCUMENT_TYPE_NODE: - return visitDocumentType(node as DocumentType); - default: - throw UnsupportedError('DOM node type ${node.nodeType}'); - } + return switch (node.nodeType) { + Node.ELEMENT_NODE => visitElement(node as Element), + Node.TEXT_NODE => visitText(node as Text), + Node.COMMENT_NODE => visitComment(node as Comment), + Node.DOCUMENT_FRAGMENT_NODE => + visitDocumentFragment(node as DocumentFragment), + Node.DOCUMENT_NODE => visitDocument(node as Document), + Node.DOCUMENT_TYPE_NODE => visitDocumentType(node as DocumentType), + _ => throw UnsupportedError('DOM node type ${node.nodeType}') + }; } void visitChildren(Node node) { diff --git a/lib/parser.dart b/lib/parser.dart index 3eb7a97..3c6340a 100644 --- a/lib/parser.dart +++ b/lib/parser.dart @@ -633,7 +633,7 @@ class Phase { } class InitialPhase extends Phase { - InitialPhase(HtmlParser parser) : super(parser); + InitialPhase(super.parser); @override Token? processSpaceCharacters(SpaceCharactersToken token) { @@ -788,7 +788,7 @@ class InitialPhase extends Phase { } class BeforeHtmlPhase extends Phase { - BeforeHtmlPhase(HtmlParser parser) : super(parser); + BeforeHtmlPhase(super.parser); // helper methods void insertHtmlElement() { @@ -849,7 +849,7 @@ class BeforeHtmlPhase extends Phase { } class BeforeHeadPhase extends Phase { - BeforeHeadPhase(HtmlParser parser) : super(parser); + BeforeHeadPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -923,7 +923,7 @@ class BeforeHeadPhase extends Phase { } class InHeadPhase extends Phase { - InHeadPhase(HtmlParser parser) : super(parser); + InHeadPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -1070,7 +1070,7 @@ class InHeadPhase extends Phase { // class InHeadNoScriptPhase extends Phase { class AfterHeadPhase extends Phase { - AfterHeadPhase(HtmlParser parser) : super(parser); + AfterHeadPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -1189,7 +1189,7 @@ class InBodyPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///parsing-main-inbody // the really-really-really-very crazy mode - InBodyPhase(HtmlParser parser) : super(parser); + InBodyPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -2227,7 +2227,7 @@ class InBodyPhase extends Phase { } class TextPhase extends Phase { - TextPhase(HtmlParser parser) : super(parser); + TextPhase(super.parser); // "Tried to process start tag %s in RCDATA/RAWTEXT mode"%token.name @override @@ -2277,7 +2277,7 @@ class TextPhase extends Phase { class InTablePhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-table - InTablePhase(HtmlParser parser) : super(parser); + InTablePhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -2507,9 +2507,7 @@ class InTableTextPhase extends Phase { Phase? originalPhase; List characterTokens; - InTableTextPhase(HtmlParser parser) - : characterTokens = [], - super(parser); + InTableTextPhase(super.parser) : characterTokens = []; void flushCharacters() { if (characterTokens.isEmpty) return; @@ -2578,7 +2576,7 @@ class InTableTextPhase extends Phase { class InCaptionPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-caption - InCaptionPhase(HtmlParser parser) : super(parser); + InCaptionPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -2700,7 +2698,7 @@ class InCaptionPhase extends Phase { class InColumnGroupPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-column - InColumnGroupPhase(HtmlParser parser) : super(parser); + InColumnGroupPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -2788,7 +2786,7 @@ class InColumnGroupPhase extends Phase { class InTableBodyPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-table0 - InTableBodyPhase(HtmlParser parser) : super(parser); + InTableBodyPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -2927,7 +2925,7 @@ class InTableBodyPhase extends Phase { class InRowPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-row - InRowPhase(HtmlParser parser) : super(parser); + InRowPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3073,7 +3071,7 @@ class InRowPhase extends Phase { class InCellPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-cell - InCellPhase(HtmlParser parser) : super(parser); + InCellPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3197,7 +3195,7 @@ class InCellPhase extends Phase { } class InSelectPhase extends Phase { - InSelectPhase(HtmlParser parser) : super(parser); + InSelectPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3353,7 +3351,7 @@ class InSelectPhase extends Phase { } class InSelectInTablePhase extends Phase { - InSelectInTablePhase(HtmlParser parser) : super(parser); + InSelectInTablePhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3479,7 +3477,7 @@ class InForeignContentPhase extends Phase { 'var' ]; - InForeignContentPhase(HtmlParser parser) : super(parser); + InForeignContentPhase(super.parser); void adjustSVGTagNames(StartTagToken token) { final replacements = const { @@ -3609,7 +3607,7 @@ class InForeignContentPhase extends Phase { } class AfterBodyPhase extends Phase { - AfterBodyPhase(HtmlParser parser) : super(parser); + AfterBodyPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3681,7 +3679,7 @@ class AfterBodyPhase extends Phase { class InFramesetPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///in-frameset - InFramesetPhase(HtmlParser parser) : super(parser); + InFramesetPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3774,7 +3772,7 @@ class InFramesetPhase extends Phase { class AfterFramesetPhase extends Phase { // http://www.whatwg.org/specs/web-apps/current-work///after3 - AfterFramesetPhase(HtmlParser parser) : super(parser); + AfterFramesetPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3831,7 +3829,7 @@ class AfterFramesetPhase extends Phase { } class AfterAfterBodyPhase extends Phase { - AfterAfterBodyPhase(HtmlParser parser) : super(parser); + AfterAfterBodyPhase(super.parser); @override Token? processStartTag(StartTagToken token) { @@ -3882,7 +3880,7 @@ class AfterAfterBodyPhase extends Phase { } class AfterAfterFramesetPhase extends Phase { - AfterAfterFramesetPhase(HtmlParser parser) : super(parser); + AfterAfterFramesetPhase(super.parser); @override Token? processStartTag(StartTagToken token) { diff --git a/lib/src/constants.dart b/lib/src/constants.dart index 5c41f81..abd483b 100644 --- a/lib/src/constants.dart +++ b/lib/src/constants.dart @@ -246,22 +246,15 @@ class Namespaces { Namespaces._(); static String? getPrefix(String? url) { - switch (url) { - case html: - return 'html'; - case mathml: - return 'math'; - case svg: - return 'svg'; - case xlink: - return 'xlink'; - case xml: - return 'xml'; - case xmlns: - return 'xmlns'; - default: - return null; - } + return switch (url) { + html => 'html', + mathml => 'math', + svg => 'svg', + xlink => 'xlink', + xml => 'xml', + xmlns => 'xmlns', + _ => null + }; } } diff --git a/lib/src/css_class_set.dart b/lib/src/css_class_set.dart index 508fd2b..deca7a5 100644 --- a/lib/src/css_class_set.dart +++ b/lib/src/css_class_set.dart @@ -6,7 +6,7 @@ import 'dart:collection'; -import 'package:html/dom.dart'; +import '../dom.dart'; class ElementCssClassSet extends _CssClassSetImpl { final Element _element; diff --git a/lib/src/html_input_stream.dart b/lib/src/html_input_stream.dart index 1c1bcc8..abae4cf 100644 --- a/lib/src/html_input_stream.dart +++ b/lib/src/html_input_stream.dart @@ -329,16 +329,12 @@ bool _hasUtf8Bom(List bytes, [int offset = 0, int? length]) { /// the codepoints. Supports the major unicode encodings as well as ascii and /// and windows-1252 encodings. List _decodeBytes(String encoding, List bytes) { - switch (encoding) { - case 'ascii': - return ascii.decode(bytes).codeUnits; - - case 'utf-8': + return switch (encoding) { + 'ascii' => ascii.decode(bytes).codeUnits, + 'utf-8' => // NOTE: To match the behavior of the other decode functions, we eat the // UTF-8 BOM here. This is the default behavior of `utf8.decode`. - return utf8.decode(bytes).codeUnits; - - default: - throw ArgumentError('Encoding $encoding not supported'); - } + utf8.decode(bytes).codeUnits, + _ => throw ArgumentError('Encoding $encoding not supported') + }; } diff --git a/lib/src/query_selector.dart b/lib/src/query_selector.dart index c45d11c..96fe4d2 100644 --- a/lib/src/query_selector.dart +++ b/lib/src/query_selector.dart @@ -3,8 +3,9 @@ library html.src.query; import 'package:csslib/parser.dart'; import 'package:csslib/visitor.dart'; -import 'package:html/dom.dart'; -import 'package:html/src/constants.dart' show isWhitespaceCC; + +import '../dom.dart'; +import 'constants.dart' show isWhitespaceCC; bool matches(Element node, String selector) => SelectorEvaluator().matches(node, _parseSelectorList(selector)); @@ -284,22 +285,16 @@ class SelectorEvaluator extends Visitor { if (node.operatorKind == TokenKind.NO_MATCH) return true; final select = '${node.value}'; - switch (node.operatorKind) { - case TokenKind.EQUALS: - return value == select; - case TokenKind.INCLUDES: - return value.split(' ').any((v) => v.isNotEmpty && v == select); - case TokenKind.DASH_MATCH: - return value.startsWith(select) && - (value.length == select.length || value[select.length] == '-'); - case TokenKind.PREFIX_MATCH: - return value.startsWith(select); - case TokenKind.SUFFIX_MATCH: - return value.endsWith(select); - case TokenKind.SUBSTRING_MATCH: - return value.contains(select); - default: - throw _unsupported(node); - } + return switch (node.operatorKind) { + TokenKind.EQUALS => value == select, + TokenKind.INCLUDES => + value.split(' ').any((v) => v.isNotEmpty && v == select), + TokenKind.DASH_MATCH => value.startsWith(select) && + (value.length == select.length || value[select.length] == '-'), + TokenKind.PREFIX_MATCH => value.startsWith(select), + TokenKind.SUFFIX_MATCH => value.endsWith(select), + TokenKind.SUBSTRING_MATCH => value.contains(select), + _ => throw _unsupported(node) + }; } } diff --git a/lib/src/token.dart b/lib/src/token.dart index 7a4e743..bbcec26 100644 --- a/lib/src/token.dart +++ b/lib/src/token.dart @@ -77,14 +77,14 @@ class ParseErrorToken extends StringToken { /// Extra information that goes along with the error message. Map? messageParams; - ParseErrorToken(String data, {this.messageParams}) : super(data); + ParseErrorToken(String super.data, {this.messageParams}); @override int get kind => TokenKind.parseError; } class CharactersToken extends StringToken { - CharactersToken([String? data]) : super(data); + CharactersToken([super.data]); @override int get kind => TokenKind.characters; @@ -98,14 +98,14 @@ class CharactersToken extends StringToken { } class SpaceCharactersToken extends StringToken { - SpaceCharactersToken([String? data]) : super(data); + SpaceCharactersToken([super.data]); @override int get kind => TokenKind.spaceCharacters; } class CommentToken extends StringToken { - CommentToken([String? data]) : super(data); + CommentToken([super.data]); @override int get kind => TokenKind.comment; diff --git a/lib/src/tokenizer.dart b/lib/src/tokenizer.dart index b1bfa36..3bed0b0 100644 --- a/lib/src/tokenizer.dart +++ b/lib/src/tokenizer.dart @@ -1,6 +1,6 @@ import 'dart:collection'; -import 'package:html/parser.dart' show HtmlParser; +import '../parser.dart' show HtmlParser; import 'constants.dart'; import 'html_input_stream.dart'; diff --git a/lib/src/treebuilder.dart b/lib/src/treebuilder.dart index 3e39700..a7a8e82 100644 --- a/lib/src/treebuilder.dart +++ b/lib/src/treebuilder.dart @@ -3,10 +3,10 @@ library treebuilder; import 'dart:collection'; -import 'package:html/dom.dart'; -import 'package:html/parser.dart' show getElementNameTuple; import 'package:source_span/source_span.dart'; +import '../dom.dart'; +import '../parser.dart' show getElementNameTuple; import 'constants.dart'; import 'list_proxy.dart'; import 'token.dart'; diff --git a/pubspec.yaml b/pubspec.yaml index e73d479..5824b76 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,13 +8,13 @@ topics: - web environment: - sdk: '>=2.19.0 <4.0.0' + sdk: ^3.2.0 dependencies: - csslib: '>=0.17.0 <2.0.0' + csslib: ^1.0.0 source_span: ^1.8.0 dev_dependencies: - dart_flutter_team_lints: ^1.0.0 + dart_flutter_team_lints: ^2.0.0 path: ^1.8.0 test: ^1.16.0