Skip to content

Commit

Permalink
fix(pkgs): Fix munching font colors
Browse files Browse the repository at this point in the history
This commit fixed the color parsing process for `font` type html nodes:
* Parse color with web color name. e.g. `Aqua`
* Parse color in hex format with hash tag `#` prefix. e.g. `#FF00aa`
* Parse color in hex format without hash tag `#`. e.g. `FF00aa` **new**
  • Loading branch information
realth000 committed Feb 14, 2024
1 parent 2dbdb93 commit 5de71fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Fixed

- 修复积分统计页面我的积分标签页中缺少空隙的问题。
- 修复部分楼层丢失字体颜色的问题。

## [0.6.0] - 2024-02-08

Expand Down
19 changes: 14 additions & 5 deletions lib/packages/html_muncher/lib/src/html_muncher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class Muncher {
/// Munch state to use when munching.
final MunchState state = MunchState();

static final _colorRe = RegExp(r'^(#)?[0-9a-fA-F]{1,6}$');

/// Map to store div classes and corresponding munch functions.
Map<String, InlineSpan Function(uh.Element)>? _divMap;

Expand Down Expand Up @@ -644,11 +646,18 @@ class Muncher {
// Set to an invalid color value if "color" attribute not found.
final attr = colorString ?? element.attributes['color'];
int? colorValue;
if (attr != null && attr.startsWith('#')) {
colorValue = int.tryParse(
element.attributes['color']?.substring(1).padLeft(8, 'ff') ?? 'g',
radix: 16,
);
if (attr != null && _colorRe.hasMatch(attr)) {
if (attr.startsWith('#')) {
colorValue = int.tryParse(
element.attributes['color']?.substring(1).padLeft(8, 'ff') ?? 'g',
radix: 16,
);
} else {
colorValue = int.tryParse(
element.attributes['color']?.padLeft(8, 'ff') ?? 'g',
radix: 16,
);
}
}
Color? color;
if (colorValue != null) {
Expand Down

0 comments on commit 5de71fc

Please sign in to comment.