Skip to content

Commit 04df776

Browse files
committed
Deduplicate loading code
1 parent fa397e0 commit 04df776

File tree

1 file changed

+36
-74
lines changed

1 file changed

+36
-74
lines changed

ext/dom/document.c

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,51 +1317,24 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, size_t so
13171317
}
13181318
/* }}} */
13191319

1320-
/* {{{ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) */
1321-
static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
1322-
xmlDoc *docp = NULL, *newdoc;
1323-
dom_doc_propsptr doc_prop;
1324-
dom_object *intern;
1325-
char *source;
1326-
size_t source_len;
1327-
int refcount;
1328-
zend_long options = 0;
1329-
1330-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &options) == FAILURE) {
1331-
RETURN_THROWS();
1332-
}
1333-
1334-
if (!source_len) {
1335-
zend_argument_value_error(1, "must not be empty");
1336-
RETURN_THROWS();
1337-
}
1338-
if (ZEND_SIZE_T_INT_OVFL(source_len)) {
1339-
php_error_docref(NULL, E_WARNING, "Input string is too long");
1340-
RETURN_FALSE;
1341-
}
1342-
if (ZEND_LONG_EXCEEDS_INT(options)) {
1343-
php_error_docref(NULL, E_WARNING, "Invalid options");
1344-
RETURN_FALSE;
1345-
}
1346-
1347-
newdoc = dom_document_parser(ZEND_THIS, mode, source, source_len, options);
1348-
1320+
static void dom_finish_loading_document(zval *this, zval *return_value, xmlDocPtr newdoc)
1321+
{
13491322
if (!newdoc)
13501323
RETURN_FALSE;
13511324

1352-
intern = Z_DOMOBJ_P(ZEND_THIS);
1325+
dom_object *intern = Z_DOMOBJ_P(this);
13531326
size_t old_modification_nr = 0;
13541327
if (intern != NULL) {
1355-
docp = (xmlDocPtr) dom_object_get_node(intern);
1356-
doc_prop = NULL;
1328+
xmlDocPtr docp = (xmlDocPtr) dom_object_get_node(intern);
1329+
dom_doc_propsptr doc_prop = NULL;
13571330
if (docp != NULL) {
13581331
const php_libxml_doc_ptr *doc_ptr = docp->_private;
13591332
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
13601333
old_modification_nr = doc_ptr->cache_tag.modification_nr;
13611334
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
13621335
doc_prop = intern->document->doc_props;
13631336
intern->document->doc_props = NULL;
1364-
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1337+
int refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
13651338
if (refcount != 0) {
13661339
docp->_private = NULL;
13671340
}
@@ -1383,6 +1356,34 @@ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
13831356

13841357
RETURN_TRUE;
13851358
}
1359+
1360+
/* {{{ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) */
1361+
static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
1362+
char *source;
1363+
size_t source_len;
1364+
zend_long options = 0;
1365+
1366+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &source, &source_len, &options) == FAILURE) {
1367+
RETURN_THROWS();
1368+
}
1369+
1370+
if (!source_len) {
1371+
zend_argument_value_error(1, "must not be empty");
1372+
RETURN_THROWS();
1373+
}
1374+
if (ZEND_SIZE_T_INT_OVFL(source_len)) {
1375+
php_error_docref(NULL, E_WARNING, "Input string is too long");
1376+
RETURN_FALSE;
1377+
}
1378+
if (ZEND_LONG_EXCEEDS_INT(options)) {
1379+
php_error_docref(NULL, E_WARNING, "Invalid options");
1380+
RETURN_FALSE;
1381+
}
1382+
1383+
xmlDocPtr newdoc = dom_document_parser(ZEND_THIS, mode, source, source_len, options);
1384+
1385+
dom_finish_loading_document(ZEND_THIS, return_value, newdoc);
1386+
}
13861387
/* }}} end dom_parser_document */
13871388

13881389
/* {{{ URL: http://www.w3.org/TR/DOM-Level-3-LS/load-save.html#LS-DocumentLS-load
@@ -1869,12 +1870,8 @@ PHP_METHOD(DOMDocument, relaxNGValidateSource)
18691870

18701871
static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
18711872
{
1872-
xmlDoc *docp = NULL, *newdoc;
1873-
dom_object *intern;
1874-
dom_doc_propsptr doc_prop;
18751873
char *source;
18761874
size_t source_len;
1877-
int refcount;
18781875
zend_long options = 0;
18791876
htmlParserCtxtPtr ctxt;
18801877

@@ -1922,45 +1919,10 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
19221919
htmlCtxtUseOptions(ctxt, (int)options);
19231920
}
19241921
htmlParseDocument(ctxt);
1925-
newdoc = ctxt->myDoc;
1922+
xmlDocPtr newdoc = ctxt->myDoc;
19261923
htmlFreeParserCtxt(ctxt);
19271924

1928-
if (!newdoc)
1929-
RETURN_FALSE;
1930-
1931-
intern = Z_DOMOBJ_P(ZEND_THIS);
1932-
size_t old_modification_nr = 0;
1933-
if (intern != NULL) {
1934-
docp = (xmlDocPtr) dom_object_get_node(intern);
1935-
doc_prop = NULL;
1936-
if (docp != NULL) {
1937-
const php_libxml_doc_ptr *doc_ptr = docp->_private;
1938-
ZEND_ASSERT(doc_ptr != NULL); /* Must exist, we have a document */
1939-
old_modification_nr = doc_ptr->cache_tag.modification_nr;
1940-
php_libxml_decrement_node_ptr((php_libxml_node_object *) intern);
1941-
doc_prop = intern->document->doc_props;
1942-
intern->document->doc_props = NULL;
1943-
refcount = php_libxml_decrement_doc_ref((php_libxml_node_object *)intern);
1944-
if (refcount != 0) {
1945-
docp->_private = NULL;
1946-
}
1947-
}
1948-
intern->document = NULL;
1949-
if (php_libxml_increment_doc_ref((php_libxml_node_object *)intern, newdoc) == -1) {
1950-
RETURN_FALSE;
1951-
}
1952-
intern->document->doc_props = doc_prop;
1953-
}
1954-
1955-
php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)newdoc, (void *)intern);
1956-
/* Since iterators should invalidate, we need to start the modification number from the old counter */
1957-
if (old_modification_nr != 0) {
1958-
php_libxml_doc_ptr* doc_ptr = (php_libxml_doc_ptr*) ((php_libxml_node_object*) intern)->node; /* downcast */
1959-
doc_ptr->cache_tag.modification_nr = old_modification_nr;
1960-
php_libxml_invalidate_node_list_cache(doc_ptr);
1961-
}
1962-
1963-
RETURN_TRUE;
1925+
dom_finish_loading_document(ZEND_THIS, return_value, newdoc);
19641926
}
19651927
/* }}} */
19661928

0 commit comments

Comments
 (0)