| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| import 'package:markdown/markdown.dart'; | |
| import 'package:test/test.dart'; | |
| void main() { | |
| test('HTML comment with dashes #2119', () { | |
| // See https://dartbug.com/tools/2119. | |
| // For this issue, the leading letter was needed, | |
| // an HTML comment starting a line is handled by a different path. | |
| // The empty line before the `-->` is needed. | |
| // The number of lines increase time exponentially. | |
| // The length of lines affect the base of the exponentiation. | |
| // Locally, three "Lorem-ipsum" lines ran in ~6 seconds, two in < 200 ms. | |
| // Adding a fourth line should ensure it cannot possibly finish in ten | |
| // seconds if the bug isn't fixed. | |
| const input = ''' | |
| a <!-- | |
| - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. | |
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi | |
| ut aliquip ex ea commodo consequat. | |
| - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. | |
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi | |
| ut aliquip ex ea commodo consequat. | |
| - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. | |
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi | |
| ut aliquip ex ea commodo consequat. | |
| - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod | |
| tempor incididunt ut labore et dolore magna aliqua. | |
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi | |
| ut aliquip ex ea commodo consequat. | |
| --> | |
| '''; | |
| final time = Stopwatch()..start(); | |
| final html = markdownToHtml(input); // Should not hang. | |
| expect(html, isNotNull); // To use the output. | |
| final elapsed = time.elapsedMilliseconds; | |
| expect(elapsed, lessThan(10000)); | |
| }); | |
| test('HTML comment with lt/gt', () { | |
| // Incorrect parsing found as part of fixing #2119. | |
| // Now matches `<!--$text-->` where text | |
| // does not start with `>` or `->`, does not end with `-`, | |
| // and does not contain `--`. | |
| const input = 'a <!--<->>-<!-->'; | |
| final html = markdownToHtml(input); | |
| expect(html, '<p>$input</p>\n'); | |
| }); | |
| test('setext heading underline is not an empty list item, GFM #2108', () { | |
| // See https://github.com/dart-lang/tools/issues/2108. | |
| // With `ExtensionSet.gitHubFlavored`, `UnorderedListWithCheckboxSyntax` | |
| // used to be checked before `SetextHeaderSyntax`, so a lone `-` line | |
| // continuing a paragraph was parsed as an empty list item instead of a | |
| // setext H2 underline. | |
| final html = markdownToHtml( | |
| 'I am paragraph\n-', | |
| extensionSet: ExtensionSet.gitHubFlavored, | |
| ); | |
| expect(html, '<h2>I am paragraph</h2>\n'); | |
| }); | |
| test('long unbroken word does not take quadratic time', () { | |
| // The plain-text accelerator regex required a trailing whitespace, so a | |
| // long run of word characters that reached the end of a line without one | |
| // (a single long word) forced the regex to backtrack across the whole run | |
| // at every position, giving quadratic parse time. A ~200k character word | |
| // used to take tens of seconds; it should now be effectively instant. | |
| final input = '${'a' * 200000}\n'; | |
| final time = Stopwatch()..start(); | |
| final html = markdownToHtml(input); // Should not hang. | |
| expect(html, isNotNull); // To use the output. | |
| expect(time.elapsedMilliseconds, lessThan(10000)); | |
| }); | |
| } |