Skip to content

Commit

Permalink
fix: all the text pasted from google doc will be applied inline link …
Browse files Browse the repository at this point in the history
…format (#993)
  • Loading branch information
asjqkkkk authored Dec 17, 2024
1 parent 4f1473b commit c6909a7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/src/plugins/html/html_document_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ class DocumentHTMLDecoder extends Converter<String, Document> {
if (domNode is dom.Element) {
final localName = domNode.localName;
if (HTMLTags.formattingElements.contains(localName)) {
final attributes = _parserFormattingElementAttributes(domNode);
delta.insert(domNode.text, attributes: attributes);
final style = domNode.attributes['style'];

///This is used for temporarily handling documents copied from Google Docs,
/// see [#6808](https://github.com/AppFlowy-IO/AppFlowy/issues/6808).
final isMeaninglessTag =
style == 'font-weight:normal;' && localName == HTMLTags.bold;
if (isMeaninglessTag && domNode.children.isNotEmpty) {
nodes.addAll(_parseElement(domNode.children));
} else {
final attributes = _parserFormattingElementAttributes(domNode);
delta.insert(domNode.text, attributes: attributes);
}
} else if (HTMLTags.specialElements.contains(localName)) {
if (delta.isNotEmpty) {
nodes.add(paragraphNode(delta: delta));
Expand Down
46 changes: 46 additions & 0 deletions test/plugins/html/html_document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,50 @@ void main() {
expect(node.attributes[HeadingBlockKeys.level], i);
}
});

test('sample 12', () {
// Checkout conversion is too low: only 18% of users who click on “Book” checkout.
// Comparable funnels for hotel booking sites achieve ~30% (source).
const html =
'''<meta charset='utf-8'><meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-30b67c3b-7fff-e5ec-0b19-376448d34980"><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial,sans-serif;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Checkout conversion is too low: only 18% of users who click on &ldquo;Book&rdquo; checkout.</span></p><p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial,sans-serif;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Comparable funnels for hotel booking sites achieve ~30% (</span><a href="https://docs.google.com/document/d/147-eAtY305IGAghvmQzn0NXUFlh2zQs50GbmBNlgnaA/edit?tab=t.0" style="text-decoration:none;"><span style="font-size:11pt;font-family:Arial,sans-serif;color:#1155cc;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:underline;-webkit-text-decoration-skip:none;text-decoration-skip-ink:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">source</span></a><span style="font-size:11pt;font-family:Arial,sans-serif;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">).</span></p></b><br class="Apple-interchange-newline">''';
final document = htmlToDocument(html);

final node = document.nodeAtPath([0])!;
expect(node.type, ParagraphBlockKeys.type);
expect(
node.delta!.toPlainText(),
'Checkout conversion is too low: only 18% of users who click on “Book” checkout.',
);

final node2 = document.nodeAtPath([1])!;
expect(node2.type, ParagraphBlockKeys.type);
final jsonList = node2.delta!.toJson();
final cleanJson = jsonList.map((e) {
final map = e as Map;
map.removeWhere((k, v) => k != 'insert' && k != 'attributes');
return map;
}).toList();
expect(
cleanJson,
[
{
'insert': 'Comparable funnels for hotel booking sites achieve ~30% (',
'attributes': {'font_color': '0xff000000'},
},
{
'insert': 'source',
'attributes': {
'href':
'https://docs.google.com/document/d/147-eAtY305IGAghvmQzn0NXUFlh2zQs50GbmBNlgnaA/edit?tab=t.0',
'underline': true,
'font_color': '0xff1155cc',
},
},
{
'insert': ').',
'attributes': {'font_color': '0xff000000'},
}
],
);
});
}

0 comments on commit c6909a7

Please sign in to comment.