Skip to content

Commit

Permalink
Fix HTML encoder for tangled inline tags (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux authored Nov 22, 2023
1 parent 74447c4 commit 56ee00f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/parchment/lib/src/codecs/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,22 @@ class _ParchmentHtmlEncoder extends Converter<ParchmentDocument, String> {
// Closing tags effectively adds the opening tag at the appropriate position
// AND adds the closing tag
final attributesToRemove = <_HtmlInlineTag>{};
for (final attr in openInlineTags) {
for (var i = 0; i < openInlineTags.length; i++) {
final attr = openInlineTags[i];
if (!inlineAttributes.contains(attr.attribute)) {
// remove any tag that was opened later as they must be closed
for (var j = 0; j <= i; j++) {
final prevAttr = openInlineTags[j];
if (prevAttr.openingPosition > attr.openingPosition) {
_writeTag(buffer, prevAttr);
attributesToRemove.add(prevAttr);
}
}
_writeTag(buffer, attr);
attributesToRemove.add(attr);
}
}

for (final attr in attributesToRemove) {
openInlineTags.remove(attr);
}
Expand Down
42 changes: 42 additions & 0 deletions packages/parchment/test/codecs/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ void main() {
'<u><a href="https://wikipedia.org">Something </a><em>in the way</em> mmmm...</u>');
});

test('tangled inline tags', () {
final doc = ParchmentDocument.fromJson([
{'insert': 'AAA'},
{
'insert': 'BB',
'attributes': {'b': true}
},
{
'insert': 'B',
'attributes': {'b': true, 's': true}
},
{
'insert': 'CCC',
'attributes': {'s': true}
},
{'insert': '\n'}
]);
expect(codec.encode(doc),
'AAA<strong>BB<del>B</del></strong><del>CCC</del>');
});

test('html escaping', () {
final doc = ParchmentDocument.fromJson([
{
Expand Down Expand Up @@ -1204,6 +1225,27 @@ void main() {
expect(codec.decode(html).toDelta(), doc.toDelta());
});

test('tangled inline tags', () {
final html = 'AAA<strong>BB<del>B</del></strong><del>CCC</del>';
final doc = ParchmentDocument.fromJson([
{'insert': 'AAA'},
{
'insert': 'BB',
'attributes': {'b': true}
},
{
'insert': 'B',
'attributes': {'b': true, 's': true}
},
{
'insert': 'CCC',
'attributes': {'s': true}
},
{'insert': '\n'}
]);
expect(codec.decode(html).toDelta(), doc.toDelta());
});

test('embedded inline attributes text', () {
final html =
'<p><u><a href="https://wikipedia.org">Something </a><em>in the way</em> mmmm...</u></p>';
Expand Down

0 comments on commit 56ee00f

Please sign in to comment.