|
2 | 2 | import sys
|
3 | 3 |
|
4 | 4 | from precisely import assert_that, is_sequence
|
5 |
| -from nose.tools import istest, assert_equal |
| 5 | +from nose.tools import istest, assert_equal, assert_is_none |
6 | 6 | from nose_parameterized import parameterized, param
|
7 | 7 | import funk
|
8 | 8 |
|
@@ -885,39 +885,92 @@ class ImageTests(object):
|
885 | 885 | IMAGE_RELATIONSHIP_ID = "rId5"
|
886 | 886 |
|
887 | 887 | def _read_embedded_image(self, element):
|
| 888 | + return self._read_embedded_images(element)[0] |
| 889 | + |
| 890 | + def _read_embedded_images(self, element): |
888 | 891 | relationships = Relationships([
|
889 | 892 | _image_relationship(self.IMAGE_RELATIONSHIP_ID, "media/hat.png"),
|
890 | 893 | ])
|
891 |
| - |
892 | 894 | mocks = funk.Mocks()
|
893 | 895 | docx_file = mocks.mock()
|
894 | 896 | funk.allows(docx_file).open("word/media/hat.png").returns(io.BytesIO(self.IMAGE_BYTES))
|
895 |
| - |
896 | 897 | content_types = mocks.mock()
|
897 | 898 | funk.allows(content_types).find_content_type("word/media/hat.png").returns("image/png")
|
898 |
| - |
899 |
| - return _read_and_get_document_xml_element( |
| 899 | + return _read_and_get_document_xml_elements( |
900 | 900 | element,
|
901 | 901 | content_types=content_types,
|
902 | 902 | relationships=relationships,
|
903 | 903 | docx_file=docx_file,
|
904 | 904 | )
|
905 | 905 |
|
906 | 906 | @istest
|
907 |
| - def can_read_imagedata_elements_with_rid_attribute(self): |
908 |
| - imagedata_element = xml_element("v:imagedata", { |
909 |
| - "r:id": self.IMAGE_RELATIONSHIP_ID, |
910 |
| - "o:title": "It's a hat" |
911 |
| - }) |
| 907 | + def can_read_shape_elements_with_rid_and_size_attributes(self): |
| 908 | + shape_element = xml_element("v:shape", {"style": "width:31.5pt;height:38.25pt"}, [ |
| 909 | + xml_element("v:imagedata", { |
| 910 | + "r:id": self.IMAGE_RELATIONSHIP_ID, |
| 911 | + "o:title": "It's a hat" |
| 912 | + }) |
| 913 | + ]) |
912 | 914 |
|
913 |
| - image = self._read_embedded_image(imagedata_element) |
| 915 | + image = self._read_embedded_image(shape_element) |
914 | 916 |
|
915 | 917 | assert_equal(documents.Image, type(image))
|
916 | 918 | assert_equal("It's a hat", image.alt_text)
|
917 | 919 | assert_equal("image/png", image.content_type)
|
| 920 | + assert_equal(documents.Size(width="31.5pt", height="38.25pt"), image.size) |
918 | 921 | with image.open() as image_file:
|
919 | 922 | assert_equal(self.IMAGE_BYTES, image_file.read())
|
920 | 923 |
|
| 924 | + @istest |
| 925 | + def cannot_resize_shape_with_multiple_nodes(self): |
| 926 | + shape_element = xml_element("v:shape", {"style": "width:31.5pt;height:38.25pt"}, [ |
| 927 | + xml_element("v:imagedata", { |
| 928 | + "r:id": self.IMAGE_RELATIONSHIP_ID, |
| 929 | + "o:title": "It's a hat" |
| 930 | + }), |
| 931 | + xml_element("v:textbox", {}, [ |
| 932 | + xml_element("w:txbxContent", {}, [ |
| 933 | + _paragraph_with_style_id("textbox-content") |
| 934 | + ]) |
| 935 | + ]) |
| 936 | + ]) |
| 937 | + |
| 938 | + nodes = self._read_embedded_images(shape_element) |
| 939 | + |
| 940 | + assert_equal(2, len(nodes)) |
| 941 | + image_node = nodes[0] |
| 942 | + assert_equal(documents.Image, type(image_node)) |
| 943 | + assert_equal("It's a hat", image_node.alt_text) |
| 944 | + assert_is_none(image_node.size) |
| 945 | + |
| 946 | + @istest |
| 947 | + def can_read_shape_elements_with_unused_style_elements(self): |
| 948 | + shape_element = xml_element("v:shape", {"style": "width:31.5pt;position:absolute;height:38.25pt"}, [ |
| 949 | + xml_element("v:imagedata", { |
| 950 | + "r:id": self.IMAGE_RELATIONSHIP_ID, |
| 951 | + "o:title": "It's a hat" |
| 952 | + }) |
| 953 | + ]) |
| 954 | + |
| 955 | + image = self._read_embedded_image(shape_element) |
| 956 | + |
| 957 | + assert_equal(documents.Image, type(image)) |
| 958 | + assert_equal(documents.Size(width="31.5pt", height="38.25pt"), image.size) |
| 959 | + |
| 960 | + @istest |
| 961 | + def can_read_shape_elements_with_inch_size_attributes(self): |
| 962 | + shape_element = xml_element("v:shape", {"style": "width:0.58in;height:0.708in"}, [ |
| 963 | + xml_element("v:imagedata", { |
| 964 | + "r:id": self.IMAGE_RELATIONSHIP_ID, |
| 965 | + "o:title": "It's a hat" |
| 966 | + }) |
| 967 | + ]) |
| 968 | + |
| 969 | + image = self._read_embedded_image(shape_element) |
| 970 | + |
| 971 | + assert_equal(documents.Image, type(image)) |
| 972 | + assert_equal(documents.Size(width="0.58in", height="0.708in"), image.size) |
| 973 | + |
921 | 974 | @istest
|
922 | 975 | def when_imagedata_element_has_no_relationship_id_then_it_is_ignored_with_warning(self):
|
923 | 976 | imagedata_element = xml_element("v:imagedata")
|
|
0 commit comments