Skip to content

Commit 18b1763

Browse files
authored
Allow <a name=""></a> elements in the markdown. (#8796)
1 parent 1203886 commit 18b1763

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

app/lib/shared/markdown.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ class _RelativeUrlRewriter extends html_parsing.TreeVisitor {
220220
super.visitElement(element);
221221

222222
// check current element
223-
if (element.localName == 'a') {
223+
if (element.localName == 'a' &&
224+
element.attributes.length == 1 &&
225+
element.attributes.containsKey('name') &&
226+
element.attributes['name']!.isNotEmpty &&
227+
element.nodes.isEmpty) {
228+
// allow name anchor without any other attribute or content
229+
} else if (element.localName == 'a') {
224230
_updateUrlAttributes(element, 'href');
225231
} else if (element.localName == 'img') {
226232
_updateUrlAttributes(element, 'src', raw: true);

app/test/shared/markdown_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ void main() {
2727
'</ul>\n',
2828
);
2929
});
30+
31+
test('named anchor', () {
32+
expect(
33+
markdownToHtml('<a name="abc"></a>'), '<p><a name="abc"></a></p>\n');
34+
});
35+
36+
test('named anchor with other content', () {
37+
expect(markdownToHtml('<a name="abc" other="1"></a>'), '<p></p>\n');
38+
expect(markdownToHtml('<a name="abc">x</a>'), '<p>x</p>\n');
39+
});
3040
});
3141

3242
group('Valid custom base URL', () {

0 commit comments

Comments
 (0)