Skip to content

Commit

Permalink
HTML codec: Support indentation decoding
Browse files Browse the repository at this point in the history
Only one level supported
  • Loading branch information
amantoux committed May 9, 2024
1 parent eb7b3ca commit e8e1dfa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/parchment/lib/src/codecs/html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const _indentWidthInPx = 32;
/// `<div class"checklist-item><input type="checklist" checked><label>`
/// - alignment -> `<xxx align="left | right | center | justify">`
/// - direction -> `<xxx dir="rtl">`
/// - indentation -> `<xxx style=padding-left:16px>`
/// - only one level is supported when decoding
///
/// ## Embed mapping
/// - [BlockEmbed.image] -> `<img src="...">`
Expand All @@ -68,6 +70,7 @@ class ParchmentHtmlCodec extends Codec<ParchmentDocument, String> {
// Mutable record for the state of the encoder
class _EncoderState {
StringBuffer buffer = StringBuffer();

// Stack on inline tags
final List<_HtmlInlineTag> openInlineTags = [];

Expand Down Expand Up @@ -1005,14 +1008,22 @@ class _ParchmentHtmlDecoder extends Converter<String, ParchmentDocument> {
switch (sValue) {
case 'right':
updated = updated.put(ParchmentAttribute.right);
break;
continue;
case 'center':
updated = updated.put(ParchmentAttribute.center);
break;
continue;
case 'justify':
updated = updated.put(ParchmentAttribute.justify);
}
break;
continue;
}
if (style.startsWith('padding-left')) {
final sValue = style.split(':')[1];
if (sValue == '0' || sValue == '0px' || sValue == 'Oem') {
continue;
}
updated = updated.put(ParchmentAttribute.indent.withLevel(1));
continue;
}
}
return updated;
Expand Down
10 changes: 10 additions & 0 deletions packages/parchment/test/codecs/html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,16 @@ void main() {
expect(codec.decode(html).toDelta(), doc.toDelta());
});

test('with indentation', () {
final html = '<p style=padding-left:32px>Hello World!</p>';
final doc = ParchmentHtmlCodec().decode(html);
expect(
doc.toDelta(),
Delta()
..insert('Hello World!')
..insert('\n', {'indent': 1}));
});

test('Paragraph with link', () {
final html =
'<p>Hello World!<a href="http://fake.link">Hello World!</a> Another hello world!</p>';
Expand Down

0 comments on commit e8e1dfa

Please sign in to comment.