Skip to content

Commit 105024d

Browse files
committed
removed Container attribute
1 parent 75fb6f8 commit 105024d

File tree

3 files changed

+59
-121
lines changed

3 files changed

+59
-121
lines changed

packages/notus/example/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:notus/convert.dart';
56
import 'package:notus/notus.dart';
67

78
void main() {
@@ -21,6 +22,9 @@ void main() {
2122
print(change);
2223
});
2324

25+
final a = NotusHTMLCodec().encode(doc.toDelta());
26+
print(a);
27+
2428
// Dispose resources allocated by this document, e.g. closes "changes" stream.
2529
// After document is closed it cannot be modified.
2630
doc.close();

packages/notus/lib/src/convert/html.dart

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class deltaKeys {
4242
static const attributes = "attributes";
4343
static const heading = "heading";
4444
static const embed = "embed";
45-
static const container = "container";
4645
}
4746

4847
class htmlKeys {
@@ -99,6 +98,7 @@ class _NotusHTMLEncoder extends Converter<Delta, String> {
9998
NotusAttribute.ol: htmlKeys.orderedList,
10099
};
101100
Map<String, dynamic> container;
101+
102102
String buildContainer(String key) {
103103
if (container == null || !container.containsKey(key)) {
104104
return '';
@@ -122,13 +122,6 @@ class _NotusHTMLEncoder extends Converter<Delta, String> {
122122
return buffer.toString();
123123
}
124124

125-
Map<String, dynamic> getContainer(Map<String, dynamic> attr) {
126-
if (attr != null && attr.containsKey(deltaKeys.container)) {
127-
return attr[deltaKeys.container];
128-
}
129-
return null;
130-
}
131-
132125
@override
133126
String convert(Delta input) {
134127
final iterator = new DeltaIterator(input);
@@ -198,7 +191,7 @@ class _NotusHTMLEncoder extends Converter<Delta, String> {
198191
final op = iterator.next();
199192

200193
final lf = op.data.indexOf('\n');
201-
container = getContainer(op.attributes);
194+
// container = getContainer(op.attributes);
202195
if (lf == -1) {
203196
_handleSpan(removeZeroWidthSpace(op.data), op.attributes);
204197
} else {
@@ -477,30 +470,6 @@ class _HTMLNotusDecoder extends Converter<String, Delta> {
477470
if (elem.localName == htmlKeys.image) {
478471
attr.remove(htmlKeys.imageSrc);
479472
}
480-
final deltaKeyForContainer =
481-
htmlTagNameToDeltaAttributeName(elem.localName);
482-
if (deltaKeyForContainer != null && attr.isNotEmpty) {
483-
addContainerAttribute(attr, key, attrMap) {
484-
if (attrMap.containsKey(deltaKeys.container)) {
485-
attrMap[deltaKeys.container]
486-
.addAll(Map<String, dynamic>.from({key: attr}));
487-
} else {
488-
attrMap[deltaKeys.container] =
489-
Map<String, dynamic>.from({key: attr});
490-
}
491-
}
492-
493-
mapEmptyToNullForEmptyHtmlDataAttribute(attr) =>
494-
attr.map((key, val) => MapEntry<String, dynamic>(
495-
key, val is String && val.isEmpty ? null : val));
496-
497-
addContainerAttribute(
498-
mapEmptyToNullForEmptyHtmlDataAttribute(attr),
499-
deltaKeyForContainer,
500-
isInlineAttribute(elem.localName)
501-
? deltaAttributeInline
502-
: deltaAttributeLine);
503-
}
504473
}
505474
return Map<String, Map<String, dynamic>>.from({
506475
keys.inline: deltaAttributeInline,

packages/notus/test/convert/html_test.dart

Lines changed: 53 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,36 @@ void main() {
2626
expect(result, expected);
2727
});
2828

29-
test('decode italic with container attribute', () {
30-
final i = NotusAttribute.italic.toJson();
31-
final ic = Map<String, dynamic>.from(i);
32-
ic.addAll(NotusAttribute.container.withValue({
33-
"i": {"foo": "bar"}
34-
}).toJson());
35-
final delta = Delta()..insert('txt', ic)..insert('\n');
36-
final html = '<i foo="bar">txt</i>\n';
29+
test('decode italic attribute', () {
30+
final i = NotusAttribute.italic.key;
31+
final attributes = {i: true};
32+
final delta = Delta()..insert('txt', attributes)..insert('\n');
33+
final html = '<i>txt</i>\n';
3734
final result = notusHTML.decode(html);
3835
expect(result.toString(), delta.toString());
3936
});
40-
test('decode bold with container attribute', () {
41-
final b = NotusAttribute.bold.toJson();
42-
final bc = Map<String, dynamic>.from(b);
43-
bc.addAll(NotusAttribute.container.withValue({
44-
"b": {"foo": "bar"}
45-
}).toJson());
46-
final delta = Delta()..insert('txt', bc)..insert('\n');
47-
final html = '<b foo="bar">txt</b>\n';
48-
final result = notusHTML.decode(html);
49-
expect(result.toString(), delta.toString());
50-
});
51-
test('decode multiple container attribute', () {
52-
final b = NotusAttribute.bold.toJson();
53-
final bc = Map<String, dynamic>.from(b);
54-
bc.addAll(NotusAttribute.container.withValue({
55-
"b": {"foo": "bar", "hidden": null}
56-
}).toJson());
57-
final delta = Delta()..insert('txt', bc)..insert('\n');
58-
final html = '<b foo="bar" hidden>txt</b>\n';
37+
38+
test('decode bold attribute', () {
39+
final b = NotusAttribute.bold.key;
40+
final attributes = {b: true};
41+
final delta = Delta()..insert('txt', attributes)..insert('\n');
42+
final html = '<b>txt</b>\n';
5943
final result = notusHTML.decode(html);
6044
expect(result.toString(), delta.toString());
6145
});
6246

47+
// test('decode multiple container attribute', () {
48+
// final b = NotusAttribute.bold.toJson();
49+
// final bc = Map<String, dynamic>.from(b);
50+
// bc.addAll(NotusAttribute.container.withValue({
51+
// "b": {"foo": "bar", "hidden": null}
52+
// }).toJson());
53+
// final delta = Delta()..insert('txt', bc)..insert('\n');
54+
// final html = '<b foo="bar" hidden>txt</b>\n';
55+
// final result = notusHTML.decode(html);
56+
// expect(result.toString(), delta.toString());
57+
// });
58+
6359
test('empty text', () {
6460
final delta = Delta()..insert('\n');
6561
final html = "";
@@ -188,9 +184,6 @@ void main() {
188184
test('decode heading styles with container attribute', () {
189185
runFor(NotusAttribute<int> attribute, String source, String html) {
190186
final attr = attribute.toJson();
191-
attr.addAll(NotusAttribute.container.withValue({
192-
"heading": {"foo": "bar"}
193-
}).toJson());
194187
final delta = new Delta()..insert(source)..insert('\n', attr);
195188
final result = notusHTML.decode(html);
196189
expect(result.toString(), delta.toString());
@@ -203,25 +196,18 @@ void main() {
203196

204197
test('decode singe block with container: quote', () {
205198
runFor(NotusAttribute<String> attribute, String source, String html) {
206-
var attr = attribute.toJson();
207-
attr.addAll(NotusAttribute.container.withValue({
208-
"block": {"foo": "bar"}
209-
}).toJson());
210-
final delta = Delta()..insert(source)..insert('\n', attr);
199+
final delta = Delta()..insert(source)..insert('\n', attribute.toJson());
211200
final result = notusHTML.decode(html);
212201
expect(result.toString(), delta.toString());
213202
}
214203

215204
runFor(NotusAttribute.bq, 'item',
216205
'<blockquote foo="bar">\nitem\n</blockquote>\n');
217206
});
207+
218208
test('decode singe block with container: list', () {
219209
runFor(NotusAttribute<String> attribute, String source, String html) {
220-
var attr = NotusAttribute.container.withValue({
221-
"block": {"foo": "bar"}
222-
}).toJson();
223-
attr.addAll(attribute.toJson());
224-
final delta = Delta()..insert(source)..insert('\n', attr);
210+
final delta = Delta()..insert(source)..insert('\n', attribute.toJson());
225211
final result = notusHTML.decode(html);
226212
expect(result.toString(), delta.toString());
227213
}
@@ -344,9 +330,6 @@ void main() {
344330
"insert": String.fromCharCode(8203),
345331
"attributes": {
346332
"embed": {"type": "hr"},
347-
"container": {
348-
"embed": {"foo": "bar"}
349-
}
350333
},
351334
},
352335
])
@@ -366,40 +349,36 @@ void main() {
366349
expect(result, 'First line\nSecond line\n');
367350
});
368351

369-
test('encode italic with container attribute', () {
352+
test('encode italic attribute', () {
370353
final i = NotusAttribute.italic.toJson();
371354
final ic = Map<String, dynamic>.from(i);
372-
ic.addAll(NotusAttribute.container.withValue({
373-
"i": {"foo": "bar"}
374-
}).toJson());
375355
final delta = Delta()..insert('txt', ic)..insert('\n');
376-
final expected = '<i foo="bar">txt</i>\n';
377-
final result = notusHTML.encode(delta);
378-
expect(result.toString(), expected.toString());
379-
});
380-
test('encode bold with container attribute', () {
381-
final b = NotusAttribute.bold.toJson();
382-
final bc = Map<String, dynamic>.from(b);
383-
bc.addAll(NotusAttribute.container.withValue({
384-
"b": {"foo": "bar"}
385-
}).toJson());
386-
final delta = Delta()..insert('txt', bc)..insert('\n');
387-
final expected = '<b foo="bar">txt</b>\n';
356+
final expected = '<i>txt</i>\n';
388357
final result = notusHTML.encode(delta);
389358
expect(result.toString(), expected.toString());
390359
});
391-
test('encode bold with multiple container attribute', () {
360+
361+
test('encode bold attribute', () {
392362
final b = NotusAttribute.bold.toJson();
393363
final bc = Map<String, dynamic>.from(b);
394-
bc.addAll(NotusAttribute.container.withValue({
395-
"b": {"foo": "bar", "hidden": null}
396-
}).toJson());
397364
final delta = Delta()..insert('txt', bc)..insert('\n');
398-
final expected = '<b foo="bar" hidden>txt</b>\n';
365+
final expected = '<b>txt</b>\n';
399366
final result = notusHTML.encode(delta);
400367
expect(result.toString(), expected.toString());
401368
});
402369

370+
// test('encode bold with multiple container attribute', () {
371+
// final b = NotusAttribute.bold.toJson();
372+
// final bc = Map<String, dynamic>.from(b);
373+
// bc.addAll(NotusAttribute.container.withValue({
374+
// "b": {"foo": "bar", "hidden": null}
375+
// }).toJson());
376+
// final delta = Delta()..insert('txt', bc)..insert('\n');
377+
// final expected = '<b foo="bar" hidden>txt</b>\n';
378+
// final result = notusHTML.encode(delta);
379+
// expect(result.toString(), expected.toString());
380+
// });
381+
403382
test('encode bold italic', () {
404383
runFor(NotusAttribute<bool> attribute, String expected) {
405384
final delta = new Delta()
@@ -540,39 +519,30 @@ void main() {
540519
runFor(NotusAttribute.h2, 'Title', '<h2>Title</h2>\n');
541520
runFor(NotusAttribute.h3, 'Title', '<h3>Title</h3>\n');
542521
});
543-
test('encode heading styles with container attribute', () {
522+
test('encode heading styles', () {
544523
runFor(NotusAttribute<int> attribute, String source, String expected) {
545524
final attr = attribute.toJson();
546-
attr.addAll(NotusAttribute.container.withValue({
547-
"heading": {"foo": "bar"}
548-
}).toJson());
549525
final delta = new Delta()..insert(source)..insert('\n', attr);
550526
final result = notusHTML.encode(delta);
551527
expect(result, expected);
552528
}
553529

554-
runFor(NotusAttribute.h1, 'Title', '<h1 foo="bar">Title</h1>\n');
555-
runFor(NotusAttribute.h2, 'Title', '<h2 foo="bar">Title</h2>\n');
556-
runFor(NotusAttribute.h3, 'Title', '<h3 foo="bar">Title</h3>\n');
530+
runFor(NotusAttribute.h1, 'Title', '<h1>Title</h1>\n');
531+
runFor(NotusAttribute.h2, 'Title', '<h2>Title</h2>\n');
532+
runFor(NotusAttribute.h3, 'Title', '<h3>Title</h3>\n');
557533
});
558534

559-
test('encode singe block with container', () {
535+
test('encode singe block', () {
560536
runFor(NotusAttribute<String> attribute, String source, String expected) {
561537
var attr = attribute.toJson();
562-
attr.addAll(NotusAttribute.container.withValue({
563-
"block": {"foo": "bar"}
564-
}).toJson());
565538
final delta = Delta()..insert(source)..insert('\n', attr);
566539
final result = notusHTML.encode(delta);
567540
expect(result, expected);
568541
}
569542

570-
runFor(
571-
NotusAttribute.ul, 'item', '<ul foo="bar">\n<li>item</li>\n</ul>\n');
572-
runFor(
573-
NotusAttribute.ol, 'item', '<ol foo="bar">\n<li>item</li>\n</ol>\n');
574-
runFor(NotusAttribute.bq, 'item',
575-
'<blockquote foo="bar">item</blockquote>\n');
543+
runFor(NotusAttribute.ul, 'item', '<ul>\n<li>item</li>\n</ul>\n');
544+
runFor(NotusAttribute.ol, 'item', '<ol>\n<li>item</li>\n</ol>\n');
545+
runFor(NotusAttribute.bq, 'item', '<blockquote>item</blockquote>\n');
576546
});
577547

578548
test('encode block styles: ol, ul', () {
@@ -641,16 +611,13 @@ void main() {
641611

642612
runFor('<hr />\n');
643613
});
644-
test('encode hr with container attribute', () {
614+
test('encode hr attribute', () {
645615
runFor(String expected) {
646616
final delta = Delta.fromJson([
647617
{
648618
"insert": "",
649619
"attributes": {
650620
"embed": {"type": "hr"},
651-
"container": {
652-
"embed": {"foo": "bar"}
653-
}
654621
},
655622
},
656623
])
@@ -659,7 +626,7 @@ void main() {
659626
expect(result, expected);
660627
}
661628

662-
runFor('<hr foo="bar" />\n');
629+
runFor('<hr />\n');
663630
});
664631
test('encode multiline blocks', () {
665632
runFor(NotusAttribute<String> attribute, String source, String expected) {
@@ -678,8 +645,6 @@ void main() {
678645
'<ol>\n<li>text</li>\n<li>text</li>\n</ol>\n');
679646
runFor(
680647
NotusAttribute.bq, 'text', '<blockquote>text\ntext</blockquote>\n');
681-
682-
// runFor(NotusAttribute.code, 'text', '```\ntext\ntext\n```\n\n');
683648
});
684649

685650
test('encode multiple styles', () {

0 commit comments

Comments
 (0)