Patch savehtml-node-argument.patch for DOM XML related Bug #50973
Patch version 2010-08-05 21:36 UTC
Return to Bug #50973 |
Download this patch
Patch Revisions:
Developer: geoffers+phpbugs@gmail.com
Index: document.c
===================================================================
--- document.c (revision 294856)
+++ document.c (working copy)
@@ -156,6 +156,7 @@
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savehtml, 0, 0, 0)
+ ZEND_ARG_OBJ_INFO(0, node, DOMNode, 1)
ZEND_END_ARG_INFO();
ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_document_savehtmlfile, 0, 0, 1)
@@ -2277,31 +2278,54 @@
}
/* }}} end dom_document_save_html_file */
-/* {{{ proto string dom_document_save_html();
+/* {{{ proto string dom_document_save_html([node n]);
Convenience method to output as html
*/
PHP_FUNCTION(dom_document_save_html)
{
- zval *id;
+ zval *id, *nodep = NULL;
xmlDoc *docp;
- dom_object *intern;
+ xmlNode *node;
+ xmlBufferPtr buf;
xmlChar *mem;
- int size;
-
- if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_document_class_entry) == FAILURE) {
+ dom_object *intern, *nodeobj;
+ int size = 0;
+
+ 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);
-
- htmlDocDumpMemory(docp, &mem, &size);
- if (!size) {
- if (mem)
- xmlFree(mem);
- RETURN_FALSE;
+
+ 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;
+ }
+ htmlNodeDump(buf, docp, node);
+ mem = (xmlChar*) xmlBufferContent(buf);
+ if (!mem) {
+ xmlBufferFree(buf);
+ RETURN_FALSE;
+ }
+ RETVAL_STRING(mem, 1);
+ xmlBufferFree(buf);
+ } else {
+ /* Encoding is handled from the encoding property set on the document */
+ htmlDocDumpMemory(docp, &mem, &size);
+ if (!size) {
+ RETURN_FALSE;
+ }
+ RETVAL_STRINGL(mem, size, 1);
+ xmlFree(mem);
}
- RETVAL_STRINGL(mem, size, 1);
- xmlFree(mem);
}
/* }}} end dom_document_save_html */
Index: tests/DOMDocument_saveHTML_node.phpt
===================================================================
--- tests/DOMDocument_saveHTML_node.phpt (revision 0)
+++ tests/DOMDocument_saveHTML_node.phpt (revision 0)
@@ -0,0 +1,23 @@
+--TEST--
+DOMDocument::saveHTML($node) should dump the internal node into a string using HTML formatting
+--CREDITS--
+Geoffrey Sneddon <me@gsnedders.com>
+--SKIPIF--
+<?php
+require_once dirname(__FILE__) .'/skipif.inc';
+?>
+--FILE--
+<?php
+$doc = new DOMDocument('1.0');
+$root = $doc->createElement('html');
+$root = $doc->appendChild($root);
+$head = $doc->createElement('head');
+$head = $root->appendChild($head);
+$title = $doc->createElement('title');
+$title = $head->appendChild($title);
+$text = $doc->createTextNode('This is the title');
+$text = $title->appendChild($text);
+echo $doc->saveHTML($title);
+?>
+--EXPECTF--
+<title>This is the title</title>
Index: tests/DOMDocument_saveHTML_error1.phpt
===================================================================
--- tests/DOMDocument_saveHTML_error1.phpt (revision 294856)
+++ tests/DOMDocument_saveHTML_error1.phpt (working copy)
@@ -1,24 +0,0 @@
---TEST--
-DOMDocument::saveHTML() should fail if a parameter is given
---CREDITS--
-Knut Urdalen <knut@php.net>
-#PHPTestFest2009 Norway 2009-06-09 \o/
---SKIPIF--
-<?php
-require_once('skipif.inc');
-?>
---FILE--
-<?php
-$doc = new DOMDocument('1.0');
-$root = $doc->createElement('html');
-$root = $doc->appendChild($root);
-$head = $doc->createElement('head');
-$head = $root->appendChild($head);
-$title = $doc->createElement('title');
-$title = $head->appendChild($title);
-$text = $doc->createTextNode('This is the title');
-$text = $title->appendChild($text);
-echo $doc->saveHTML(true);
-?>
---EXPECTF--
-Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in %s on line %d
|