From 02bfa65f68fd0bd3cf5eee8a1601367ff3037d95 Mon Sep 17 00:00:00 2001 From: realth000 Date: Sun, 15 Sep 2024 04:19:26 +0800 Subject: [PATCH] fix(html): Fix parsing web colors * Fix parsing named colors. * Fix parsing hex color values. --- CHANGELOG.md | 1 + lib/utils/html/css_parser.dart | 6 +- lib/utils/html/web_colors.dart | 208 ++++++++++++++++----------------- pubspec.lock | 8 -- pubspec.yaml | 1 - 5 files changed, 102 insertions(+), 122 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3138816..4d12075c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - 消息:修复解析7天内的公共消息时失败的问题。 - 主页:修复部分网络不稳定的情况下将加载失败显示为未登录的问题。 - 网页:修复解析网页时,在折叠/展开按钮上的文字丢失颜色的问题。 +- 网页:修复解析网页中的颜色时,部分颜色解析失败或错误的问题。 ### Changed diff --git a/lib/utils/html/css_parser.dart b/lib/utils/html/css_parser.dart index 1ed6a6dc..af0d4c53 100644 --- a/lib/utils/html/css_parser.dart +++ b/lib/utils/html/css_parser.dart @@ -80,13 +80,13 @@ extension StringToColorExt on String? { // Parse as color value. if (this != null && _colorRe.hasMatch(this!)) { if (this!.startsWith('#')) { - colorValue = - int.tryParse(this!.substring(1).padLeft(8, 'ff'), radix: 16); + colorValue = int.tryParse(this!.substring(1), radix: 16); } else { - colorValue = int.tryParse(this!.padLeft(8, 'ff'), radix: 16); + colorValue = int.tryParse(this!, radix: 16); } } if (colorValue != null) { + colorValue += 0xFF000000; return Color(colorValue); } else { // If color not in format #aabcc, try parse as color name. diff --git a/lib/utils/html/web_colors.dart b/lib/utils/html/web_colors.dart index a1f22a5f..5a15bca0 100644 --- a/lib/utils/html/web_colors.dart +++ b/lib/utils/html/web_colors.dart @@ -1,7 +1,5 @@ import 'dart:ui'; -import 'package:change_case/change_case.dart'; - // ignore_for_file: public_member_api_docs enum WebColors { @@ -158,144 +156,144 @@ enum WebColors { const WebColors(this.color); factory WebColors.fromString(String? c) { - return switch (c?.toCamelCase()) { - 'aliceBlue' => WebColors.aliceBlue, - 'antiqueWhite' => WebColors.antiqueWhite, + return switch (c?.toLowerCase()) { + 'aliceblue' => WebColors.aliceBlue, + 'antiquewhite' => WebColors.antiqueWhite, 'aqua' => WebColors.aqua, 'aquamarine' => WebColors.aquamarine, 'azure' => WebColors.azure, 'beige' => WebColors.beige, 'bisque' => WebColors.bisque, 'black' => WebColors.black, - 'blanchedAlmond' => WebColors.blanchedAlmond, + 'blanchedalmond' => WebColors.blanchedAlmond, 'blue' => WebColors.blue, - 'blueViolet' => WebColors.blueViolet, + 'blueviolet' => WebColors.blueViolet, 'brown' => WebColors.brown, - 'burlyWood' => WebColors.burlyWood, - 'cadetBlue' => WebColors.cadetBlue, + 'burlywood' => WebColors.burlyWood, + 'cadetblue' => WebColors.cadetBlue, 'chartreuse' => WebColors.chartreuse, 'chocolate' => WebColors.chocolate, 'coral' => WebColors.coral, - 'cornflowerBlue' => WebColors.cornflowerBlue, + 'cornflowerblue' => WebColors.cornflowerBlue, 'cornsilk' => WebColors.cornsilk, 'crimson' => WebColors.crimson, 'cyan' => WebColors.cyan, - 'darkBlue' => WebColors.darkBlue, - 'darkCyan' => WebColors.darkCyan, - 'darkGoldenRod' => WebColors.darkGoldenRod, - 'darkGray' => WebColors.darkGray, - 'darkGrey' => WebColors.darkGrey, - 'darkGreen' => WebColors.darkGreen, - 'darkKhaki' => WebColors.darkKhaki, - 'darkMagenta' => WebColors.darkMagenta, - 'darkOliveGreen' => WebColors.darkOliveGreen, - 'darkOrange' => WebColors.darkOrange, - 'darkOrchid' => WebColors.darkOrchid, - 'darkRed' => WebColors.darkRed, - 'darkSalmon' => WebColors.darkSalmon, - 'darkSeaGreen' => WebColors.darkSeaGreen, - 'darkSlateBlue' => WebColors.darkSlateBlue, - 'darkSlateGray' => WebColors.darkSlateGray, - 'darkSlateGrey' => WebColors.darkSlateGrey, - 'darkTurquoise' => WebColors.darkTurquoise, - 'darkViolet' => WebColors.darkViolet, - 'deepPink' => WebColors.deepPink, - 'deepSkyBlue' => WebColors.deepSkyBlue, - 'dimGray' => WebColors.dimGray, - 'dimGrey' => WebColors.dimGrey, - 'dodgerBlue' => WebColors.dodgerBlue, - 'fireBrick' => WebColors.fireBrick, - 'floralWhite' => WebColors.floralWhite, - 'forestGreen' => WebColors.forestGreen, + 'darkblue' => WebColors.darkBlue, + 'darkcyan' => WebColors.darkCyan, + 'darkgoldenrod' => WebColors.darkGoldenRod, + 'darkgray' => WebColors.darkGray, + 'darkgrey' => WebColors.darkGrey, + 'darkgreen' => WebColors.darkGreen, + 'darkkhaki' => WebColors.darkKhaki, + 'darkmagenta' => WebColors.darkMagenta, + 'darkolivegreen' => WebColors.darkOliveGreen, + 'darkorange' => WebColors.darkOrange, + 'darkorchid' => WebColors.darkOrchid, + 'darkred' => WebColors.darkRed, + 'darksalmon' => WebColors.darkSalmon, + 'darkseagreen' => WebColors.darkSeaGreen, + 'darkslateblue' => WebColors.darkSlateBlue, + 'darkslategray' => WebColors.darkSlateGray, + 'darkslategrey' => WebColors.darkSlateGrey, + 'darkturquoise' => WebColors.darkTurquoise, + 'darkviolet' => WebColors.darkViolet, + 'deeppink' => WebColors.deepPink, + 'deepskyblue' => WebColors.deepSkyBlue, + 'dimgray' => WebColors.dimGray, + 'dimgrey' => WebColors.dimGrey, + 'dodgerblue' => WebColors.dodgerBlue, + 'firebrick' => WebColors.fireBrick, + 'floralwhite' => WebColors.floralWhite, + 'forestgreen' => WebColors.forestGreen, 'fuchsia' => WebColors.fuchsia, 'gainsboro' => WebColors.gainsboro, - 'ghostWhite' => WebColors.ghostWhite, + 'ghostwhite' => WebColors.ghostWhite, 'gold' => WebColors.gold, - 'goldenRod' => WebColors.goldenRod, + 'goldenrod' => WebColors.goldenRod, 'gray' => WebColors.gray, 'grey' => WebColors.grey, 'green' => WebColors.green, - 'greenYellow' => WebColors.greenYellow, - 'honeyDew' => WebColors.honeyDew, - 'hotPink' => WebColors.hotPink, - 'indianRed' => WebColors.indianRed, + 'greenyellow' => WebColors.greenYellow, + 'honeydew' => WebColors.honeyDew, + 'hotpink' => WebColors.hotPink, + 'indianred' => WebColors.indianRed, 'indigo' => WebColors.indigo, 'ivory' => WebColors.ivory, 'khaki' => WebColors.khaki, 'lavender' => WebColors.lavender, - 'lavenderBlush' => WebColors.lavenderBlush, - 'lawnGreen' => WebColors.lawnGreen, - 'lemonChiffon' => WebColors.lemonChiffon, - 'lightBlue' => WebColors.lightBlue, - 'lightCoral' => WebColors.lightCoral, - 'lightCyan' => WebColors.lightCyan, - 'lightGoldenRodYellow' => WebColors.lightGoldenRodYellow, - 'lightGray' => WebColors.lightGray, - 'lightGrey' => WebColors.lightGrey, - 'lightGreen' => WebColors.lightGreen, - 'lightPink' => WebColors.lightPink, - 'lightSalmon' => WebColors.lightSalmon, - 'lightSeaGreen' => WebColors.lightSeaGreen, - 'lightSkyBlue' => WebColors.lightSkyBlue, - 'lightSlateGray' => WebColors.lightSlateGray, - 'lightSlateGrey' => WebColors.lightSlateGrey, - 'lightSteelBlue' => WebColors.lightSteelBlue, - 'lightYellow' => WebColors.lightYellow, + 'lavenderblush' => WebColors.lavenderBlush, + 'lawngreen' => WebColors.lawnGreen, + 'lemonchiffon' => WebColors.lemonChiffon, + 'lightblue' => WebColors.lightBlue, + 'lightcoral' => WebColors.lightCoral, + 'lightcyan' => WebColors.lightCyan, + 'lightgoldenrodyellow' => WebColors.lightGoldenRodYellow, + 'lightgray' => WebColors.lightGray, + 'lightgrey' => WebColors.lightGrey, + 'lightgreen' => WebColors.lightGreen, + 'lightpink' => WebColors.lightPink, + 'lightsalmon' => WebColors.lightSalmon, + 'lightseagreen' => WebColors.lightSeaGreen, + 'lightskyblue' => WebColors.lightSkyBlue, + 'lightslategray' => WebColors.lightSlateGray, + 'lightslategrey' => WebColors.lightSlateGrey, + 'lightsteelblue' => WebColors.lightSteelBlue, + 'lightyellow' => WebColors.lightYellow, 'lime' => WebColors.lime, - 'limeGreen' => WebColors.limeGreen, + 'limegreen' => WebColors.limeGreen, 'linen' => WebColors.linen, 'magenta' => WebColors.magenta, 'maroon' => WebColors.maroon, - 'mediumAquaMarine' => WebColors.mediumAquaMarine, - 'mediumBlue' => WebColors.mediumBlue, - 'mediumOrchid' => WebColors.mediumOrchid, - 'mediumPurple' => WebColors.mediumPurple, - 'mediumSeaGreen' => WebColors.mediumSeaGreen, - 'mediumSlateBlue' => WebColors.mediumSlateBlue, - 'mediumSpringGreen' => WebColors.mediumSpringGreen, - 'mediumTurquoise' => WebColors.mediumTurquoise, - 'mediumVioletRed' => WebColors.mediumVioletRed, - 'midnightBlue' => WebColors.midnightBlue, - 'mintCream' => WebColors.mintCream, - 'mistyRose' => WebColors.mistyRose, + 'mediumaquamarine' => WebColors.mediumAquaMarine, + 'mediumblue' => WebColors.mediumBlue, + 'mediumorchid' => WebColors.mediumOrchid, + 'mediumpurple' => WebColors.mediumPurple, + 'mediumseagreen' => WebColors.mediumSeaGreen, + 'mediumslateblue' => WebColors.mediumSlateBlue, + 'mediumspringgreen' => WebColors.mediumSpringGreen, + 'mediumturquoise' => WebColors.mediumTurquoise, + 'mediumvioletred' => WebColors.mediumVioletRed, + 'midnightblue' => WebColors.midnightBlue, + 'mintcream' => WebColors.mintCream, + 'mistyrose' => WebColors.mistyRose, 'moccasin' => WebColors.moccasin, - 'navajoWhite' => WebColors.navajoWhite, + 'navajowhite' => WebColors.navajoWhite, 'navy' => WebColors.navy, - 'oldLace' => WebColors.oldLace, + 'oldlace' => WebColors.oldLace, 'olive' => WebColors.olive, - 'oliveDrab' => WebColors.oliveDrab, + 'olivedrab' => WebColors.oliveDrab, 'orange' => WebColors.orange, - 'orangeRed' => WebColors.orangeRed, + 'orangered' => WebColors.orangeRed, 'orchid' => WebColors.orchid, - 'paleGoldenRod' => WebColors.paleGoldenRod, - 'paleGreen' => WebColors.paleGreen, - 'paleTurquoise' => WebColors.paleTurquoise, - 'paleVioletRed' => WebColors.paleVioletRed, - 'papayaWhip' => WebColors.papayaWhip, - 'peachPuff' => WebColors.peachPuff, + 'palegoldenrod' => WebColors.paleGoldenRod, + 'palegreen' => WebColors.paleGreen, + 'paleturquoise' => WebColors.paleTurquoise, + 'palevioletred' => WebColors.paleVioletRed, + 'papayawhip' => WebColors.papayaWhip, + 'peachpuff' => WebColors.peachPuff, 'peru' => WebColors.peru, 'pink' => WebColors.pink, 'plum' => WebColors.plum, - 'powderBlue' => WebColors.powderBlue, + 'powderblue' => WebColors.powderBlue, 'purple' => WebColors.purple, - 'rebeccaPurple' => WebColors.rebeccaPurple, + 'rebeccapurple' => WebColors.rebeccaPurple, 'red' => WebColors.red, - 'rosyBrown' => WebColors.rosyBrown, - 'royalBlue' => WebColors.royalBlue, - 'saddleBrown' => WebColors.saddleBrown, + 'rosybrown' => WebColors.rosyBrown, + 'royalblue' => WebColors.royalBlue, + 'saddlebrown' => WebColors.saddleBrown, 'salmon' => WebColors.salmon, - 'sandyBrown' => WebColors.sandyBrown, - 'seaGreen' => WebColors.seaGreen, - 'seaShell' => WebColors.seaShell, + 'sandybrown' => WebColors.sandyBrown, + 'seagreen' => WebColors.seaGreen, + 'seashell' => WebColors.seaShell, 'sienna' => WebColors.sienna, 'silver' => WebColors.silver, - 'skyBlue' => WebColors.skyBlue, - 'slateBlue' => WebColors.slateBlue, - 'slateGray' => WebColors.slateGray, - 'slateGrey' => WebColors.slateGrey, + 'skyblue' => WebColors.skyBlue, + 'slateblue' => WebColors.slateBlue, + 'slategray' => WebColors.slateGray, + 'slategrey' => WebColors.slateGrey, 'snow' => WebColors.snow, - 'springGreen' => WebColors.springGreen, - 'steelBlue' => WebColors.steelBlue, + 'springgreen' => WebColors.springGreen, + 'steelblue' => WebColors.steelBlue, 'tan' => WebColors.tan, 'teal' => WebColors.teal, 'thistle' => WebColors.thistle, @@ -304,9 +302,9 @@ enum WebColors { 'violet' => WebColors.violet, 'wheat' => WebColors.wheat, 'white' => WebColors.white, - 'whiteSmoke' => WebColors.whiteSmoke, + 'whitesmoke' => WebColors.whiteSmoke, 'yellow' => WebColors.yellow, - 'yellowGreen' => WebColors.yellowGreen, + 'yellowgreen' => WebColors.yellowGreen, String() || null => WebColors.invalid, }; } @@ -317,13 +315,3 @@ enum WebColors { final Color color; } - -enum OperatingSystem { - macOS('Yes I can'), - windows(0), - linux(false); - - const OperatingSystem(this.canBuildForIos); - - final T canBuildForIos; -} diff --git a/pubspec.lock b/pubspec.lock index 6d2e089d..574345f3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -158,14 +158,6 @@ packages: url: "https://pub.dev" source: hosted version: "8.9.2" - change_case: - dependency: "direct main" - description: - name: change_case - sha256: "99cfdf2018c627c8a3af5a23ea4c414eb69c75c31322d23b9660ebc3cf30b514" - url: "https://pub.dev" - source: hosted - version: "2.1.0" characters: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4073a4b5..494ef72b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,6 @@ dependencies: ansicolor: any bbob_dart: ^0.2.1 bloc: ^8.1.4 - change_case: ^2.1.0 # Blocked by flutter collection: any cookie_jar: ^4.0.8