Skip to content

Commit ac77f7f

Browse files
committed
Wire up documentURI
1 parent 429f1f9 commit ac77f7f

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

ext/dom/html_document.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,6 @@ PHP_METHOD(DOM_HTMLDocument, createFromFile)
731731
}
732732
}
733733

734-
php_stream_close(stream);
735-
stream = NULL;
736-
737734
if (!dom_parse_decode_encode_finish(&ctx, document, parser, &decoding_encoding_ctx, &tokenizer_error_offset, &tree_error_offset)) {
738735
goto fail_oom;
739736
}
@@ -749,6 +746,7 @@ PHP_METHOD(DOM_HTMLDocument, createFromFile)
749746
if (UNEXPECTED(bridge_status != LEXBOR_LIBXML2_BRIDGE_STATUS_OK)) {
750747
php_libxml_ctx_error(NULL, "%s in %s", dom_lexbor_libxml2_bridge_status_code_to_string(bridge_status), filename);
751748
lxb_html_document_destroy(document);
749+
php_stream_close(stream);
752750
RETURN_FALSE;
753751
}
754752
lxb_html_document_destroy(document);
@@ -761,6 +759,36 @@ PHP_METHOD(DOM_HTMLDocument, createFromFile)
761759
lxml_doc->encoding = xmlStrdup((const xmlChar *) "UTF-8");
762760
}
763761

762+
if (stream->wrapper == &php_plain_files_wrapper) {
763+
// TODO: do the same for XMLDocument?
764+
xmlChar *converted = xmlPathToURI((const xmlChar *) filename);
765+
if (UNEXPECTED(!converted)) {
766+
goto fail_oom;
767+
}
768+
/* Check for "file:/"" instead of "file://" because of libxml2 quirk */
769+
if (strncmp((const char *) converted, "file:/", sizeof("file:/") - 1) != 0) {
770+
xmlChar *buffer = xmlStrdup((const xmlChar *) "file://");
771+
if (UNEXPECTED(!buffer)) {
772+
xmlFree(converted);
773+
goto fail_oom;
774+
}
775+
xmlChar *new_buffer = xmlStrcat(buffer, converted);
776+
if (UNEXPECTED(!new_buffer)) {
777+
xmlFree(buffer);
778+
xmlFree(converted);
779+
goto fail_oom;
780+
}
781+
xmlFree(converted);
782+
lxml_doc->URL = new_buffer;
783+
} else {
784+
lxml_doc->URL = converted;
785+
}
786+
} else {
787+
lxml_doc->URL = xmlStrdup((const xmlChar *) filename);
788+
}
789+
790+
php_stream_close(stream);
791+
764792
dom_object *intern = php_dom_instantiate_object_helper(return_value, dom_html_document_class_entry, (xmlNodePtr) lxml_doc, NULL);
765793
intern->document->is_modern_api_class = true;
766794
return;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
DOM\HTMLDocument::documentURI
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = DOM\HTMLDocument::createFromFile(__DIR__ . "/test foo.html", LIBXML_NOERROR);
9+
var_dump($dom->documentURI);
10+
11+
$memory = fopen("php://memory", "w+");
12+
fwrite($memory, "foobar");
13+
rewind($memory);
14+
$dom = DOM\HTMLDocument::createFromFile("php://memory");
15+
var_dump($dom->documentURI);
16+
fclose($memory);
17+
18+
class DummyWrapper {
19+
public $context;
20+
21+
public function stream_open($path, $mode, $options, &$opened_path) {
22+
return true;
23+
}
24+
25+
public function stream_read($count) {
26+
return "";
27+
}
28+
29+
public function stream_eof() {
30+
return true;
31+
}
32+
33+
public function stream_close() {
34+
return true;
35+
}
36+
}
37+
38+
stream_wrapper_register("dummy", DummyWrapper::class);
39+
40+
$dom = DOM\HTMLDocument::createFromFile("dummy://foo/ bar");
41+
var_dump($dom->documentURI);
42+
43+
?>
44+
--EXPECTF--
45+
string(%d) "file:/%stest%20foo.html"
46+
string(12) "php://memory"
47+
string(16) "dummy://foo/ bar"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

0 commit comments

Comments
 (0)