Author: tlsa
Date: Thu Dec 22 05:05:48 2011
New Revision: 13324
URL:
http://source.netsurf-browser.org?rev=13324&view=rev
Log:
Make example a smidgen more generic. Free the dom_string created for attribute
acquisition.
Modified:
trunk/libdom/examples/dom-structure-dump.c
Modified: trunk/libdom/examples/dom-structure-dump.c
URL:
http://source.netsurf-browser.org/trunk/libdom/examples/dom-structure-dum...
==============================================================================
--- trunk/libdom/examples/dom-structure-dump.c (original)
+++ trunk/libdom/examples/dom-structure-dump.c Thu Dec 22 05:05:48 2011
@@ -130,16 +130,16 @@
/**
- * Dump class attribute/value for an element node
- *
- * \param node The element node to dump class for
+ * Dump attribute/value for an element node
+ *
+ * \param node The element node to dump attribute details for
* \return true on success, or false on error
*/
-bool dump_dom_element_class(dom_node_internal *node)
+bool dump_dom_element_attribute(dom_node_internal *node, char *attribute)
{
dom_exception exc;
- dom_string *class = NULL;
- dom_string *classvalue = NULL;
+ dom_string *attr = NULL;
+ dom_string *attr_value = NULL;
dom_node_type type;
const char *string;
size_t length;
@@ -152,32 +152,35 @@
}
assert(type == DOM_ELEMENT_NODE);
- /* Create a dom_string containing "class". */
- exc = dom_string_create((uint8_t *)"class", 5, &class);
+ /* Create a dom_string containing required attribute name. */
+ exc = dom_string_create((uint8_t *)attribute, 5, &attr);
if (exc != DOM_NO_ERR) {
printf(" Exception raised for dom_string_create\n");
return false;
}
/* Get class attribute's value */
- exc = dom_element_get_attribute(node, class, &classvalue);
+ exc = dom_element_get_attribute(node, attr, &attr_value);
if (exc != DOM_NO_ERR) {
printf(" Exception raised for element_get_attribute\n");
return false;
- } else if (classvalue == NULL) {
- /* Element has no class attribute */
+ } else if (attr_value == NULL) {
+ /* Element lacks required attribute */
return true;
}
- /* Get attributes's string data */
- string = dom_string_data(classvalue);
- length = dom_string_byte_length(classvalue);
-
- /* Print class info */
- printf(" class=\"%*s\"", (int)length, string);
-
- /* Finished with the classvalue dom_string */
- dom_string_unref(classvalue);
+ /* Finished with the attr dom_string */
+ dom_string_unref(attr);
+
+ /* Get attribute value's string data */
+ string = dom_string_data(attr_value);
+ length = dom_string_byte_length(attr_value);
+
+ /* Print attribute info */
+ printf(" %s=\"%*s\"", attribute, (int)length, string);
+
+ /* Finished with the attr_value dom_string */
+ dom_string_unref(attr_value);
return true;
}
@@ -236,7 +239,7 @@
dom_string_unref(node_name);
/* Print the element's class, if it has one */
- if (dump_dom_element_class(node) == false) {
+ if (dump_dom_element_attribute(node, "class") == false) {
printf("\n");
return false;
}