]> granicus.if.org Git - php/commitdiff
- Implemented FR #39771 (Made DOMDocument::saveHTML accept an optional
authorGustavo André dos Santos Lopes <cataphract@php.net>
Tue, 18 Jan 2011 19:45:38 +0000 (19:45 +0000)
committerGustavo André dos Santos Lopes <cataphract@php.net>
Tue, 18 Jan 2011 19:45:38 +0000 (19:45 +0000)
  DOMNode like DOMDocument::saveXML).

ext/dom/document.c
ext/dom/tests/DOMDocument_saveHTML_variant1.phpt [moved from ext/dom/tests/DOMDocument_saveHTML_error1.phpt with 58% similarity]

index 540e04de68d99f88684eb1de8b12ef2b6a4df069..42598e3e87e75d16d4a0f912d3fd991100adbd87 100644 (file)
@@ -2284,33 +2284,63 @@ Convenience method to output as html
 */
 PHP_FUNCTION(dom_document_save_html)
 {
-       zval *id;
+       zval *id, *nodep = NULL;
        xmlDoc *docp;
-       dom_object *intern;
-       xmlChar *mem;
+       xmlNode *node;
+       xmlBufferPtr buf;
+       dom_object *intern, *nodeobj;
+       xmlChar *mem = NULL;
        int size, format;
        dom_doc_propsptr doc_props;
 
-       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
+       if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
+               "O|O!", &id, dom_document_class_entry, &nodep, dom_node_class_entry)
+               == FAILURE) {
                return;
        }
 
        DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
 
-#if LIBXML_VERSION >= 20623
        doc_props = dom_get_doc_props(intern->document);
        format = doc_props->formatoutput;
-       htmlDocDumpMemoryFormat(docp, &mem, &size, format);
+
+       if (nodep != NULL) {
+               /* Dump contents of Node */
+               DOM_GET_OBJ(node, nodep, xmlNodePtr, nodeobj);
+               if (node->doc != docp) {
+                       php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
+                       RETURN_FALSE;
+               }
+               
+               buf = xmlBufferCreate();
+               if (!buf) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch buffer");
+                       RETURN_FALSE;
+               }
+               
+               xmlNodeDump(buf, docp, node, 0, format);
+               mem = (xmlChar*) xmlBufferContent(buf);
+               if (!mem) {
+                       RETVAL_FALSE;
+               } else {
+                       RETVAL_STRING(mem, 1);
+               }
+               xmlBufferFree(buf);
+       } else {
+#if LIBXML_VERSION >= 20623
+               htmlDocDumpMemoryFormat(docp, &mem, &size, format);
 #else
-       htmlDocDumpMemory(docp, &mem, &size);
+               htmlDocDumpMemory(docp, &mem, &size);
 #endif
-       if (!size) {
+               if (!size) {
+                       RETVAL_FALSE;
+               } else {
+                       RETVAL_STRINGL(mem, size, 1);
+               }
                if (mem)
                        xmlFree(mem);
-               RETURN_FALSE;
        }
-       RETVAL_STRINGL(mem, size, 1);
-       xmlFree(mem);
+
 }
 /* }}} end dom_document_save_html */
 
similarity index 58%
rename from ext/dom/tests/DOMDocument_saveHTML_error1.phpt
rename to ext/dom/tests/DOMDocument_saveHTML_variant1.phpt
index 78718de8e7fbb258b585a4d4d1cfaa9a68f79b7b..d1691138ae17786d3375f1d7adfea99c16b3429e 100644 (file)
@@ -1,11 +1,8 @@
 --TEST--
-DOMDocument::saveHTML() should fail if a parameter is given
---CREDITS--
-Knut Urdalen <knut@php.net>
-#PHPTestFest2009 Norway 2009-06-09 \o/
+DOMDocument::saveHTML() optional parameters 
 --SKIPIF--
 <?php
-require_once('skipif.inc');
+require_once dirname(__FILE__) .'/skipif.inc';
 ?>
 --FILE--
 <?php
@@ -18,7 +15,10 @@ $title = $doc->createElement('title');
 $title = $head->appendChild($title);
 $text = $doc->createTextNode('This is the title');
 $text = $title->appendChild($text);
-echo $doc->saveHTML(true);
+echo $doc->saveHTML(NULL), "\n";
+echo $doc->saveHTML($title), "\n";
 ?>
 --EXPECTF--
-Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in %s on line %d
+<html><head><title>This is the title</title></head></html>
+
+<title>This is the title</title>