Skip to content

Commit c28738b

Browse files
authored
Refactor: Safely access 'last' element in operations
Refactor: Safely access 'last' element in operations The previous code directly accessed `operations.last`, which could lead to a `NoSuchElementException` if the `operations` list was empty. This commit changes the access to `operations.lastOrNull?.data`, which safely returns `null` if the list is empty, preventing potential runtime errors. ```
1 parent 1b1efa1 commit c28738b

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

lib/parser/html_to_delta.dart

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,18 @@ class HtmlToDelta {
106106
.join()
107107
.removeAllNewLines;
108108
final Delta delta = Delta();
109-
final dom.Document $document = dparser.parse(replaceNormalNewLinesToBr
110-
? parsedText.transformNewLinesToBrTag
111-
: parsedText);
109+
final dom.Document $document = dparser.parse(replaceNormalNewLinesToBr ? parsedText.transformNewLinesToBrTag : parsedText);
112110
final dom.Element? $body = $document.body;
113111
final dom.Element? $html = $document.documentElement;
114112

115113
// Determine nodes to process: <body>, <html>, or document nodes if neither is present
116-
final List<dom.Node> nodesToProcess =
117-
$body?.nodes ?? $html?.nodes ?? $document.nodes;
114+
final List<dom.Node> nodesToProcess = $body?.nodes ?? $html?.nodes ?? $document.nodes;
118115

119116
for (int i = 0; i < nodesToProcess.length; i++) {
120117
dom.Node node = nodesToProcess[i];
121118
//first just verify if the customBlocks aren't empty and then store on them to
122119
//validate if one of them make match with the current Node
123-
if (customBlocks != null &&
124-
customBlocks!.isNotEmpty &&
125-
node is dom.Element) {
120+
if (customBlocks != null && customBlocks!.isNotEmpty && node is dom.Element) {
126121
for (CustomHtmlPart customBlock in customBlocks!) {
127122
if (customBlock.matches(node)) {
128123
final operations = customBlock.convert(node);
@@ -137,10 +132,7 @@ class HtmlToDelta {
137132

138133
bool nextIsBlock = nextNode is dom.Element ? nextNode.isBlock : false;
139134
if (isBlockValidator != null) {
140-
nextIsBlock = isBlockValidator?.call(nextNode is dom.Element
141-
? nextNode.localName ?? 'no-localname'
142-
: 'text-node') ??
143-
false;
135+
nextIsBlock = isBlockValidator?.call(nextNode is dom.Element ? nextNode.localName ?? 'no-localname' : 'text-node') ?? false;
144136
}
145137

146138
final List<Operation> operations = nodeToOperation(
@@ -155,8 +147,7 @@ class HtmlToDelta {
155147
delta.insert(op.data, op.attributes);
156148
}
157149
}
158-
final bool? shouldInsertNewLine = shouldInsertANewLine?.call(
159-
node is dom.Element ? node.localName ?? 'no-localname' : 'text-node');
150+
final bool? shouldInsertNewLine = shouldInsertANewLine?.call(node is dom.Element ? node.localName ?? 'no-localname' : 'text-node');
160151
if (shouldInsertNewLine != null && shouldInsertNewLine) {
161152
delta.insert('\n');
162153
}
@@ -166,9 +157,7 @@ class HtmlToDelta {
166157
final Operation lastOpdata = delta.last;
167158
final bool lastDataIsNotNewLine = lastOpdata.data.toString() != '\n';
168159
final bool hasAttributes = lastOpdata.attributes != null;
169-
if (lastDataIsNotNewLine && hasAttributes ||
170-
lastDataIsNotNewLine ||
171-
!lastDataIsNotNewLine && hasAttributes) {
160+
if (lastDataIsNotNewLine && hasAttributes || lastDataIsNotNewLine || !lastDataIsNotNewLine && hasAttributes) {
172161
delta.insert('\n');
173162
}
174163
}
@@ -201,14 +190,11 @@ class HtmlToDelta {
201190
final dom.Element? $html = $document.documentElement;
202191

203192
// Determine nodes to process: <body>, <html>, or document nodes if neither is present
204-
final List<dom.Node> nodesToProcess =
205-
$body?.nodes ?? $html?.nodes ?? $document.nodes;
193+
final List<dom.Node> nodesToProcess = $body?.nodes ?? $html?.nodes ?? $document.nodes;
206194

207195
for (int i = 0; i < nodesToProcess.length; i++) {
208196
dom.Node node = nodesToProcess[i];
209-
if (customBlocks != null &&
210-
customBlocks!.isNotEmpty &&
211-
node is dom.Element) {
197+
if (customBlocks != null && customBlocks!.isNotEmpty && node is dom.Element) {
212198
for (var customBlock in customBlocks!) {
213199
if (customBlock.matches(node)) {
214200
final operations = customBlock.convert(node);
@@ -226,10 +212,7 @@ class HtmlToDelta {
226212
? false
227213
: nextNode.isBlock;
228214
if (isBlockValidator != null) {
229-
nextIsBlock = isBlockValidator?.call(nextNode is dom.Element
230-
? nextNode.localName ?? 'no-localname'
231-
: 'text-node') ??
232-
false;
215+
nextIsBlock = isBlockValidator?.call(nextNode is dom.Element ? nextNode.localName ?? 'no-localname' : 'text-node') ?? false;
233216
}
234217
final List<Operation> operations = nodeToOperation(
235218
node,
@@ -242,8 +225,7 @@ class HtmlToDelta {
242225
delta.insert(op.data, op.attributes);
243226
}
244227
}
245-
final bool? shouldInsertNewLine = shouldInsertANewLine?.call(
246-
node is dom.Element ? node.localName ?? 'no-localname' : 'text-node');
228+
final bool? shouldInsertNewLine = shouldInsertANewLine?.call(node is dom.Element ? node.localName ?? 'no-localname' : 'text-node');
247229
if (shouldInsertNewLine != null && shouldInsertNewLine) {
248230
delta.insert('\n');
249231
}
@@ -252,9 +234,7 @@ class HtmlToDelta {
252234
final Operation lastOpdata = delta.last;
253235
final bool lastDataIsNotNewLine = lastOpdata.data.toString() != '\n';
254236
final bool hasAttributes = lastOpdata.attributes != null;
255-
if (lastDataIsNotNewLine && hasAttributes ||
256-
lastDataIsNotNewLine ||
257-
!lastDataIsNotNewLine && hasAttributes) {
237+
if (lastDataIsNotNewLine && hasAttributes || lastDataIsNotNewLine || !lastDataIsNotNewLine && hasAttributes) {
258238
delta.insert('\n');
259239
}
260240
return delta;
@@ -301,7 +281,7 @@ class HtmlToDelta {
301281
// Check if the nextElement is a block AND if the last
302282
// operation already has a new line, this would otherwise
303283
// create a double new line
304-
if (nextIsBlock && operations.last.data != '\n') {
284+
if (nextIsBlock && operations.lastOrNull?.data != '\n') {
305285
operations.add(Operation.insert('\n'));
306286
}
307287
}

0 commit comments

Comments
 (0)