1010
1111#include < optional>
1212
13- #include < pugixml.hpp>
14-
1513namespace odr ::internal::ooxml::text {
1614
17- Element::Element (pugi::xml_node node) : m_node{node} {
15+ Element::Element (const pugi::xml_node node) : m_node{node} {
1816 if (!node) {
1917 // TODO log error
2018 throw std::runtime_error (" node not set" );
@@ -28,8 +26,7 @@ ResolvedStyle Element::partial_style(const abstract::Document *) const {
2826ResolvedStyle
2927Element::intermediate_style (const abstract::Document *document) const {
3028 ResolvedStyle base;
31- abstract::Element *parent = this ->parent (document);
32- if (parent == nullptr ) {
29+ if (abstract::Element *parent = this ->parent (document); parent == nullptr ) {
3330 base = style_ (document)->default_style ()->resolved ();
3431 } else {
3532 base = dynamic_cast <Element *>(parent)->intermediate_style (document);
@@ -87,9 +84,9 @@ TextStyle Span::style(const abstract::Document *document) const {
8784 return intermediate_style (document).text_style ;
8885}
8986
90- Text::Text (pugi::xml_node node) : Text(node, node) {}
87+ Text::Text (const pugi::xml_node node) : Text(node, node) {}
9188
92- Text::Text (pugi::xml_node first, pugi::xml_node last)
89+ Text::Text (const pugi::xml_node first, const pugi::xml_node last)
9390 : Element(first), m_last{last} {
9491 if (!last) {
9592 // TODO log error
@@ -111,14 +108,14 @@ void Text::set_content(const abstract::Document *, const std::string &text) {
111108 // <w:t xml:space="preserve">
112109 // use `xml:space`
113110
114- auto parent = m_node.parent ();
115- auto old_first = m_node;
116- auto old_last = m_last;
117- auto new_first = old_first;
118- auto new_last = m_last;
111+ pugi::xml_node parent = m_node.parent ();
112+ const pugi::xml_node old_first = m_node;
113+ const pugi::xml_node old_last = m_last;
114+ pugi::xml_node new_first = old_first;
115+ pugi::xml_node new_last = m_last;
119116
120117 const auto insert_node = [&](const char *node) {
121- auto new_node = parent.insert_child_before (node, old_first);
118+ const pugi::xml_node new_node = parent.insert_child_before (node, old_first);
122119 if (new_first == old_first) {
123120 new_first = new_node;
124121 }
@@ -154,8 +151,8 @@ void Text::set_content(const abstract::Document *, const std::string &text) {
154151 m_node = new_first;
155152 m_last = new_last;
156153
157- for (auto node = old_first; node != old_last.next_sibling ();) {
158- auto next = node.next_sibling ();
154+ for (pugi::xml_node node = old_first; node != old_last.next_sibling ();) {
155+ const pugi::xml_node next = node.next_sibling ();
159156 parent.remove_child (node);
160157 node = next;
161158 }
@@ -166,7 +163,7 @@ TextStyle Text::style(const abstract::Document *document) const {
166163}
167164
168165std::string Text::text_ (const pugi::xml_node node) {
169- std::string name = node.name ();
166+ const std::string name = node.name ();
170167
171168 if (name == " w:t" ) {
172169 return node.text ().get ();
@@ -179,12 +176,13 @@ std::string Text::text_(const pugi::xml_node node) {
179176}
180177
181178std::string Link::href (const abstract::Document *document) const {
182- if (auto anchor = m_node.attribute (" w:anchor" )) {
179+ if (const pugi::xml_attribute anchor = m_node.attribute (" w:anchor" )) {
183180 return std::string (" #" ) + anchor.value ();
184181 }
185- if (auto ref = m_node.attribute (" r:id" )) {
182+ if (const pugi::xml_attribute ref = m_node.attribute (" r:id" )) {
186183 auto relations = document_relations_ (document);
187- if (auto rel = relations.find (ref.value ()); rel != std::end (relations)) {
184+ if (const auto rel = relations.find (ref.value ());
185+ rel != std::end (relations)) {
188186 return rel->second ;
189187 }
190188 }
@@ -213,7 +211,8 @@ TableStyle Table::style(const abstract::Document *document) const {
213211
214212TableColumnStyle TableColumn::style (const abstract::Document *) const {
215213 TableColumnStyle result;
216- if (auto width = read_twips_attribute (m_node.attribute (" w:w" ))) {
214+ if (const std::optional<Measure> width =
215+ read_twips_attribute (m_node.attribute (" w:w" ))) {
217216 result.width = width;
218217 }
219218 return result;
@@ -254,7 +253,7 @@ std::optional<std::string> Frame::y(const abstract::Document *) const {
254253
255254std::optional<std::string>
256255Frame::width (const abstract::Document *document) const {
257- if (auto width = read_emus_attribute (
256+ if (const std::optional<Measure> width = read_emus_attribute (
258257 inner_node_ (document).child (" wp:extent" ).attribute (" cx" ))) {
259258 return width->to_string ();
260259 }
@@ -263,7 +262,7 @@ Frame::width(const abstract::Document *document) const {
263262
264263std::optional<std::string>
265264Frame::height (const abstract::Document *document) const {
266- if (auto height = read_emus_attribute (
265+ if (const std::optional<Measure> height = read_emus_attribute (
267266 inner_node_ (document).child (" wp:extent" ).attribute (" cy" ))) {
268267 return height->to_string ();
269268 }
@@ -277,17 +276,18 @@ std::optional<std::string> Frame::z_index(const abstract::Document *) const {
277276GraphicStyle Frame::style (const abstract::Document *) const { return {}; }
278277
279278pugi::xml_node Frame::inner_node_ (const abstract::Document *) const {
280- if (auto anchor = m_node.child (" wp:anchor" )) {
279+ if (const pugi::xml_node anchor = m_node.child (" wp:anchor" )) {
281280 return anchor;
282- } else if (auto inline_node = m_node.child (" wp:inline" )) {
281+ }
282+ if (const pugi::xml_node inline_node = m_node.child (" wp:inline" )) {
283283 return inline_node;
284284 }
285285 return {};
286286}
287287
288288bool Image::is_internal (const abstract::Document *document) const {
289- auto doc = document_ (document);
290- if (! doc || !doc->as_filesystem ()) {
289+ const Document * doc = document_ (document);
290+ if (doc == nullptr || !doc->as_filesystem ()) {
291291 return false ;
292292 }
293293 try {
@@ -297,22 +297,23 @@ bool Image::is_internal(const abstract::Document *document) const {
297297 return false ;
298298}
299299
300- std::optional<odr:: File> Image::file (const abstract::Document *document) const {
301- auto doc = document_ (document);
302- if (! doc || !is_internal (document)) {
300+ std::optional<File> Image::file (const abstract::Document *document) const {
301+ const Document * doc = document_ (document);
302+ if (doc == nullptr || !is_internal (document)) {
303303 return {};
304304 }
305- AbsPath path = Path (href (document)).make_absolute ();
305+ const AbsPath path = Path (href (document)).make_absolute ();
306306 return File (doc->as_filesystem ()->open (path));
307307}
308308
309309std::string Image::href (const abstract::Document *document) const {
310- if (auto ref = m_node.child (" pic:pic" )
311- .child (" pic:blipFill" )
312- .child (" a:blip" )
313- .attribute (" r:embed" )) {
310+ if (const pugi::xml_attribute ref = m_node.child (" pic:pic" )
311+ .child (" pic:blipFill" )
312+ .child (" a:blip" )
313+ .attribute (" r:embed" )) {
314314 auto relations = document_relations_ (document);
315- if (auto rel = relations.find (ref.value ()); rel != std::end (relations)) {
315+ if (const auto rel = relations.find (ref.value ());
316+ rel != std::end (relations)) {
316317 return AbsPath (" /word" ).join (RelPath (rel->second )).string ();
317318 }
318319 }
0 commit comments