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

Drop Pair util; use Records #255

Merged
merged 1 commit into from
Aug 29, 2024
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
8 changes: 4 additions & 4 deletions lib/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ class HtmlParser {
return enc == 'text/html' || enc == 'application/xhtml+xml';
} else {
return htmlIntegrationPointElements
.contains(Pair(element.namespaceUri, element.localName));
.contains((element.namespaceUri, element.localName));
}
}

bool isMathMLTextIntegrationPoint(Element element) {
return mathmlTextIntegrationPointElements
.contains(Pair(element.namespaceUri, element.localName));
.contains((element.namespaceUri, element.localName));
}

bool inForeignContent(Token token, int type) {
Expand Down Expand Up @@ -3978,7 +3978,7 @@ class ParseError implements SourceSpanException {
}

/// Convenience function to get the pair of namespace and localName.
Pair<String, String?> getElementNameTuple(Element e) {
(String, String?) getElementNameTuple(Element e) {
final ns = e.namespaceUri ?? Namespaces.html;
return Pair(ns, e.localName);
return (ns, e.localName);
}
238 changes: 119 additions & 119 deletions lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,139 +258,139 @@ class Namespaces {
}
}

const List<Pair<String, String>> scopingElements = [
Pair(Namespaces.html, 'applet'),
Pair(Namespaces.html, 'caption'),
Pair(Namespaces.html, 'html'),
Pair(Namespaces.html, 'marquee'),
Pair(Namespaces.html, 'object'),
Pair(Namespaces.html, 'table'),
Pair(Namespaces.html, 'td'),
Pair(Namespaces.html, 'th'),
Pair(Namespaces.mathml, 'mi'),
Pair(Namespaces.mathml, 'mo'),
Pair(Namespaces.mathml, 'mn'),
Pair(Namespaces.mathml, 'ms'),
Pair(Namespaces.mathml, 'mtext'),
Pair(Namespaces.mathml, 'annotation-xml'),
Pair(Namespaces.svg, 'foreignObject'),
Pair(Namespaces.svg, 'desc'),
Pair(Namespaces.svg, 'title')
const List<(String, String)> scopingElements = [
(Namespaces.html, 'applet'),
(Namespaces.html, 'caption'),
(Namespaces.html, 'html'),
(Namespaces.html, 'marquee'),
(Namespaces.html, 'object'),
(Namespaces.html, 'table'),
(Namespaces.html, 'td'),
(Namespaces.html, 'th'),
(Namespaces.mathml, 'mi'),
(Namespaces.mathml, 'mo'),
(Namespaces.mathml, 'mn'),
(Namespaces.mathml, 'ms'),
(Namespaces.mathml, 'mtext'),
(Namespaces.mathml, 'annotation-xml'),
(Namespaces.svg, 'foreignObject'),
(Namespaces.svg, 'desc'),
(Namespaces.svg, 'title')
];

const formattingElements = [
Pair(Namespaces.html, 'a'),
Pair(Namespaces.html, 'b'),
Pair(Namespaces.html, 'big'),
Pair(Namespaces.html, 'code'),
Pair(Namespaces.html, 'em'),
Pair(Namespaces.html, 'font'),
Pair(Namespaces.html, 'i'),
Pair(Namespaces.html, 'nobr'),
Pair(Namespaces.html, 's'),
Pair(Namespaces.html, 'small'),
Pair(Namespaces.html, 'strike'),
Pair(Namespaces.html, 'strong'),
Pair(Namespaces.html, 'tt'),
Pair(Namespaces.html, '')
(Namespaces.html, 'a'),
(Namespaces.html, 'b'),
(Namespaces.html, 'big'),
(Namespaces.html, 'code'),
(Namespaces.html, 'em'),
(Namespaces.html, 'font'),
(Namespaces.html, 'i'),
(Namespaces.html, 'nobr'),
(Namespaces.html, 's'),
(Namespaces.html, 'small'),
(Namespaces.html, 'strike'),
(Namespaces.html, 'strong'),
(Namespaces.html, 'tt'),
(Namespaces.html, '')
];

const specialElements = [
Pair(Namespaces.html, 'address'),
Pair(Namespaces.html, 'applet'),
Pair(Namespaces.html, 'area'),
Pair(Namespaces.html, 'article'),
Pair(Namespaces.html, 'aside'),
Pair(Namespaces.html, 'base'),
Pair(Namespaces.html, 'basefont'),
Pair(Namespaces.html, 'bgsound'),
Pair(Namespaces.html, 'blockquote'),
Pair(Namespaces.html, 'body'),
Pair(Namespaces.html, 'br'),
Pair(Namespaces.html, 'button'),
Pair(Namespaces.html, 'caption'),
Pair(Namespaces.html, 'center'),
Pair(Namespaces.html, 'col'),
Pair(Namespaces.html, 'colgroup'),
Pair(Namespaces.html, 'command'),
Pair(Namespaces.html, 'dd'),
Pair(Namespaces.html, 'details'),
Pair(Namespaces.html, 'dir'),
Pair(Namespaces.html, 'div'),
Pair(Namespaces.html, 'dl'),
Pair(Namespaces.html, 'dt'),
Pair(Namespaces.html, 'embed'),
Pair(Namespaces.html, 'fieldset'),
Pair(Namespaces.html, 'figure'),
Pair(Namespaces.html, 'footer'),
Pair(Namespaces.html, 'form'),
Pair(Namespaces.html, 'frame'),
Pair(Namespaces.html, 'frameset'),
Pair(Namespaces.html, 'h1'),
Pair(Namespaces.html, 'h2'),
Pair(Namespaces.html, 'h3'),
Pair(Namespaces.html, 'h4'),
Pair(Namespaces.html, 'h5'),
Pair(Namespaces.html, 'h6'),
Pair(Namespaces.html, 'head'),
Pair(Namespaces.html, 'header'),
Pair(Namespaces.html, 'hr'),
Pair(Namespaces.html, 'html'),
Pair(Namespaces.html, 'iframe'),
(Namespaces.html, 'address'),
(Namespaces.html, 'applet'),
(Namespaces.html, 'area'),
(Namespaces.html, 'article'),
(Namespaces.html, 'aside'),
(Namespaces.html, 'base'),
(Namespaces.html, 'basefont'),
(Namespaces.html, 'bgsound'),
(Namespaces.html, 'blockquote'),
(Namespaces.html, 'body'),
(Namespaces.html, 'br'),
(Namespaces.html, 'button'),
(Namespaces.html, 'caption'),
(Namespaces.html, 'center'),
(Namespaces.html, 'col'),
(Namespaces.html, 'colgroup'),
(Namespaces.html, 'command'),
(Namespaces.html, 'dd'),
(Namespaces.html, 'details'),
(Namespaces.html, 'dir'),
(Namespaces.html, 'div'),
(Namespaces.html, 'dl'),
(Namespaces.html, 'dt'),
(Namespaces.html, 'embed'),
(Namespaces.html, 'fieldset'),
(Namespaces.html, 'figure'),
(Namespaces.html, 'footer'),
(Namespaces.html, 'form'),
(Namespaces.html, 'frame'),
(Namespaces.html, 'frameset'),
(Namespaces.html, 'h1'),
(Namespaces.html, 'h2'),
(Namespaces.html, 'h3'),
(Namespaces.html, 'h4'),
(Namespaces.html, 'h5'),
(Namespaces.html, 'h6'),
(Namespaces.html, 'head'),
(Namespaces.html, 'header'),
(Namespaces.html, 'hr'),
(Namespaces.html, 'html'),
(Namespaces.html, 'iframe'),
// Note that image is commented out in the spec as "this isn't an
// element that can end up on the stack, so it doesn't matter,"
Pair(Namespaces.html, 'image'),
Pair(Namespaces.html, 'img'),
Pair(Namespaces.html, 'input'),
Pair(Namespaces.html, 'isindex'),
Pair(Namespaces.html, 'li'),
Pair(Namespaces.html, 'link'),
Pair(Namespaces.html, 'listing'),
Pair(Namespaces.html, 'marquee'),
Pair(Namespaces.html, 'men'),
Pair(Namespaces.html, 'meta'),
Pair(Namespaces.html, 'nav'),
Pair(Namespaces.html, 'noembed'),
Pair(Namespaces.html, 'noframes'),
Pair(Namespaces.html, 'noscript'),
Pair(Namespaces.html, 'object'),
Pair(Namespaces.html, 'ol'),
Pair(Namespaces.html, 'p'),
Pair(Namespaces.html, 'param'),
Pair(Namespaces.html, 'plaintext'),
Pair(Namespaces.html, 'pre'),
Pair(Namespaces.html, 'script'),
Pair(Namespaces.html, 'section'),
Pair(Namespaces.html, 'select'),
Pair(Namespaces.html, 'style'),
Pair(Namespaces.html, 'table'),
Pair(Namespaces.html, 'tbody'),
Pair(Namespaces.html, 'td'),
Pair(Namespaces.html, 'textarea'),
Pair(Namespaces.html, 'tfoot'),
Pair(Namespaces.html, 'th'),
Pair(Namespaces.html, 'thead'),
Pair(Namespaces.html, 'title'),
Pair(Namespaces.html, 'tr'),
Pair(Namespaces.html, 'ul'),
Pair(Namespaces.html, 'wbr'),
Pair(Namespaces.html, 'xmp'),
Pair(Namespaces.svg, 'foreignObject')
(Namespaces.html, 'image'),
(Namespaces.html, 'img'),
(Namespaces.html, 'input'),
(Namespaces.html, 'isindex'),
(Namespaces.html, 'li'),
(Namespaces.html, 'link'),
(Namespaces.html, 'listing'),
(Namespaces.html, 'marquee'),
(Namespaces.html, 'men'),
(Namespaces.html, 'meta'),
(Namespaces.html, 'nav'),
(Namespaces.html, 'noembed'),
(Namespaces.html, 'noframes'),
(Namespaces.html, 'noscript'),
(Namespaces.html, 'object'),
(Namespaces.html, 'ol'),
(Namespaces.html, 'p'),
(Namespaces.html, 'param'),
(Namespaces.html, 'plaintext'),
(Namespaces.html, 'pre'),
(Namespaces.html, 'script'),
(Namespaces.html, 'section'),
(Namespaces.html, 'select'),
(Namespaces.html, 'style'),
(Namespaces.html, 'table'),
(Namespaces.html, 'tbody'),
(Namespaces.html, 'td'),
(Namespaces.html, 'textarea'),
(Namespaces.html, 'tfoot'),
(Namespaces.html, 'th'),
(Namespaces.html, 'thead'),
(Namespaces.html, 'title'),
(Namespaces.html, 'tr'),
(Namespaces.html, 'ul'),
(Namespaces.html, 'wbr'),
(Namespaces.html, 'xmp'),
(Namespaces.svg, 'foreignObject')
];

const htmlIntegrationPointElements = [
Pair(Namespaces.mathml, 'annotaion-xml'),
Pair(Namespaces.svg, 'foreignObject'),
Pair(Namespaces.svg, 'desc'),
Pair(Namespaces.svg, 'title')
(Namespaces.mathml, 'annotaion-xml'),
(Namespaces.svg, 'foreignObject'),
(Namespaces.svg, 'desc'),
(Namespaces.svg, 'title')
];

const mathmlTextIntegrationPointElements = [
Pair(Namespaces.mathml, 'mi'),
Pair(Namespaces.mathml, 'mo'),
Pair(Namespaces.mathml, 'mn'),
Pair(Namespaces.mathml, 'ms'),
Pair(Namespaces.mathml, 'mtext')
(Namespaces.mathml, 'mi'),
(Namespaces.mathml, 'mo'),
(Namespaces.mathml, 'mn'),
(Namespaces.mathml, 'ms'),
(Namespaces.mathml, 'mtext')
];

const spaceCharacters = ' \n\r\t\u000C';
Expand Down
17 changes: 8 additions & 9 deletions lib/src/treebuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import '../parser.dart' show getElementNameTuple;
import 'constants.dart';
import 'list_proxy.dart';
import 'token.dart';
import 'utils.dart';

/// Open elements in the formatting category, most recent element last.
///
Expand Down Expand Up @@ -113,29 +112,29 @@ class TreeBuilder {
final exactNode = target is Node;

var listElements1 = scopingElements;
var listElements2 = const <Pair<String, String>>[];
var listElements2 = const <(String, String)>[];
var invert = false;
if (variant != null) {
switch (variant) {
case 'button':
listElements2 = const [Pair(Namespaces.html, 'button')];
listElements2 = const [(Namespaces.html, 'button')];
break;
case 'list':
listElements2 = const [
Pair(Namespaces.html, 'ol'),
Pair(Namespaces.html, 'ul')
(Namespaces.html, 'ol'),
(Namespaces.html, 'ul')
];
break;
case 'table':
listElements1 = const [
Pair(Namespaces.html, 'html'),
Pair(Namespaces.html, 'table')
(Namespaces.html, 'html'),
(Namespaces.html, 'table')
];
break;
case 'select':
listElements1 = const [
Pair(Namespaces.html, 'optgroup'),
Pair(Namespaces.html, 'option')
(Namespaces.html, 'optgroup'),
(Namespaces.html, 'option')
];
invert = true;
break;
Expand Down
14 changes: 0 additions & 14 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import 'constants.dart';

class Pair<F, S> {
final F first;
final S second;

const Pair(this.first, this.second);

@override
int get hashCode => 37 * first.hashCode + second.hashCode;

@override
bool operator ==(Object other) =>
other is Pair && other.first == first && other.second == second;
}

bool startsWithAny(String str, List<String> prefixes) =>
prefixes.any(str.startsWith);

Expand Down