Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Update lints, require Dart 3.2 (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Jan 26, 2024
1 parent aaf7d1a commit 910f6d7
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.15.5-wip

- Require Dart `3.2`.

## 0.15.4

Expand Down
2 changes: 1 addition & 1 deletion lib/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element> getElementsByTagName(String localName) =>
Expand Down
26 changes: 10 additions & 16 deletions lib/dom_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 22 additions & 24 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ class Phase {
}

class InitialPhase extends Phase {
InitialPhase(HtmlParser parser) : super(parser);
InitialPhase(super.parser);

@override
Token? processSpaceCharacters(SpaceCharactersToken token) {
Expand Down Expand Up @@ -788,7 +788,7 @@ class InitialPhase extends Phase {
}

class BeforeHtmlPhase extends Phase {
BeforeHtmlPhase(HtmlParser parser) : super(parser);
BeforeHtmlPhase(super.parser);

// helper methods
void insertHtmlElement() {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -2507,9 +2507,7 @@ class InTableTextPhase extends Phase {
Phase? originalPhase;
List<StringToken> characterTokens;

InTableTextPhase(HtmlParser parser)
: characterTokens = <StringToken>[],
super(parser);
InTableTextPhase(super.parser) : characterTokens = <StringToken>[];

void flushCharacters() {
if (characterTokens.isEmpty) return;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3479,7 +3477,7 @@ class InForeignContentPhase extends Phase {
'var'
];

InForeignContentPhase(HtmlParser parser) : super(parser);
InForeignContentPhase(super.parser);

void adjustSVGTagNames(StartTagToken token) {
final replacements = const {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 9 additions & 16 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/css_class_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import 'dart:collection';

import 'package:html/dom.dart';
import '../dom.dart';

class ElementCssClassSet extends _CssClassSetImpl {
final Element _element;
Expand Down
16 changes: 6 additions & 10 deletions lib/src/html_input_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,12 @@ bool _hasUtf8Bom(List<int> bytes, [int offset = 0, int? length]) {
/// the codepoints. Supports the major unicode encodings as well as ascii and
/// and windows-1252 encodings.
List<int> _decodeBytes(String encoding, List<int> 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')
};
}
33 changes: 14 additions & 19 deletions lib/src/query_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
};
}
}
Loading

0 comments on commit 910f6d7

Please sign in to comment.