Skip to content

Commit

Permalink
Merge v2.2.4 - Optimize parse
Browse files Browse the repository at this point in the history
 - `$p`: added parameter `content`.
 - Added `$emsp`.
 - Optimized `$nbsp`.

 - Optimized `_parseStringNodes` and `_parseListNodes`.
  • Loading branch information
gmpassos authored May 25, 2024
2 parents c1ec2e4 + df773f5 commit d7e9990
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 2.2.4

- `$p`: added parameter `content`.
- Added `$emsp`.
- Optimized `$nbsp`.

- Optimized `_parseStringNodes` and `_parseListNodes`.

## 2.2.3

- Added `possiblyWithHTMLTag` and `possiblyWithHTMLEntity`.
Expand Down
25 changes: 19 additions & 6 deletions lib/src/dom_builder_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class DOMNode implements AsDOMNode {
if (isHTMLElement(s)) {
return parseHTML(s) ?? <DOMNode>[];
} else if (hasHTMLEntity(s) || hasHTMLTag(s)) {
return parseHTML('<span>$s</span>')!;
return [DOMElement._('span', content: parseHTML(s))];
} else {
return <DOMNode>[TextNode.toTextNode(s)];
}
Expand All @@ -228,8 +228,21 @@ class DOMNode implements AsDOMNode {
}
}

var list = l.whereNotNull().expand(parseNodes).toList();
return list;
var nodes = <DOMNode>[];

for (var e in l) {
if (e == null) continue;

var node = _parseNode(e);

if (node is DOMNode) {
nodes.add(node);
} else {
nodes.addAll(node as Iterable<DOMNode>);
}
}

return nodes;
}

static DOMNode? _parseSingleNode(Object o) {
Expand Down Expand Up @@ -866,12 +879,12 @@ class DOMNode implements AsDOMNode {
/// Sets the content of this node.
DOMNode setContent(Object? newContent) {
var nodes = DOMNode.parseNodes(newContent);
if (nodes.isNotEmpty) {
if (nodes.isEmpty) {
_content = null;
} else {
_content = nodes;
_setChildrenParent();
normalizeContent();
} else {
_content = null;
}
return this;
}
Expand Down
27 changes: 24 additions & 3 deletions lib/src/dom_builder_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -994,13 +994,15 @@ DOMElement $p(
Object? classes,
Object? style,
Map<String, String>? attributes,
Object? content,
bool? hidden,
bool commented = false}) =>
$tag('p',
id: id,
classes: classes,
style: style,
attributes: attributes,
content: content,
hidden: hidden,
commented: commented);

Expand All @@ -1020,16 +1022,35 @@ DOMElement $br({int amount = 1, bool commented = false}) {
}

String $nbsp([int length = 1]) {
if (length < 1) return '';
if (length == 1) {
return '&nbsp;';
} else if (length < 1) {
return '';
}

var s = StringBuffer('&nbsp;');
for (var i = 1; i < length; i++) {
var s = StringBuffer('&nbsp;&nbsp;');
for (var i = 2; i < length; i++) {
s.write('&nbsp;');
}

return s.toString();
}

String $emsp([int length = 1]) {
if (length == 1) {
return '&emsp;';
} else if (length < 1) {
return '';
}

var s = StringBuffer('&emsp;&emsp;');
for (var i = 2; i < length; i++) {
s.write('&emsp;');
}

return s.toString();
}

/// Creates a `hr` node.
DOMElement $hr(
{Object? id,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dom_builder
description: Generate and manipulate DOM elements (virtual or real), DSX (like JSX) and HTML declarations (Web and Native support).
version: 2.2.3
version: 2.2.4
homepage: https://github.com/gmpassos/dom_builder

environment:
Expand Down

0 comments on commit d7e9990

Please sign in to comment.