Skip to content

Commit 3437e15

Browse files
committed
fix element creation
1 parent a473acf commit 3437e15

File tree

5 files changed

+73
-66
lines changed

5 files changed

+73
-66
lines changed

src/odr/internal/odf/odf_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ parse_element_tree(ElementRegistry &registry, const ElementType type,
4747
const ExtendedElementIdentifier element_id = registry.create_element();
4848
ElementRegistry::Element &element = registry.element(element_id);
4949
element.type = type;
50+
element.node = node;
5051

5152
children_parser(registry, element_id, node);
5253

src/odr/internal/ooxml/presentation/ooxml_presentation_parser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ parse_element_tree(ElementRegistry &registry, const ElementType type,
4848
const ExtendedElementIdentifier element_id = registry.create_element();
4949
ElementRegistry::Element &element = registry.element(element_id);
5050
element.type = type;
51+
element.node = node;
5152

5253
children_parser(registry, element_id, node);
5354

src/odr/internal/ooxml/spreadsheet/ooxml_spreadsheet_parser.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ parse_element_tree(ElementRegistry &registry, const ParseContext &context,
5252
const ExtendedElementIdentifier element_id = registry.create_element();
5353
ElementRegistry::Element &element = registry.element(element_id);
5454
element.type = type;
55+
element.node = node;
5556

5657
children_parser(registry, context, element_id, node);
5758

@@ -113,6 +114,7 @@ parse_sheet_element(ElementRegistry &registry, const ParseContext &context,
113114
const ExtendedElementIdentifier element_id = registry.create_element();
114115
ElementRegistry::Element &element = registry.element(element_id);
115116
element.type = ElementType::sheet;
117+
element.node = node;
116118
ElementRegistry::Sheet &sheet = registry.create_sheet_element(element_id);
117119

118120
for (const pugi::xml_node col_node : node.child("cols").children("col")) {

src/odr/internal/ooxml/text/ooxml_text_parser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ parse_element_tree(ElementRegistry &registry, const ElementType type,
4848
const ExtendedElementIdentifier element_id = registry.create_element();
4949
ElementRegistry::Element &element = registry.element(element_id);
5050
element.type = type;
51+
element.node = node;
5152

5253
children_parser(registry, element_id, node);
5354

@@ -108,6 +109,7 @@ parse_list_element(ElementRegistry &registry, pugi::xml_node node) {
108109
const ExtendedElementIdentifier element_id = registry.create_element();
109110
ElementRegistry::Element &element = registry.element(element_id);
110111
element.type = ElementType::list;
112+
element.node = node;
111113

112114
for (; is_list_item(node); node = node.next_sibling()) {
113115
const std::int32_t level = list_level(node);
@@ -150,6 +152,7 @@ parse_table_row_element(ElementRegistry &registry, const pugi::xml_node node) {
150152
const ExtendedElementIdentifier element_id = registry.create_element();
151153
ElementRegistry::Element &element = registry.element(element_id);
152154
element.type = ElementType::table_row;
155+
element.node = node;
153156

154157
for (const pugi::xml_node cell_node : node.children("w:tc")) {
155158
auto [cell_id, _] =
@@ -170,6 +173,7 @@ parse_table_element(ElementRegistry &registry, const pugi::xml_node node) {
170173
const ExtendedElementIdentifier element_id = registry.create_element();
171174
ElementRegistry::Element &element = registry.element(element_id);
172175
element.type = ElementType::table_row;
176+
element.node = node;
173177

174178
for (const pugi::xml_node column_node :
175179
node.child("w:tblGrid").children("w:gridCol")) {

test/src/document_test.cpp

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ TEST(Document, odt) {
2525
EXPECT_EQ(document.document_type(), DocumentType::text);
2626

2727
const auto page_layout = document.root_element().as_text_root().page_layout();
28-
EXPECT_TRUE(page_layout.width);
28+
EXPECT_TRUE(page_layout.width.has_value());
2929
EXPECT_EQ(Measure("8.2673in"), page_layout.width);
30-
EXPECT_TRUE(page_layout.height);
30+
EXPECT_TRUE(page_layout.height.has_value());
3131
EXPECT_EQ(Measure("11.6925in"), page_layout.height);
32-
EXPECT_TRUE(page_layout.margin.top);
32+
EXPECT_TRUE(page_layout.margin.top.has_value());
3333
EXPECT_EQ(Measure("0.7874in"), page_layout.margin.top);
3434
}
3535

@@ -91,11 +91,11 @@ TEST(Document, odg) {
9191

9292
for (auto child : document.root_element().children()) {
9393
auto page_layout = child.as_page().page_layout();
94-
EXPECT_TRUE(page_layout.width);
94+
EXPECT_TRUE(page_layout.width.has_value());
9595
EXPECT_EQ(Measure("21cm"), page_layout.width);
96-
EXPECT_TRUE(page_layout.height);
96+
EXPECT_TRUE(page_layout.height.has_value());
9797
EXPECT_EQ(Measure("29.7cm"), page_layout.height);
98-
EXPECT_TRUE(page_layout.margin.top);
98+
EXPECT_TRUE(page_layout.margin.top.has_value());
9999
EXPECT_EQ(Measure("1cm"), page_layout.margin.top);
100100
}
101101
}
@@ -107,7 +107,7 @@ TEST(Document, edit_odt) {
107107
TestData::test_file_path("odr-public/odt/about.odt"), *logger);
108108
const Document document = document_file.document();
109109

110-
std::function<void(Element)> edit = [&](const Element element) {
110+
std::function<void(Element)> edit = [&](const Element &element) {
111111
for (const Element child : element.children()) {
112112
edit(child);
113113
}
@@ -124,7 +124,7 @@ TEST(Document, edit_odt) {
124124

125125
const DocumentFile validate_file(output_path);
126126
const Document validate_document = validate_file.document();
127-
std::function<void(Element)> validate = [&](const Element element) {
127+
std::function<void(Element)> validate = [&](const Element &element) {
128128
for (const Element child : element.children()) {
129129
validate(child);
130130
}
@@ -144,7 +144,7 @@ TEST(Document, edit_docx) {
144144
*logger);
145145
const Document document = document_file.document();
146146

147-
std::function<void(Element)> edit = [&](const Element element) {
147+
std::function<void(Element)> edit = [&](const Element &element) {
148148
for (const Element child : element.children()) {
149149
edit(child);
150150
}
@@ -161,7 +161,7 @@ TEST(Document, edit_docx) {
161161

162162
const DocumentFile validate_file(output_path);
163163
const Document validate_document = validate_file.document();
164-
std::function<void(Element)> validate = [&](const Element element) {
164+
std::function<void(Element)> validate = [&](const Element &element) {
165165
for (const Element child : element.children()) {
166166
validate(child);
167167
}
@@ -208,62 +208,61 @@ TEST(Document, edit_odt_diff) {
208208
.content());
209209
}
210210

211-
// TODO make sheet editing work by implementing table addressing
212-
// TEST(Document, edit_ods_diff) {
213-
// auto logger = Logger::create_stdio("odr-test", LogLevel::verbose);
214-
//
215-
// auto diff =
216-
// R"({"modifiedText":{"/child:0/row:0/child:0/child:0/child:0":"Page 1
217-
// hi","/child:1/row:0/child:0/child:0/child:0":"Page 2
218-
// hihi","/child:2/row:0/child:0/child:0/child:0":"Page 3
219-
// hihihi","/child:3/row:0/child:0/child:0/child:0":"Page 4
220-
// hihihihi","/child:4/row:0/child:0/child:0/child:0":"Page 5
221-
// hihihihihi"}})";
222-
// DocumentFile document_file(
223-
// TestData::test_file_path("odr-public/ods/pages.ods"), *logger);
224-
// document_file = document_file.decrypt(
225-
// TestData::test_file("odr-public/ods/pages.ods").password.value());
226-
// Document document = document_file.document();
227-
//
228-
// html::edit(document, diff);
229-
//
230-
// std::string output_path =
231-
// (std::filesystem::current_path() / "pages_edit_diff.ods").string();
232-
// document.save(output_path);
233-
//
234-
// DocumentFile validate_file(output_path);
235-
// Document validate_document = validate_file.document();
236-
// EXPECT_EQ(
237-
// "Page 1 hi",
238-
// DocumentPath::find(validate_document.root_element(),
239-
// DocumentPath("/child:0/row:0/child:0/child:0/child:0"))
240-
// .as_text()
241-
// .content());
242-
// EXPECT_EQ(
243-
// "Page 2 hihi",
244-
// DocumentPath::find(validate_document.root_element(),
245-
// DocumentPath("/child:1/row:0/child:0/child:0/child:0"))
246-
// .as_text()
247-
// .content());
248-
// EXPECT_EQ(
249-
// "Page 3 hihihi",
250-
// DocumentPath::find(validate_document.root_element(),
251-
// DocumentPath("/child:2/row:0/child:0/child:0/child:0"))
252-
// .as_text()
253-
// .content());
254-
// EXPECT_EQ(
255-
// "Page 4 hihihihi",
256-
// DocumentPath::find(validate_document.root_element(),
257-
// DocumentPath("/child:3/row:0/child:0/child:0/child:0"))
258-
// .as_text()
259-
// .content());
260-
// EXPECT_EQ(
261-
// "Page 5 hihihihihi",
262-
// DocumentPath::find(validate_document.root_element(),
263-
// DocumentPath("/child:4/row:0/child:0/child:0/child:0"))
264-
// .as_text()
265-
// .content());
266-
// }
211+
TEST(Document, edit_ods_diff) {
212+
const auto logger = Logger::create_stdio("odr-test", LogLevel::verbose);
213+
214+
const auto diff =
215+
R"({"modifiedText":{"/child:0/row:0/child:0/child:0/child:0":"Page 1
216+
hi","/child:1/row:0/child:0/child:0/child:0":"Page 2
217+
hihi","/child:2/row:0/child:0/child:0/child:0":"Page 3
218+
hihihi","/child:3/row:0/child:0/child:0/child:0":"Page 4
219+
hihihihi","/child:4/row:0/child:0/child:0/child:0":"Page 5
220+
hihihihihi"}})";
221+
DocumentFile document_file(
222+
TestData::test_file_path("odr-public/ods/pages.ods"), *logger);
223+
document_file = document_file.decrypt(
224+
TestData::test_file("odr-public/ods/pages.ods").password.value());
225+
const Document document = document_file.document();
226+
227+
html::edit(document, diff);
228+
229+
const std::string output_path =
230+
(std::filesystem::current_path() / "pages_edit_diff.ods").string();
231+
document.save(output_path);
232+
233+
const DocumentFile validate_file(output_path);
234+
const Document validate_document = validate_file.document();
235+
EXPECT_EQ(
236+
"Page 1 hi",
237+
DocumentPath::find(validate_document.root_element(),
238+
DocumentPath("/child:0/row:0/child:0/child:0/child:0"))
239+
.as_text()
240+
.content());
241+
EXPECT_EQ(
242+
"Page 2 hihi",
243+
DocumentPath::find(validate_document.root_element(),
244+
DocumentPath("/child:1/row:0/child:0/child:0/child:0"))
245+
.as_text()
246+
.content());
247+
EXPECT_EQ(
248+
"Page 3 hihihi",
249+
DocumentPath::find(validate_document.root_element(),
250+
DocumentPath("/child:2/row:0/child:0/child:0/child:0"))
251+
.as_text()
252+
.content());
253+
EXPECT_EQ(
254+
"Page 4 hihihihi",
255+
DocumentPath::find(validate_document.root_element(),
256+
DocumentPath("/child:3/row:0/child:0/child:0/child:0"))
257+
.as_text()
258+
.content());
259+
EXPECT_EQ(
260+
"Page 5 hihihihihi",
261+
DocumentPath::find(validate_document.root_element(),
262+
DocumentPath("/child:4/row:0/child:0/child:0/child:0"))
263+
.as_text()
264+
.content());
265+
}
267266

268267
TEST(Document, edit_docx_diff) {
269268
const auto logger = Logger::create_stdio("odr-test", LogLevel::verbose);

0 commit comments

Comments
 (0)