Re: [xml] XHTML Doc serialization and meta element



Daniel Veillard wrote:

 Another way would be to make libxml2 not touch anything if it detects
any http-equiv meta tag, and if it doesn't detect one try to put the
tag serialization in the output buffer, but not in the tree itself.
This would requires some tweaking of the serialization code but should
not be too hard. This would be more satisfactory from an API POV since
serialization would not modify the input tree, and would not disturb
headers if the application is already taking care of it, while still
adding the meta if the application is ignorant about it.
 What do you think ?
Not sure which way this is going to be handled, but I took a crack at doing it when serializing. Attached is the diff of the changes I made (only meant to look at not meant to be commited).
It looks a bit hackish, but couldnt see any other way to do it.
Didn't touch the HTMLTree.c file at this point till its decided if this is to be done during serialization or one of the other possible ways.

Another thing doing it this way is previously, meta element is only added if dumping the document element (html). Now, if the head element were dumped as the top node in xmlNodeDumpOutput, the meta element is still going to be added to the output as there is no way to know then first node being dumped since all that is avaiable is the node and the xmlSaveCtxt (probably not a big deal though).

Rob
Index: xmlsave.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/xmlsave.c,v
retrieving revision 1.25
diff -c -r1.25 xmlsave.c
*** xmlsave.c   8 Aug 2005 14:44:11 -0000       1.25
--- xmlsave.c   24 Aug 2005 22:47:54 -0000
***************
*** 860,871 ****
--- 860,873 ----
        is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
        if (is_xhtml < 0) is_xhtml = 0;
      }
+ /*
      if (is_xhtml) {
        if (encoding != NULL)
            htmlSetMetaEncoding(cur, (const xmlChar *) ctxt->encoding);
        else
            htmlSetMetaEncoding(cur, BAD_CAST "UTF-8");
      }
+ */
  #endif
      if (cur->children != NULL) {
          xmlNodePtr child = cur->children;
***************
*** 1086,1092 ****
   */
  static void
  xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
!     int format;
      xmlNodePtr tmp;
      xmlChar *start, *end;
      xmlOutputBufferPtr buf;
--- 1088,1094 ----
   */
  static void
  xhtmlNodeDumpOutput(xmlSaveCtxtPtr ctxt, xmlNodePtr cur) {
!     int format, addmeta = 0;
      xmlNodePtr tmp;
      xmlChar *start, *end;
      xmlOutputBufferPtr buf;
***************
*** 1210,1227 ****
      if (cur->properties != NULL)
          xhtmlAttrListDumpOutput(ctxt, cur->properties);
  
      if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
        if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
!           (xhtmlIsEmpty(cur) == 1)) {
            /*
             * C.2. Empty Elements
             */
            xmlOutputBufferWrite(buf, 3, " />");
        } else {
            /*
             * C.3. Element Minimization and Empty Element Content
             */
!           xmlOutputBufferWrite(buf, 3, "></");
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
                xmlOutputBufferWrite(buf, 1, ":");
--- 1212,1256 ----
      if (cur->properties != NULL)
          xhtmlAttrListDumpOutput(ctxt, cur->properties);
  
+       if ((cur->type == XML_ELEMENT_NODE) && 
+               (xmlStrcasecmp(cur->name, BAD_CAST"head") == 0) && 
+               (cur->parent != NULL) && 
+               (xmlStrcasecmp(cur->parent->name, BAD_CAST"html") == 0)) {
+               tmp = cur->children;
+               while (tmp != NULL) {
+                       if ((xmlStrcasecmp(tmp->name, BAD_CAST"meta") == 0) &&
+                               (xmlHasProp(tmp, BAD_CAST"http-equiv"))) {
+                               break;
+                       }
+                       tmp = tmp->next;
+               }
+               if (tmp == NULL)
+                       addmeta = 1;
+       }
+ 
      if ((cur->type == XML_ELEMENT_NODE) && (cur->children == NULL)) {
        if (((cur->ns == NULL) || (cur->ns->prefix == NULL)) &&
!           ((xhtmlIsEmpty(cur) == 1) && (addmeta == 0))) {
            /*
             * C.2. Empty Elements
             */
            xmlOutputBufferWrite(buf, 3, " />");
        } else {
+               if (addmeta == 1) {
+                       xmlOutputBufferWrite(buf, 1, ">");
+                       xmlOutputBufferWriteString(buf,
+                               "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
+                       if (ctxt->encoding) {
+                               xmlOutputBufferWriteString(buf, ctxt->encoding);
+                       } else {
+                               xmlOutputBufferWrite(buf, 5, "UTF-8");
+                       }
+                       xmlOutputBufferWrite(buf, 3, "\" /");
+               }
            /*
             * C.3. Element Minimization and Empty Element Content
             */
!                       xmlOutputBufferWrite(buf, 3, "></");
            if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
                xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
                xmlOutputBufferWrite(buf, 1, ":");
***************
*** 1232,1237 ****
--- 1261,1276 ----
        return;
      }
      xmlOutputBufferWrite(buf, 1, ">");
+       if (addmeta == 1) {
+               xmlOutputBufferWriteString(buf,
+                       "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
+               if (ctxt->encoding) {
+                       xmlOutputBufferWriteString(buf, ctxt->encoding);
+               } else {
+                       xmlOutputBufferWrite(buf, 5, "UTF-8");
+               }
+               xmlOutputBufferWrite(buf, 4, "\" />");
+       }
      if ((cur->type != XML_ELEMENT_NODE) && (cur->content != NULL)) {
        xmlOutputBufferWriteEscape(buf, cur->content, ctxt->escape);
      }
***************
*** 1832,1837 ****
--- 1871,1877 ----
          is_xhtml = xmlIsXHTML(dtd->SystemID, dtd->ExternalID);
          if (is_xhtml < 0)
              is_xhtml = 0;
+ /*
          if ((is_xhtml) && (cur->parent == (xmlNodePtr) doc) &&
              (cur->type == XML_ELEMENT_NODE) &&
              (xmlStrEqual(cur->name, BAD_CAST "html"))) {
***************
*** 1841,1846 ****
--- 1881,1887 ----
              else
                  htmlSetMetaEncoding((htmlDocPtr) doc, BAD_CAST "UTF-8");
          }
+ */
      }
  
      if (is_xhtml)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]