Return ByteStringViews from xfa_basic_data.cpp functions.

We can widen later as needed, but the underlying data is single-byte.
Add method to retrieve element name for someday de-duping class names.
Add test for the XFA_*ToName() functions.
Remove redundant test now present in fxfa_basic_unittest.cpp

Change-Id: I48ea0e3985eece2bfa5d8f08dfdae13933290b6d
Reviewed-on: https://pdfium-review.googlesource.com/c/47130
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp
index 9e8188d..a762027 100644
--- a/fxjs/xfa/cjx_object.cpp
+++ b/fxjs/xfa/cjx_object.cpp
@@ -345,7 +345,8 @@
   CFX_XMLElement* elem = SetValue(eAttr, XFA_AttributeType::Boolean,
                                   (void*)(uintptr_t)bValue, bNotify);
   if (elem)
-    elem->SetAttribute(XFA_AttributeToName(eAttr), bValue ? L"1" : L"0");
+    elem->SetAttribute(WideString::FromASCII(XFA_AttributeToName(eAttr)),
+                       bValue ? L"1" : L"0");
 }
 
 bool CJX_Object::GetBoolean(XFA_Attribute eAttr) {
@@ -356,7 +357,7 @@
   CFX_XMLElement* elem = SetValue(eAttr, XFA_AttributeType::Integer,
                                   (void*)(uintptr_t)iValue, bNotify);
   if (elem) {
-    elem->SetAttribute(XFA_AttributeToName(eAttr),
+    elem->SetAttribute(WideString::FromASCII(XFA_AttributeToName(eAttr)),
                        WideString::Format(L"%d", iValue));
   }
 }
@@ -397,7 +398,7 @@
   CFX_XMLElement* elem = SetValue(eAttr, XFA_AttributeType::Enum,
                                   (void*)(uintptr_t)eValue, bNotify);
   if (elem) {
-    elem->SetAttribute(XFA_AttributeToName(eAttr),
+    elem->SetAttribute(WideString::FromASCII(XFA_AttributeToName(eAttr)),
                        WideString::FromASCII(XFA_AttributeValueToName(eValue)));
   }
 }
@@ -478,7 +479,7 @@
     return;
   }
 
-  WideString wsAttrName = XFA_AttributeToName(eAttr);
+  WideString wsAttrName = WideString::FromASCII(XFA_AttributeToName(eAttr));
   if (eAttr == XFA_Attribute::ContentType)
     wsAttrName = L"xfa:" + wsAttrName;
 
diff --git a/xfa/fxfa/parser/xfa_basic_data.cpp b/xfa/fxfa/parser/xfa_basic_data.cpp
index 395add8..6c53325 100644
--- a/xfa/fxfa/parser/xfa_basic_data.cpp
+++ b/xfa/fxfa/parser/xfa_basic_data.cpp
@@ -211,6 +211,10 @@
 
 }  // namespace
 
+ByteStringView XFA_ElementToName(XFA_Element elem) {
+  return g_ElementTable[static_cast<size_t>(elem)].name;
+}
+
 XFA_Element XFA_GetElementByName(const WideString& name) {
   uint32_t hash = FX_HashCode_GetW(name.AsStringView(), false);
   auto* elem = std::lower_bound(
@@ -221,9 +225,8 @@
   return XFA_Element::Unknown;
 }
 
-WideString XFA_AttributeToName(XFA_Attribute attr) {
-  return WideString::FromASCII(
-      g_AttributeTable[static_cast<size_t>(attr)].name);
+ByteStringView XFA_AttributeToName(XFA_Attribute attr) {
+  return g_AttributeTable[static_cast<size_t>(attr)].name;
 }
 
 Optional<XFA_ATTRIBUTEINFO> XFA_GetAttributeByName(const WideStringView& name) {
diff --git a/xfa/fxfa/parser/xfa_basic_data.h b/xfa/fxfa/parser/xfa_basic_data.h
index 5166ebe..609e8e0 100644
--- a/xfa/fxfa/parser/xfa_basic_data.h
+++ b/xfa/fxfa/parser/xfa_basic_data.h
@@ -14,9 +14,10 @@
 #include "third_party/base/optional.h"
 #include "xfa/fxfa/fxfa_basic.h"
 
+ByteStringView XFA_ElementToName(XFA_Element elem);
 XFA_Element XFA_GetElementByName(const WideString& name);
 
-WideString XFA_AttributeToName(XFA_Attribute attr);
+ByteStringView XFA_AttributeToName(XFA_Attribute attr);
 Optional<XFA_ATTRIBUTEINFO> XFA_GetAttributeByName(const WideStringView& name);
 
 ByteStringView XFA_AttributeValueToName(XFA_AttributeValue item);
diff --git a/xfa/fxfa/parser/xfa_basic_data_unittest.cpp b/xfa/fxfa/parser/xfa_basic_data_unittest.cpp
index 8a9c38b..d0abe64 100644
--- a/xfa/fxfa/parser/xfa_basic_data_unittest.cpp
+++ b/xfa/fxfa/parser/xfa_basic_data_unittest.cpp
@@ -8,18 +8,23 @@
 
 #include "testing/gtest/include/gtest/gtest.h"
 
-namespace {
+TEST(XFABasicDataTest, GetElementByName) {
+  EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L""));
+  EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L"nonesuch"));
+  EXPECT_EQ(XFA_Element::ConnectionSet, XFA_GetElementByName(L"connectionSet"));
+  EXPECT_EQ(XFA_Element::Items, XFA_GetElementByName(L"items"));
 
-void DoElementAttrTestCase(XFA_Element elem,
-                           XFA_Attribute attr,
-                           std::pair<XFA_Element, XFA_Attribute>* so_far) {
-  auto curr = std::make_pair(elem, attr);
-  EXPECT_LT(*so_far, curr) << " for " << static_cast<int>(elem) << ", "
-                           << static_cast<int>(attr);
-  *so_far = curr;
+  // Internal elements are not retrievable by name.
+  EXPECT_EQ(XFA_Element::Unknown, XFA_GetElementByName(L"model"));
 }
 
-}  // namespace
+TEST(XFABasicDataTest, ElementToName) {
+  EXPECT_EQ("conformance", XFA_ElementToName(XFA_Element::Conformance));
+  EXPECT_EQ("tagged", XFA_ElementToName(XFA_Element::Tagged));
+
+  // Internal elements resolve to some sort of name.
+  EXPECT_EQ("******", XFA_ElementToName(XFA_Element::Node));
+}
 
 TEST(XFABasicDataTest, GetAttributeByName) {
   Optional<XFA_ATTRIBUTEINFO> result = XFA_GetAttributeByName(L"");
@@ -41,6 +46,11 @@
   EXPECT_EQ(XFA_Attribute::DecipherOnly, result.value().attribute);
 }
 
+TEST(XFABasicDataTest, AttributeToNamee) {
+  EXPECT_EQ("spaceBelow", XFA_AttributeToName(XFA_Attribute::SpaceBelow));
+  EXPECT_EQ("decipherOnly", XFA_AttributeToName(XFA_Attribute::DecipherOnly));
+}
+
 TEST(XFABasicDataTest, GetAttributeValueByName) {
   Optional<XFA_AttributeValue> result = XFA_GetAttributeValueByName(L"");
   EXPECT_FALSE(result.has_value());
@@ -61,11 +71,7 @@
   EXPECT_EQ(XFA_AttributeValue::Lowered, result.value());
 }
 
-TEST(XFABasicDataText, ElementAttributeOrder) {
-  std::pair<XFA_Element, XFA_Attribute> so_far = {};
-#undef ELEM_ATTR____
-#define ELEM_ATTR____(a, b, c) \
-  DoElementAttrTestCase(XFA_Element::a, XFA_Attribute::b, &so_far);
-#include "xfa/fxfa/parser/element_attributes.inc"
-#undef ELEM_ATTR____
+TEST(XFABasicDataTest, AttributeValueToName) {
+  EXPECT_EQ("rl-tb", XFA_AttributeValueToName(XFA_AttributeValue::Rl_tb));
+  EXPECT_EQ("lowered", XFA_AttributeValueToName(XFA_AttributeValue::Lowered));
 }
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp
index 6d6f9d3..566556e 100644
--- a/xfa/fxfa/parser/xfa_utils.cpp
+++ b/xfa/fxfa/parser/xfa_utils.cpp
@@ -166,7 +166,8 @@
       continue;
     }
     WideString wsAttr;
-    SaveAttribute(pNode, attr, XFA_AttributeToName(attr), bSaveXML, wsAttr);
+    SaveAttribute(pNode, attr, WideString::FromASCII(XFA_AttributeToName(attr)),
+                  bSaveXML, wsAttr);
     wsAttrs += wsAttr;
   }
 
@@ -345,7 +346,8 @@
       continue;
 
     WideString wsAttr;
-    SaveAttribute(pNode, attr, XFA_AttributeToName(attr), false, wsAttr);
+    SaveAttribute(pNode, attr, WideString::FromASCII(XFA_AttributeToName(attr)),
+                  false, wsAttr);
     wsOutput += wsAttr;
   }