fix: common link is detected as a video link

pull/1978/head
CatHood0 10 months ago
parent f766bf055e
commit 150989d3dd
  1. 8
      lib/src/utils/delta_x_utils.dart
  2. 31
      test/utils/delta_x_test.dart

@ -20,7 +20,7 @@ class UnderlineSyntax extends md.DelimiterSyntax {
class VideoSyntax extends md.LinkSyntax { class VideoSyntax extends md.LinkSyntax {
VideoSyntax({super.linkResolver}) VideoSyntax({super.linkResolver})
: super( : super(
pattern: r'\[', pattern: r'\[!\[',
startCharacter: _$lbracket, startCharacter: _$lbracket,
); );
@ -60,13 +60,13 @@ final videoRule = hmd.Rule('video', filters: ['iframe', 'video'],
if (!_youtubeVideoUrlValidator.hasMatch(src ?? '')) { if (!_youtubeVideoUrlValidator.hasMatch(src ?? '')) {
return '<video>${child.outerHTML}</video>'; return '<video>${child.outerHTML}</video>';
} }
return '[$content]($src)'; return '[![$content]($src)';
} }
final src = node.getAttribute('src'); final src = node.getAttribute('src');
if (src == null || !_youtubeVideoUrlValidator.hasMatch(src)) { if (src == null || !_youtubeVideoUrlValidator.hasMatch(src)) {
return node.outerHTML; return node.outerHTML;
} }
return '[$content]($src)'; return '[![$content]($src)';
} }
//by now, we can only access to src //by now, we can only access to src
final src = node.getAttribute('src'); final src = node.getAttribute('src');
@ -76,5 +76,5 @@ final videoRule = hmd.Rule('video', filters: ['iframe', 'video'],
return node.outerHTML; return node.outerHTML;
} }
final title = node.getAttribute('title'); final title = node.getAttribute('title');
return '[$title]($src)'; return '[![$title]($src)';
}); });

@ -3,28 +3,27 @@ import 'package:flutter_quill/src/models/documents/delta_x.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
void main() { void main() {
const htmlWithEmp = const htmlWithEmp = '<p>This is a normal sentence, and this section has greater emp<em>hasis.</em></p>';
'<p>This is a normal sentence, and this section has greater emp<em>hasis.</em></p>';
const htmlWithUnderline = const htmlWithUnderline = '<p>This is a normal sentence, and this section has greater <u>underline</u>';
'<p>This is a normal sentence, and this section has greater <u>underline</u>';
const htmlWithIframeVideo = const htmlWithIframeVideo =
'<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player"></iframe>'; '<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" title="YouTube video player"></iframe>';
const htmlWithVideoTag = const htmlWithVideoTag =
'''<video src="https://www.youtube.com/embed/dQw4w9WgXcQ">Your browser does not support the video tag.</video> '<video src="https://www.youtube.com/embed/dQw4w9WgXcQ">Your browser does not support the video tag.</video>';
''';
const htmlWithNormalLinkAndVideo =
'<a href="https://www.macrumors.com/" type="text/html">fdsfsd</a><br><video src="https://www.youtube.com/embed/dQw4w9WgXcQ">Your browser does not support the video tag.</video>';
final expectedDeltaEmp = Delta.fromOperations([ final expectedDeltaEmp = Delta.fromOperations([
Operation.insert( Operation.insert('This is a normal sentence, and this section has greater emp'),
'This is a normal sentence, and this section has greater emp'),
Operation.insert('hasis.', {'italic': true}), Operation.insert('hasis.', {'italic': true}),
Operation.insert('\n'), Operation.insert('\n'),
]); ]);
final expectedDeltaUnderline = Delta.fromOperations([ final expectedDeltaUnderline = Delta.fromOperations([
Operation.insert( Operation.insert('This is a normal sentence, and this section has greater '),
'This is a normal sentence, and this section has greater '),
Operation.insert('underline', {'underline': true}), Operation.insert('underline', {'underline': true}),
Operation.insert('\n'), Operation.insert('\n'),
]); ]);
@ -34,6 +33,13 @@ void main() {
Operation.insert('\n'), Operation.insert('\n'),
]); ]);
final expectedDeltaLinkAndVideoLink = Delta.fromOperations([
Operation.insert('fdsfsd', {'link': 'https://www.macrumors.com/'}),
Operation.insert('\n\n'),
Operation.insert({'video': 'https://www.youtube.com/embed/dQw4w9WgXcQ'}),
Operation.insert('\n'),
]);
test('should detect emphasis and parse correctly', () { test('should detect emphasis and parse correctly', () {
final delta = DeltaX.fromHtml(htmlWithEmp); final delta = DeltaX.fromHtml(htmlWithEmp);
expect(delta, expectedDeltaEmp); expect(delta, expectedDeltaEmp);
@ -53,4 +59,9 @@ void main() {
final delta = DeltaX.fromHtml(htmlWithVideoTag); final delta = DeltaX.fromHtml(htmlWithVideoTag);
expect(delta, expectedDeltaVideo); expect(delta, expectedDeltaVideo);
}); });
test('should detect by different way normal link and video link', () {
final delta = DeltaX.fromHtml(htmlWithNormalLinkAndVideo);
expect(delta, expectedDeltaLinkAndVideoLink);
});
} }

Loading…
Cancel
Save