Commit 369b8e8097346924b056ebec4298571ee81a2f70

Authored by m-holger
1 parent 12e413a0

Refactor: rename `getTopLevelField` to `root_field`, update logic for clarity and consistency.

libqpdf/QPDFFormFieldObjectHelper.cc
@@ -54,24 +54,25 @@ QPDFFormFieldObjectHelper::getParent() @@ -54,24 +54,25 @@ QPDFFormFieldObjectHelper::getParent()
54 QPDFFormFieldObjectHelper 54 QPDFFormFieldObjectHelper
55 QPDFFormFieldObjectHelper::getTopLevelField(bool* is_different) 55 QPDFFormFieldObjectHelper::getTopLevelField(bool* is_different)
56 { 56 {
57 - return Null::if_null(m->getTopLevelField(is_different).oh()); 57 + return Null::if_null(m->root_field(is_different).oh());
58 } 58 }
59 59
60 FormField 60 FormField
61 -FormField::getTopLevelField(bool* is_different) 61 +FormField::root_field(bool* is_different)
62 { 62 {
63 if (!obj) { 63 if (!obj) {
64 return {}; 64 return {};
65 } 65 }
66 - auto top_field = oh(); 66 + auto rf = *this;
  67 + size_t depth = 0; // Don't bother with loop detection until depth becomes suspicious
67 QPDFObjGen::set seen; 68 QPDFObjGen::set seen;
68 - while (seen.add(top_field) && !top_field.getKeyIfDict("/Parent").null()) {  
69 - top_field = top_field.getKey("/Parent"); 69 + while (rf.Parent() && (++depth < 10 || seen.add(rf))) {
  70 + rf = rf.Parent();
70 if (is_different) { 71 if (is_different) {
71 *is_different = true; 72 *is_different = true;
72 } 73 }
73 } 74 }
74 - return {top_field}; 75 + return rf;
75 } 76 }
76 77
77 QPDFObjectHandle 78 QPDFObjectHandle
libqpdf/qpdf/FormField.hh
@@ -46,10 +46,19 @@ namespace qpdf::impl @@ -46,10 +46,19 @@ namespace qpdf::impl
46 return {get("/Parent")}; 46 return {get("/Parent")};
47 } 47 }
48 48
49 - // Return the top-level field for this field. Typically this will be the field itself or its  
50 - // parent. If is_different is provided, it is set to true if the top-level field is  
51 - // different from the field itself; otherwise it is set to false.  
52 - FormField getTopLevelField(bool* is_different = nullptr); 49 + /// @brief Returns the top-level field associated with the current field.
  50 + ///
  51 + /// The function traverses the hierarchy of parent fields to identify the highest-level
  52 + /// field in the tree. Typically, this will be the current field itself unless it has a
  53 + /// parent field. Optionally, it can indicate whether the top-level field is different from
  54 + /// the current field.
  55 + ///
  56 + /// @param is_different A pointer to a boolean that, if provided, will be set to true if the
  57 + /// top-level field differs from the current field; otherwise, it will be set to
  58 + /// false.
  59 + ///
  60 + /// @return The top-level field in the form field hierarchy.
  61 + FormField root_field(bool* is_different = nullptr);
53 62
54 // Get a field value, possibly inheriting the value from an ancestor node. 63 // Get a field value, possibly inheriting the value from an ancestor node.
55 QPDFObjectHandle getInheritableFieldValue(std::string const& name); 64 QPDFObjectHandle getInheritableFieldValue(std::string const& name);