-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_parser.py
1229 lines (951 loc) · 41.5 KB
/
test_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
import sys
from unittest import TestCase, mock
import bs4
from bs4 import BeautifulSoup
from mf2py import Parser
TestCase.maxDiff = None
TEST_DIR = "test/examples/"
def parse_fixture(path, **kwargs):
with open(os.path.join(TEST_DIR, path)) as f:
p = Parser(doc=f, html_parser="html5lib", **kwargs)
return p.to_dict()
def test_empty():
p = Parser()
assert type(p) is not None
assert type(p.to_dict()) is dict
def test_open_file():
with open(os.path.join(TEST_DIR, "empty.html")) as f:
p = Parser(doc=f)
assert p.__doc__ is not None
assert type(p) is not None
assert type(p.to_dict()) is dict
def test_doc_tag():
# test that strings, BS doc and BS tags are all parsed
doc = """<article class="h-entry"></article>"""
soup = BeautifulSoup(doc, "html5lib")
parse_string = Parser(doc).to_dict()
assert "h-entry" in parse_string["items"][0]["type"]
parse_doc = Parser(soup).to_dict()
assert "h-entry" in parse_doc["items"][0]["type"]
parse_tag = Parser(soup.article).to_dict()
assert "h-entry" in parse_tag["items"][0]["type"]
@mock.patch("requests.get")
def test_user_agent(getter):
ua_expect = "mf2py - microformats2 parser for python"
assert Parser.useragent.startswith(ua_expect)
resp = mock.MagicMock()
resp.content = b""
resp.text = ""
resp.headers = {}
getter.return_value = resp
Parser(url="http://example.com")
getter.assert_called_with(
"http://example.com", headers={"User-Agent": Parser.useragent}
)
Parser.useragent = "something else"
assert Parser.useragent == "something else"
# set back to default. damn stateful classes
Parser.useragent = "mf2py - microformats2 parser for python"
def test_base():
with open(os.path.join(TEST_DIR, "base.html")) as f:
p = Parser(doc=f)
assert p.__url__ == "http://tantek.com/"
def test_simple_parse():
result = parse_fixture("simple_person_reference.html")
assert result["items"][0]["properties"] == {"name": ["Frances Berriman"]}
def test_simple_person_reference_same_element():
result = parse_fixture("simple_person_reference_same_element.html")
assert result["items"][0]["properties"] == {"name": ["Frances Berriman"]}
def test_person_with_url():
result = parse_fixture("person_with_url.html")
assert result["items"][0]["properties"]["name"] == ["Tom Morris"]
assert result["items"][0]["properties"]["url"] == ["http://tommorris.org/"]
def test_vcp():
result = parse_fixture("value_class_person.html")
assert result["items"][0]["properties"]["tel"] == ["+44 1234 567890"]
def test_multiple_root_classnames():
result = parse_fixture("nested_multiple_classnames.html")
# order does not matter
assert len(result["items"]) == 1
assert set(result["items"][0]["type"]) == set(["h-entry", "h-as-note"])
def test_property_nested_microformat():
result = parse_fixture("nested_multiple_classnames.html")
assert len(result["items"]) == 1
assert "author" in result["items"][0]["properties"]
assert (
result["items"][0]["properties"]["author"][0]["properties"]["name"][0]
== "Tom Morris"
)
assert (
result["items"][0]["properties"]["reviewer"][0]["properties"]["name"][0]
== "Tom Morris"
)
assert (
result["items"][0]["properties"]["author"][0]["properties"]["adr"][0][
"properties"
]["city"][0]
== "London"
)
def test_plain_child_microformat():
result = parse_fixture("nested_multiple_classnames.html")
assert len(result["items"]) == 1
assert "children" in result["items"][0]
assert len(result["items"][0]["children"]) == 1
assert result["items"][0]["children"][0]["properties"]["name"][0] == "Some Citation"
def test_datetime_parsing():
result = parse_fixture("datetimes.html")
assert result["items"][0]["properties"]["start"][0] == "2014-01-01T12:00:00+0000"
assert result["items"][0]["properties"]["end"][0] == "3014-01-01T18:00:00+0000"
assert result["items"][0]["properties"]["duration"][0] == "P1000Y"
assert result["items"][0]["properties"]["updated"][0] == "2011-08-26T00:01:21+0000"
assert result["items"][0]["properties"]["updated"][1] == "2011-08-26T00:01:21+0000"
def test_datetime_vcp_parsing():
result = parse_fixture("datetimes.html")
assert len(result["items"]) == 16
assert result["items"][1]["properties"]["published"][0] == "3014-01-01 01:21Z"
assert result["items"][2]["properties"]["updated"][0] == "2014-03-11 09:55"
assert result["items"][3]["properties"]["published"][0] == "2014-01-30 15:28"
assert result["items"][4]["properties"]["published"][0] == "9999-01-14T11:52+0800"
assert result["items"][5]["properties"]["published"][0] == "2014-06-01 12:30-0600"
assert result["items"][8]["properties"]["start"][0] == "2014-06-01 12:30-0600"
assert result["items"][9]["properties"]["start"][0] == "2014-06-01 12:30-0600"
assert result["items"][10]["properties"]["start"][0] == "2014-06-01 00:30-0600"
assert result["items"][10]["properties"]["end"][0] == "2014-06-01 12:15"
assert result["items"][10]["properties"]["start"][1] == "2014-06-01 00:30-0600"
assert result["items"][10]["properties"]["end"][1] == "2014-06-01 12:15"
assert result["items"][11]["properties"]["start"][0] == "2016-03-02 00:30-0600"
assert result["items"][12]["properties"]["start"][0] == "2014-06-01 12:30-600"
assert result["items"][13]["properties"]["start"][0] == "2014-06-01 12:30+600"
assert result["items"][14]["properties"]["start"][0] == "2014-06-01 12:30Z"
assert result["items"][15]["properties"]["start"][0] == "2014-06-01 12:30-600"
def test_dt_end_implied_date():
"""Test that events with dt-start and dt-end use the implied date rule
http://microformats.org/wiki/value-class-pattern#microformats2_parsers
for times without dates"""
result = parse_fixture("datetimes.html")
event_wo_tz = result["items"][6]
assert event_wo_tz["properties"]["start"][0] == "2014-05-21 18:30"
assert event_wo_tz["properties"]["end"][0] == "2014-05-21 19:30"
event_w_tz = result["items"][7]
assert event_w_tz["properties"]["start"][0] == "2014-06-01 12:30-0600"
assert event_w_tz["properties"]["end"][0] == "2014-06-01 19:30-0600"
def test_embedded_parsing():
result = parse_fixture("embedded.html")
assert (
result["items"][0]["properties"]["content"][0]["html"]
== "<p>Blah blah blah blah blah.</p>\n <p>Blah.</p>\n <p>Blah blah blah.</p>"
)
assert (
result["items"][0]["properties"]["content"][0]["value"]
== "Blah blah blah blah blah.\n\nBlah.\n\nBlah blah blah."
)
def test_embedded_exposed_dom():
result = parse_fixture("embedded.html", expose_dom=True)
content = result["items"][0]["properties"]["content"][0]
assert "html" not in content
assert isinstance(content["dom"], bs4.element.Tag)
def test_hoisting_nested_hcard():
result = parse_fixture("nested_hcards.html")
expected = [
{
"properties": {
"author": [
{
"properties": {"name": ["KP1"]},
"type": ["h-card"],
"value": "KP1",
}
],
"in-reply-to": [
{"properties": {"name": ["KP"]}, "type": ["h-cite"], "value": "KP"}
],
},
"type": ["h-entry"],
}
]
assert expected == result["items"]
def test_html_tag_class():
result = parse_fixture("hfeed_on_html_tag.html")
assert ["h-feed"] == result["items"][0]["type"]
assert ["entry1"] == result["items"][0]["children"][0]["properties"]["name"]
assert ["entry2"] == result["items"][0]["children"][1]["properties"]["name"]
def test_string_strip():
result = parse_fixture("string_stripping.html")
assert "Tom Morris" == result["items"][0]["properties"]["name"][0]
def test_template_parse():
result = parse_fixture("template_tag.html")
assert 0 == len(result["items"])
def test_template_tag_inside_e_value():
result = parse_fixture("template_tag_inside_e_value.html")
assert (
"This is a Test with a <code>template</code> tag after this:"
== result["items"][0]["properties"]["content"][0]["html"]
)
assert (
"This is a Test with a template tag after this:"
== result["items"][0]["properties"]["content"][0]["value"]
)
def test_ordering_dedup():
"""test that classes are dedeuped and alphabetically ordered"""
result = parse_fixture("ordering_dedup.html")
item = result["items"][0]
assert ["h-entry", "h-feed", "h-product", "h-x-test"] == item["type"]
assert ["example.com", "example.com/2"] == item["properties"]["url"]
assert ["name", "URL name"] == item["properties"]["name"]
assert ["author", "bookmark", "me"] == result["rel-urls"]["example.com/rel"]["rels"]
assert "de" == result["rel-urls"]["example.com/lang"]["hreflang"]
def test_class_names_format():
"""test that only classes with letters and possibly numbers in the vendor prefix part are used"""
result = parse_fixture("class_names_format.html")
item = result["items"][0]
assert ["h-feed", "h-p3k-entry", "h-x-test"] == item["type"]
assert "url" in item["properties"]
assert "p3k-url" in item["properties"]
assert "Url" not in item["properties"]
assert "-url" not in item["properties"]
assert "url-" not in item["properties"]
assert "name" in item["properties"]
assert "p3k-name" in item["properties"]
assert "nAme" not in item["properties"]
assert "-name" not in item["properties"]
assert "name-" not in item["properties"]
def test_area_uparsing():
result = parse_fixture("area.html")
assert {"url": ["http://suda.co.uk"], "name": ["Brian Suda"]} == result["items"][0][
"properties"
]
assert "shape" in result["items"][0]
assert "coords" in result["items"][0]
def test_src_equiv():
result = parse_fixture("test_src_equiv.html")
for item in result["items"]:
assert "x-example" in item["properties"]
assert "http://example.org/" == item["properties"]["x-example"][0]
def test_rels():
result = parse_fixture("rel.html")
assert {
"in-reply-to": ["http://example.com/1", "http://example.com/2"],
"author": ["http://example.com/a", "http://example.com/b"],
"alternate": ["http://example.com/fr"],
"home": ["http://example.com/fr"],
} == result["rels"]
assert {
"http://example.com/1": {"text": "post 1", "rels": ["in-reply-to"]},
"http://example.com/2": {"text": "post 2", "rels": ["in-reply-to"]},
"http://example.com/a": {"text": "author a", "rels": ["author"]},
"http://example.com/b": {"text": "author b", "rels": ["author"]},
"http://example.com/fr": {
"text": "French mobile homepage",
"media": "handheld",
"rels": ["alternate", "home"],
"hreflang": "fr",
},
} == result["rel-urls"]
def test_alternates():
result = parse_fixture("rel.html")
assert [
{
"url": "http://example.com/fr",
"media": "handheld",
"text": "French mobile homepage",
"rel": "home",
"hreflang": "fr",
}
] == result["alternates"]
def test_enclosures():
result = parse_fixture("rel_enclosure.html")
assert {"enclosure": ["http://example.com/movie.mp4"]} == result["rels"]
assert {
"http://example.com/movie.mp4": {
"rels": ["enclosure"],
"text": "my movie",
"type": "video/mpeg",
}
} == result["rel-urls"]
def test_empty_href():
result = parse_fixture("hcard_with_empty_url.html", url="http://foo.com")
for hcard in result["items"]:
assert ["http://foo.com"] == hcard["properties"]["url"]
def test_link_with_u_url():
result = parse_fixture("link_with_u-url.html", url="http://foo.com")
assert {
"type": ["h-card"],
"properties": {
"name": [""],
"url": ["http://foo.com/"],
},
} == result["items"][0]
def test_broken_url():
result = parse_fixture("broken_url.html", url="http://example.com")
assert (
result["items"][0]["properties"]["relative"][0] == "http://example.com/foo.html"
)
assert result["items"][0]["properties"]["url"][0] == "http://www.[w3.org/"
assert (
result["items"][0]["properties"]["photo"][0]
== "http://www.w3].org/20[08/site/images/logo-w3c-mobile-lg"
)
def test_complex_e_content():
"""When parsing h-* e-* properties, we should fold {"value":..., "html":...}
into the parsed microformat object, instead of nesting it under an
unnecessary second layer of "value":
"""
result = parse_fixture("complex_e_content.html")
assert {
"type": ["h-entry"],
"properties": {
"content": [
{
"type": ["h-card"],
"properties": {"name": ["Hello"]},
"html": "<p>Hello</p>",
"value": "Hello",
}
],
},
} == result["items"][0]
def test_relative_url_in_e():
"""When parsing e-* properties, make relative URLs absolute."""
result = parse_fixture("relative_url_in_e.html")
assert (
'<p><a href="http://example.com/cat.html">Cat '
'<img src="http://example.com/cat.jpg"/></a></p>'
) == result["items"][0]["properties"]["content"][0]["html"]
def test_nested_values():
"""When parsing nested microformats, check that value is the value of
the simple property element"""
result = parse_fixture("nested_values.html")
entry = result["items"][0]
assert {
"properties": {
"name": ["Kyle"],
"url": ["http://about.me/kyle"],
},
"value": "Kyle",
"type": ["h-card"],
} == entry["properties"]["author"][0]
assert {
"properties": {
"name": ["foobar"],
"url": ["http://example.com/foobar"],
},
"value": "http://example.com/foobar",
"type": ["h-cite"],
} == entry["properties"]["like-of"][0]
assert {
"properties": {
"name": ["George"],
"url": ["http://people.com/george"],
},
"type": ["h-card"],
} == entry["children"][0]
# implied properties tests
def test_implied_name():
result = parse_fixture("implied_properties/implied_properties.html")
for i in range(7):
assert result["items"][i]["properties"]["name"][0] == "Tom Morris"
def test_implied_url():
result = parse_fixture(
"implied_properties/implied_properties.html", url="http://foo.com/"
)
assert result["items"][1]["properties"]["url"][0] == "http://tommorris.org/"
# img should not have a "url" property
assert "url" not in result["items"][4]["properties"]
# href="" is relative to the base url
assert result["items"][5]["properties"]["url"][0] == "http://foo.com/"
def test_implied_photo():
result = parse_fixture("implied_properties/implied_photo.html")
for i in range(12):
photos = result["items"][i]["properties"]["photo"]
assert len(photos) == 1
assert photos[0] == "http://example.com/photo.jpg"
# tests for no photo
for i in range(12, 23):
assert "photo" not in result["items"][i]["properties"]
result = parse_fixture("implied_properties/implied_photo_relative_url.html")
assert (
result["items"][0]["properties"]["photo"][0]["value"]
== "http://example.com/jane-img.jpeg"
)
assert (
result["items"][1]["properties"]["photo"][0]
== "http://example.com/jane-object.jpeg"
)
def test_implied_url():
result = parse_fixture("implied_properties/implied_url.html")
for i in range(12):
urls = result["items"][i]["properties"]["url"]
assert len(urls) == 1
assert urls[0] == "http://example.com"
# tests for no url
for i in range(12, 23):
assert "url" not in result["items"][i]["properties"]
def test_stop_implied_url():
"""testing that explicit properties case implied url-parsing to be aborted"""
result = parse_fixture("implied_properties/stop_implied_url.html")
assert "url" not in result["items"][0]["properties"]
assert "url" not in result["items"][1]["properties"]
assert "url" not in result["items"][2]["properties"]
assert "url" not in result["items"][3]["properties"]
assert "url" not in result["items"][4]["properties"]
assert "url" not in result["items"][5]["properties"]
assert result["items"][6]["properties"]["url"] == ["http://example.com/"]
assert result["items"][7]["properties"]["url"] == ["http://example.com/"]
assert result["items"][8]["properties"]["url"] == ["http://example.com/"]
assert result["items"][9]["properties"]["url"] == ["http://example.com/"]
def test_implied_nested_photo():
result = parse_fixture(
"implied_properties/implied_properties.html", url="http://bar.org"
)
assert result["items"][2]["properties"]["photo"][0] == {
"alt": "",
"value": "http://tommorris.org/photo.png",
}
assert (
result["items"][3]["properties"]["photo"][0] == "http://tommorris.org/photo.png"
)
assert result["items"][4]["properties"]["photo"][0] == {
"alt": "Tom Morris",
"value": "http://tommorris.org/photo.png",
}
# src="" is relative to the base url
assert result["items"][6]["properties"]["photo"][0] == "http://bar.org"
def test_implied_nested_photo_alt_name():
result = parse_fixture("implied_properties/implied_properties.html")
assert result["items"][3]["properties"]["name"][0] == "Tom Morris"
def test_implied_image():
result = parse_fixture("implied_properties/implied_properties.html")
assert result["items"][4]["properties"]["photo"][0] == {
"alt": "Tom Morris",
"value": "http://tommorris.org/photo.png",
}
assert result["items"][4]["properties"]["name"][0] == "Tom Morris"
def test_implied_name_empty_alt():
"""An empty alt text should not prevent us from including other
children in the implied name.
"""
result = parse_fixture("implied_properties/implied_name_empty_alt.html")
hcard = result["items"][0]
assert {
"type": ["h-card"],
"properties": {
"name": ["@kylewmahan"],
"url": ["https://twitter.com/kylewmahan"],
"photo": [{"alt": "", "value": "https://example.org/test.jpg"}],
},
} == hcard
def test_relative_datetime():
result = parse_fixture("implied_properties/implied_relative_datetimes.html")
assert result["items"][0]["properties"]["updated"][0] == "2015-01-02 05:06"
def test_stop_implied_name_nested_h():
result = parse_fixture("implied_properties/stop_implied_name_nested_h.html")
assert "name" not in result["items"][0]["properties"]
def test_stop_implied_name_e_content():
result = parse_fixture("implied_properties/stop_implied_name_e_content.html")
assert "name" not in result["items"][0]["properties"]
def test_stop_implied_name_p_content():
result = parse_fixture("implied_properties/stop_implied_name_p_content.html")
assert "name" not in result["items"][0]["properties"]
def test_implied_properties_silo_pub():
result = parse_fixture("implied_properties/implied_properties_silo_pub.html")
item = result["items"][0]
# implied_name = item['properties']['name'][0]
# implied_name = re.sub('\s+', ' ', implied_name).strip()
# assert '@kylewmahan on Twitter', implied_name)
# no implied name expected under new rules
assert "name" not in item["properties"]
def test_simple_person_reference_implied():
result = parse_fixture("implied_properties/simple_person_reference_implied.html")
assert result["items"][0]["properties"] == {"name": ["Frances Berriman"]}
def test_implied_name_alt():
result = parse_fixture("implied_properties/implied_name_alt.html")
assert result["items"][0]["children"][0] == {
"type": ["h-card"],
"properties": {
"name": ["Avatar of Stephen"],
"photo": [{"alt": "Avatar of", "value": "avatar.jpg"}],
},
}
def test_value_name_whitespace():
result = parse_fixture("value_name_whitespace.html")
for i in range(3):
assert result["items"][i]["properties"]["content"][0]["value"] == "Hello World"
assert result["items"][i]["properties"]["name"][0] == "Hello World"
for i in range(3, 7):
assert result["items"][i]["properties"]["content"][0]["value"] == "Hello\nWorld"
assert result["items"][i]["properties"]["name"][0] == "Hello\nWorld"
assert result["items"][7]["properties"]["content"][0]["value"] == "Hello\n\nWorld"
assert result["items"][7]["properties"]["name"][0] == "Hello\n\nWorld"
assert result["items"][8]["properties"]["content"][0]["value"] == "One\nTwo\nThree"
assert result["items"][8]["properties"]["name"][0] == "One\nTwo\nThree"
assert (
result["items"][9]["properties"]["content"][0]["value"] == "One\n\nTwo\n\nThree"
)
assert result["items"][9]["properties"]["name"][0] == "One\n\nTwo\n\nThree"
assert (
result["items"][10]["properties"]["content"][0]["value"]
== "Hello World one\n two\n three\n "
)
assert (
result["items"][10]["properties"]["name"][0]
== "Hello World one\n two\n three\n "
)
assert (
result["items"][11]["properties"]["content"][0]["value"]
== "Correct name Correct summary"
)
assert result["items"][11]["properties"]["name"][0] == "Correct name"
# backcompat tests
def test_backcompat_hentry():
result = parse_fixture("backcompat/hentry.html")
assert "h-entry" in result["items"][0]["type"]
assert (
"Tom Morris"
== result["items"][0]["properties"]["author"][0]["properties"]["name"][0]
)
assert "A Title" == result["items"][0]["properties"]["name"][0]
assert "Some Content" == result["items"][0]["properties"]["content"][0]["value"]
def test_backcompat_hproduct():
result = parse_fixture("backcompat/hproduct.html")
assert 1 == len(result["items"])
assert ["h-product"] == result["items"][0]["type"]
assert ["bullshit"] == result["items"][0]["properties"]["category"]
assert ["Quacktastic Products"] == result["items"][0]["properties"]["brand"]
assert ["#BULLSHIT-001"] == result["items"][0]["properties"]["identifier"]
assert (
"Magical tasty sugar pills that don't do anything."
== result["items"][0]["properties"]["description"][0]
)
assert ["Tom's Magical Quack Tincture"] == result["items"][0]["properties"]["name"]
def test_backcompat_hproduct_nested_hreview():
result = parse_fixture("backcompat/hproduct_hreview_nested.html")
assert ["h-review"] == result["items"][0]["children"][0]["type"]
def test_backcompat_hreview_nested_card_event_product():
result = parse_fixture("backcompat/hreview_nested_card_event_product.html")
assert ["h-review"] == result["items"][0]["type"]
items = result["items"][0]["properties"]["item"]
assert 3 == len(items)
event = items[0]
assert ["h-event"] == event["type"]
assert ["http://example.com/event-url"] == event["properties"]["url"]
assert ["event name"] == event["properties"]["name"]
card = items[1]
assert ["h-card"] == card["type"]
assert ["http://example.com/card-url"] == card["properties"]["url"]
assert ["card name"] == card["properties"]["name"]
product = items[2]
assert ["h-product"] == product["type"]
assert ["http://example.com/product-url"] == product["properties"]["url"]
assert ["product name"] == product["properties"]["name"]
def test_backcompat_rel_bookmark():
"""Confirm that rel=bookmark inside of an h-entry is converted
to u-url.
"""
result = parse_fixture("backcompat/feed_with_rel_bookmark.html")
for ii, url in enumerate(
(
"/2014/11/24/jump-rope",
"/2014/11/23/graffiti",
"/2014/11/21/earth",
"/2014/11/19/labor",
)
):
assert ["h-entry"] == result["items"][ii]["type"]
assert [url] == result["items"][ii]["properties"]["url"]
def test_backcompat_rel_bookmark():
"""Confirm that rel=bookmark inside of an hentry and hreview is converted
to a u-url and original u-url is ignored
"""
tests = [
"backcompat/hentry_with_rel_bookmark.html",
"backcompat/hreview_with_rel_tag_bookmark.html",
]
results = [parse_fixture(x) for x in tests]
for result in results:
assert [
"https://example.com/bookmark",
"https://example.com/bookmark-url",
] == result["items"][0]["properties"]["url"]
def test_backcompat_rel_tag():
"""Confirm that rel=tag inside of an hentry is converted
to a p-category and the last path segment of the href is used.
"""
tests = [
"backcompat/hentry_with_rel_tag.html",
"backcompat/hfeed_with_rel_tag.html",
"backcompat/hrecipe_with_rel_tag.html",
"backcompat/hreview_with_rel_tag_bookmark.html",
]
results = [parse_fixture(x) for x in tests]
for result in results:
assert ["cat", "dog", "mountain lion", "mouse", "meerkat"] == result["items"][
0
]["properties"]["category"]
def test_backcompat_rel_tag_entry_title():
"""Confirm that other backcompat properties on a rel=tag are parsed"""
result = parse_fixture("backcompat/hentry_with_rel_tag_entry_title.html")
assert ["cat"] == result["items"][0]["properties"]["category"]
assert ["rhinoceros"] == result["items"][0]["properties"]["name"]
def test_backcompat_rel_multiple_root():
"""Confirm that rel=tag and rel=bookmark inside of an hentry+hreview is parsed correctly"""
result = parse_fixture("backcompat/hreview_hentry_with_rel_tag_bookmark.html")
assert len(result["items"]) == 1
assert "h-entry" in result["items"][0]["type"]
assert "h-review" in result["items"][0]["type"]
assert ["cat", "dog", "mountain lion", "mouse", "meerkat"] == result["items"][0][
"properties"
]["category"]
assert [
"https://example.com/bookmark",
"https://example.com/bookmark-url",
] == result["items"][0]["properties"]["url"]
def test_backcompat_ignore_mf1_root_if_mf2_present():
"""Confirm that mf1 root class is ignored if another mf2 root class is present."""
result = parse_fixture("backcompat/ignore_mf1_root_if_mf2_present.html")
assert "h-entry" not in result["items"][0]["type"]
assert "h-event" in result["items"][0]["type"]
def test_backcompat_no_implied_properties_mf1_root():
"""Confirm that mf1 root class does not have implied properties"""
result = parse_fixture("backcompat/ignore_mf1_root_if_mf2_present.html")
assert "h-entry" not in result["items"][0]["properties"]
assert "name" not in result["items"][0]["type"]
assert "url" not in result["items"][0]["properties"]
assert "photo" not in result["items"][0]["properties"]
def test_backcompat_ignore_mf2_properties_in_mf1_root():
"""Confirm that mf2 properties are ignored in mf1 root class"""
result = parse_fixture("backcompat/ignore_mf2_properties_in_mf1_root.html")
assert "Correct name" == result["items"][0]["properties"]["name"][0]
assert "Correct summary" == result["items"][0]["properties"]["summary"][0]
def test_backcompat_ignore_mf1_properties_in_mf2_root():
"""Confirm that mf1 properties are ignored in mf2 root class"""
result = parse_fixture("backcompat/ignore_mf1_properties_in_mf2_root.html")
assert "Correct name" == result["items"][0]["properties"]["name"][0]
assert "Correct summary" == result["items"][0]["properties"]["summary"][0]
def test_backcompat_nested_mf2_in_mf1():
"""Confirm that mf2 roots nested inside mf1 root are parsed"""
result = parse_fixture("backcompat/nested_mf2_in_mf1.html")
assert "h-feed" == result["items"][0]["type"][0]
assert "h-entry" == result["items"][0]["children"][0]["type"][0]
assert "Correct name" == result["items"][0]["children"][0]["properties"]["name"][0]
assert (
"Correct summary"
== result["items"][0]["children"][0]["properties"]["summary"][0]
)
def test_backcompat_nested_mf1_in_mf2():
"""Confirm that mf1 roots nested inside mf2 root are parsed"""
result = parse_fixture("backcompat/nested_mf1_in_mf2.html")
assert "h-feed" == result["items"][0]["type"][0]
assert "h-entry" == result["items"][0]["children"][0]["type"][0]
assert "Correct name" == result["items"][0]["children"][0]["properties"]["name"][0]
assert (
"Correct summary"
== result["items"][0]["children"][0]["properties"]["summary"][0]
)
def test_backcompat_nested_mf1_in_mf2_e_content():
"""Confirm that mf1 roots nested inside mf2 root e-content are parsed as authored"""
result = parse_fixture("backcompat/nested_mf1_in_mf2_e_content.html")
mf2_entry = result["items"][0]
mf1_entry = mf2_entry["children"][0]
assert (
'<div class="hentry">\n<span class="entry-title">Correct name</span>\n\n<span class="entry-summary">Correct summary</span>\n</div>'
== mf2_entry["properties"]["content"][0]["html"]
)
assert (
"Correct name Correct summary" == mf2_entry["properties"]["content"][0]["value"]
)
assert "h-entry" == mf1_entry["type"][0]
assert "Correct name" == mf1_entry["properties"]["name"][0]
assert "Correct summary" == mf1_entry["properties"]["summary"][0]
def test_backcompat_hentry_content_html():
"""Confirm that mf1 entry-content html is parsed as authored without mf2 replacements"""
result = parse_fixture("backcompat/hentry_content_html.html")
entry = result["items"][0]
assert (
'<p class="entry-summary">This is a summary</p> \n <p>This is <a href="/tags/mytag" rel="tag">mytag</a> inside content. </p>'
== entry["properties"]["content"][0]["html"]
)
def test_whitespace_with_tags_inside_property():
"""Whitespace should only be trimmed at the ends of textContent, not inside.
https://github.com/microformats/mf2py/issues/112
"""
result = parse_fixture("tag_whitespace_inside_p_value.html")
assert result["items"][0]["properties"] == {"name": ["foo bar"]}
def test_plaintext_p_whitespace():
result = parse_fixture("plaintext_p_whitespace.html")
assert result["items"][0]["properties"]["content"][0]["value"] == "foo\nbar baz"
assert result["items"][1]["properties"]["content"][0]["value"] == "foo\nbar baz"
assert result["items"][2]["properties"]["content"][0]["value"] == "foo bar\nbaz"
def test_plaintext_img_whitespace():
result = parse_fixture("plaintext_img_whitespace.html")
assert (
result["items"][0]["properties"]["content"][0]["value"]
== "selfie At some tourist spot"
)
assert (
result["items"][1]["properties"]["content"][0]["value"]
== "At another tourist spot"
)
assert (
result["items"][2]["properties"]["content"][0]["value"]
== "https://example.com/photo.jpg At yet another tourist spot"
)
def test_photo_with_alt():
"""Confirm that alt text in img is parsed as a u-* property and implied photo"""
path = "img_with_alt.html"
result = parse_fixture(path)
with open(os.path.join(TEST_DIR, path)) as f:
exp_result = Parser(doc=f, html_parser="html5lib").to_dict()
# simple img with u-*
assert "/photo.jpg" == result["items"][0]["properties"]["photo"][0]
assert "/photo.jpg" == exp_result["items"][0]["properties"]["photo"][0]
assert {"alt": "alt text", "value": "/photo.jpg"} == result["items"][1][
"properties"
]["url"][0]
assert "/photo.jpg" == exp_result["items"][1]["properties"]["url"][0]["value"]
assert "alt text" == exp_result["items"][1]["properties"]["url"][0]["alt"]
assert {"alt": "", "value": "/photo.jpg"} == result["items"][2]["properties"][
"in-reply-to"
][0]
assert (
"/photo.jpg" == exp_result["items"][2]["properties"]["in-reply-to"][0]["value"]
)
assert "" == exp_result["items"][2]["properties"]["in-reply-to"][0]["alt"]
# img with u-* and h-* example
assert "h-cite" in result["items"][3]["properties"]["in-reply-to"][0]["type"]
assert (
"/photo.jpg"
== result["items"][3]["properties"]["in-reply-to"][0]["properties"]["photo"][0]
)
assert "/photo.jpg" == result["items"][3]["properties"]["in-reply-to"][0]["value"]
assert "alt" not in result["items"][3]["properties"]["in-reply-to"][0]
assert "h-cite" in exp_result["items"][3]["properties"]["in-reply-to"][0]["type"]
assert (
"/photo.jpg"
== exp_result["items"][3]["properties"]["in-reply-to"][0]["properties"][
"photo"
][0]
)
assert (
"/photo.jpg" == exp_result["items"][3]["properties"]["in-reply-to"][0]["value"]
)
assert "alt" not in exp_result["items"][3]["properties"]["in-reply-to"][0]
assert "h-cite" in result["items"][4]["properties"]["in-reply-to"][0]["type"]
assert {"alt": "alt text", "value": "/photo.jpg"} == result["items"][4][
"properties"
]["in-reply-to"][0]["properties"]["photo"][0]
assert "/photo.jpg" == result["items"][4]["properties"]["in-reply-to"][0]["value"]
assert "alt" in result["items"][4]["properties"]["in-reply-to"][0]
assert "h-cite" in exp_result["items"][4]["properties"]["in-reply-to"][0]["type"]
assert (
"/photo.jpg"
== exp_result["items"][4]["properties"]["in-reply-to"][0]["properties"][
"photo"
][0]["value"]
)
assert (
"/photo.jpg" == exp_result["items"][4]["properties"]["in-reply-to"][0]["value"]
)
assert (
"alt text"
== exp_result["items"][4]["properties"]["in-reply-to"][0]["properties"][
"photo"
][0]["alt"]
)
assert "alt text" == exp_result["items"][4]["properties"]["in-reply-to"][0]["alt"]
assert "h-cite" in result["items"][5]["properties"]["in-reply-to"][0]["type"]
assert {"alt": "", "value": "/photo.jpg"} == result["items"][5]["properties"][
"in-reply-to"
][0]["properties"]["photo"][0]
assert "/photo.jpg" == result["items"][5]["properties"]["in-reply-to"][0]["value"]
assert "alt" in result["items"][5]["properties"]["in-reply-to"][0]
assert "h-cite" in exp_result["items"][5]["properties"]["in-reply-to"][0]["type"]
assert (
"/photo.jpg"
== exp_result["items"][5]["properties"]["in-reply-to"][0]["properties"][
"photo"
][0]["value"]
)
assert (
"/photo.jpg" == exp_result["items"][5]["properties"]["in-reply-to"][0]["value"]