Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HTML encoder for tangled inline tags #174

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading