Skip to content

Commit 937038c

Browse files
authored
[flutter_markdown] fixes null check operator used on null value if onSelectionChanged is… (flutter#6883)
- Fixes flutter/issues/149817 - Fixes flutter/issues/146282
1 parent 1e62ab0 commit 937038c

File tree

4 files changed

+128
-79
lines changed

4 files changed

+128
-79
lines changed

packages/flutter_markdown/CHANGELOG.md

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.2+1
2+
3+
* Fixes a crash caused by text selection when `selectable` is true and `onSelectionChanged` is null.
4+
15
## 0.7.2
26

37
* Multiple code blocks within a single markdown will now use separate ScrollControllers.
@@ -160,198 +164,198 @@
160164

161165
## 0.6.10
162166

163-
* Update `markdown` dependency
167+
* Update `markdown` dependency
164168

165169
## 0.6.9+1
166170

167-
* Remove build status badge from `README.md`
171+
* Remove build status badge from `README.md`
168172

169173
## 0.6.9
170174

171-
* Leading spaces in a paragraph and in list items are now ignored according to [GFM #192](https://github.github.com/gfm/#example-192) and [GFM #236](https://github.github.com/gfm/#example-236).
175+
* Leading spaces in a paragraph and in list items are now ignored according to [GFM #192](https://github.github.com/gfm/#example-192) and [GFM #236](https://github.github.com/gfm/#example-236).
172176

173177
## 0.6.8
174178

175-
* Added option paddingBuilders
179+
* Added option paddingBuilders
176180

177181
## 0.6.7
178182

179-
* Fix `unnecessary_import` lint errors.
180-
* Added option pPadding
181-
* Added options h1Padding - h6Padding
183+
* Fix `unnecessary_import` lint errors.
184+
* Added option pPadding
185+
* Added options h1Padding - h6Padding
182186

183187
## 0.6.6
184188

185-
* Soft line break
189+
* Soft line break
186190

187191
## 0.6.5
188192

189-
* Fix unique Keys for RichText blocks
193+
* Fix unique Keys for RichText blocks
190194

191195
## 0.6.4
192196

193-
* Fix merging of spans when first span is not a TextSpan
197+
* Fix merging of spans when first span is not a TextSpan
194198

195199
## 0.6.3
196200

197-
* Fixed `onTap`, now the changed hyperlinks are reflected even with keeping the same link name unchanged.
201+
* Fixed `onTap`, now the changed hyperlinks are reflected even with keeping the same link name unchanged.
198202

199203
## 0.6.2
200204

201-
* Updated metadata for new source location
202-
* Style changes to conform to flutter/packages analyzer settings
205+
* Updated metadata for new source location
206+
* Style changes to conform to flutter/packages analyzer settings
203207

204-
## 0.6.1
208+
## 0.6.1
205209

206-
* Added builder option bulletBuilder
210+
* Added builder option bulletBuilder
207211

208212
## 0.6.0
209213

210-
* Null safety release
211-
* Added stylesheet option listBulletPadding
212-
* Fixed blockquote inline styling
213-
* Added onTapText handler for selectable text
214+
* Null safety release
215+
* Added stylesheet option listBulletPadding
216+
* Fixed blockquote inline styling
217+
* Added onTapText handler for selectable text
214218

215219
## 0.6.0-nullsafety.2
216220

217-
* Dependencies updated for null safety
221+
* Dependencies updated for null safety
218222

219223
## 0.6.0-nullsafety.1
220224

221-
* Fix null safety on web
222-
* Image test mocks fixed for null safety
225+
* Fix null safety on web
226+
* Image test mocks fixed for null safety
223227

224228
## 0.6.0-nullsafety.0
225229

226-
* Initial null safety migration.
230+
* Initial null safety migration.
227231

228232
## 0.5.2
229233

230-
* Added `MarkdownListItemCrossAxisAlignment` to allow for intrinsic height
234+
* Added `MarkdownListItemCrossAxisAlignment` to allow for intrinsic height
231235
measurements of lists.
232236

233237
## 0.5.1
234238

235-
* Fix user defined builders
239+
* Fix user defined builders
236240

237241
## 0.5.0
238242

239-
* BREAKING CHANGE: `MarkdownTapLinkCallback` now has three parameters, not one, exposing more
243+
* BREAKING CHANGE: `MarkdownTapLinkCallback` now has three parameters, not one, exposing more
240244
information about a tapped link.
241-
* Note for upgraders, the old single parameter `href` is now the second parameter to match the specification.
242-
* Android example upgraded
243-
* Test coverage updated to match GitHub Flavoured Markdown and CommonMark
244-
* Handle links with empty descriptions
245-
* Handle empty rows in tables
245+
* Note for upgraders, the old single parameter `href` is now the second parameter to match the specification.
246+
* Android example upgraded
247+
* Test coverage updated to match GitHub Flavoured Markdown and CommonMark
248+
* Handle links with empty descriptions
249+
* Handle empty rows in tables
246250

247251
## 0.4.4
248252

249-
* Fix handling of newline character in blockquote
250-
* Add new example demo
251-
* Use the start attribute in ordered list to set the first number
252-
* Revert changes made in PR #235 (which broke newline handling)
253+
* Fix handling of newline character in blockquote
254+
* Add new example demo
255+
* Use the start attribute in ordered list to set the first number
256+
* Revert changes made in PR #235 (which broke newline handling)
253257

254258
## 0.4.3
255259

256-
* Fix merging of `MarkdownStyleSheets`
257-
* Fix `MarkdownStyleSheet` textScaleFactor to use default value of 1.0, if not provided, instead using the textScaleFactor of the nearest MediaQuery
260+
* Fix merging of `MarkdownStyleSheets`
261+
* Fix `MarkdownStyleSheet` textScaleFactor to use default value of 1.0, if not provided, instead using the textScaleFactor of the nearest MediaQuery
258262

259263
## 0.4.2
260264

261-
* Fix parsing of image caption & alt attributes
262-
* Fix baseline alignment in lists
263-
* Support `LineBreakSyntax`
265+
* Fix parsing of image caption & alt attributes
266+
* Fix baseline alignment in lists
267+
* Support `LineBreakSyntax`
264268

265269
## 0.4.1
266270

267-
* Downgrade Flutter minimum from 1.17.1 to 1.17.0 for Pub
271+
* Downgrade Flutter minimum from 1.17.1 to 1.17.0 for Pub
268272

269273
## 0.4.0
270274

271-
* Updated for Flutter 1.17
272-
* Ignore newlines in paragraphs
273-
* Improve handling of horizontal rules
275+
* Updated for Flutter 1.17
276+
* Ignore newlines in paragraphs
277+
* Improve handling of horizontal rules
274278

275279
## 0.3.5
276280

277-
* Fix hardcoded colors and improve Darktheme
278-
* Fix text alignment when formatting is involved
281+
* Fix hardcoded colors and improve Darktheme
282+
* Fix text alignment when formatting is involved
279283

280284
## 0.3.4
281285

282-
* Add support for text paragraphs and blockquotes.
286+
* Add support for text paragraphs and blockquotes.
283287

284288
## 0.3.3
285289

286-
* Add the ability to control the scroll position of the `MarkdownWidget`.
290+
* Add the ability to control the scroll position of the `MarkdownWidget`.
287291

288292
## 0.3.2
289293

290-
* Uplift `package:markdown` dependency version to enable deleting HTML unescape URI workaround
291-
* Explictly state that Flutter 1.10.7 is the minimum supported Flutter version in the library `pubspec.yaml`.
294+
* Uplift `package:markdown` dependency version to enable deleting HTML unescape URI workaround
295+
* Explictly state that Flutter 1.10.7 is the minimum supported Flutter version in the library `pubspec.yaml`.
292296

293297
## 0.3.1
294298

295-
* Expose `tableColumnWidth`
296-
* Add `MarkdownStyleSheet.fromCupertinoTheme`
297-
* Fix `MarkdownStyleSheet.blockquote`
298-
* Flutter for web support
299-
* Add physic and shrinkWrap to Markdown widget
300-
* Add MarkdownBody.fitContent
301-
* Support select text to copy
302-
* Fix list bullet alignment
303-
* HTML unescape URIs (temporary workaround for [dart-lang/markdown #272](https://github.com/dart-lang/markdown/issues/272))
304-
* Rebuilt `example/android` and `example/ios` directories
299+
* Expose `tableColumnWidth`
300+
* Add `MarkdownStyleSheet.fromCupertinoTheme`
301+
* Fix `MarkdownStyleSheet.blockquote`
302+
* Flutter for web support
303+
* Add physic and shrinkWrap to Markdown widget
304+
* Add MarkdownBody.fitContent
305+
* Support select text to copy
306+
* Fix list bullet alignment
307+
* HTML unescape URIs (temporary workaround for [dart-lang/markdown #272](https://github.com/dart-lang/markdown/issues/272))
308+
* Rebuilt `example/android` and `example/ios` directories
305309

306310
**Note:** this version has an implicit minimum supported version of Flutter 1.10.7.
307311
See [flutter/flutter_markdown issue #156](https://github.com/flutter/flutter_markdown/issues/156) for more detail.
308312

309313
## 0.3.0
310314

311-
* Support GitHub flavoured Markdown
312-
* Support strikethrough
313-
* Convert TextSpan to use new InlineSpan API
315+
* Support GitHub flavoured Markdown
316+
* Support strikethrough
317+
* Convert TextSpan to use new InlineSpan API
314318

315319
## 0.2.0
316320

317-
* Updated environment sdk constraints to make the package
321+
* Updated environment sdk constraints to make the package
318322
Dart 2 compatible. As a result, usage of this version and higher
319323
requires a Dart 2 SDK.
320324

321325
## 0.1.6
322326

323-
* Updated `markdown` dependency.
327+
* Updated `markdown` dependency.
324328

325329
## 0.1.5
326330

327-
* Add `mockito` as a dev dependency. Eliminate use of `package:http`, which
331+
* Add `mockito` as a dev dependency. Eliminate use of `package:http`, which
328332
is no longer part of Flutter.
329333

330334
## 0.1.4
331335

332-
* Add `li` style to bullets
336+
* Add `li` style to bullets
333337

334338
## 0.1.3
335339

336-
* Add `path` and `http` as declared dependencies in `pubspec.yaml`
340+
* Add `path` and `http` as declared dependencies in `pubspec.yaml`
337341

338342
## 0.1.2
339343

340-
* Add support for horizontal rules.
341-
* Fix the `onTap` callback on images nested in hyperlinks
344+
* Add support for horizontal rules.
345+
* Fix the `onTap` callback on images nested in hyperlinks
342346

343347
## 0.1.1
344348

345-
* Add support for local file paths in image links. Make sure to set the
349+
* Add support for local file paths in image links. Make sure to set the
346350
`imageDirectory` property to specify the base directory containing the image
347351
files.
348352

349353
## 0.1.0
350354

351-
* Roll the dependency on `markdown` to 1.0.0
352-
* Add a test and example for image links
353-
* Fix the `onTap` callback on hyperlinks
355+
* Roll the dependency on `markdown` to 1.0.0
356+
* Add a test and example for image links
357+
* Fix the `onTap` callback on hyperlinks
354358

355359
## 0.0.9
356360

357-
* First published version
361+
* First published version

packages/flutter_markdown/lib/src/builder.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,10 @@ class MarkdownBuilder implements md.NodeVisitor {
961961
text!,
962962
textScaler: styleSheet.textScaler,
963963
textAlign: textAlign ?? TextAlign.start,
964-
onSelectionChanged:
965-
(TextSelection selection, SelectionChangedCause? cause) =>
966-
onSelectionChanged!(text.text, selection, cause),
964+
onSelectionChanged: onSelectionChanged != null
965+
? (TextSelection selection, SelectionChangedCause? cause) =>
966+
onSelectionChanged!(text.text, selection, cause)
967+
: null,
967968
onTap: onTapText,
968969
key: k,
969970
);

packages/flutter_markdown/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
44
formatted with simple Markdown tags.
55
repository: https://github.com/flutter/packages/tree/main/packages/flutter_markdown
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
7-
version: 0.7.2
7+
version: 0.7.2+1
88

99
environment:
1010
sdk: ^3.3.0

packages/flutter_markdown/test/text_test.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,50 @@ void defineTests() {
284284
},
285285
);
286286

287+
testWidgets(
288+
'Selectable without onSelectionChanged',
289+
(WidgetTester tester) async {
290+
const String data = '# abc def ghi\njkl opq';
291+
292+
await tester.pumpWidget(
293+
const MaterialApp(
294+
home: Material(
295+
child: MarkdownBody(
296+
data: data,
297+
selectable: true,
298+
),
299+
),
300+
),
301+
);
302+
303+
// Find the positions before character 'd' and 'f'.
304+
final Offset dPos = positionInRenderedText(tester, 'abc def ghi', 4);
305+
final Offset fPos = positionInRenderedText(tester, 'abc def ghi', 6);
306+
// Select from 'd' until 'f'.
307+
final TestGesture firstGesture =
308+
await tester.startGesture(dPos, kind: PointerDeviceKind.mouse);
309+
addTearDown(firstGesture.removePointer);
310+
await tester.pump();
311+
await firstGesture.moveTo(fPos);
312+
await firstGesture.up();
313+
await tester.pump();
314+
315+
// Find the positions before character 'j' and 'o'.
316+
final Offset jPos = positionInRenderedText(tester, 'jkl opq', 0);
317+
final Offset oPos = positionInRenderedText(tester, 'jkl opq', 4);
318+
// Select from 'j' until 'o'.
319+
final TestGesture secondGesture =
320+
await tester.startGesture(jPos, kind: PointerDeviceKind.mouse);
321+
addTearDown(secondGesture.removePointer);
322+
await tester.pump();
323+
await secondGesture.moveTo(oPos);
324+
await secondGesture.up();
325+
await tester.pump();
326+
327+
expect(tester.takeException(), isNull);
328+
},
329+
);
330+
287331
testWidgets(
288332
'header with line of text and onSelectionChanged callback',
289333
(WidgetTester tester) async {

0 commit comments

Comments
 (0)