Commit graph

335 commits

Author SHA1 Message Date
Niels Dossche
ddcf5d75b5 Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix reference access in dimensions for DOMNodeList and DOMNodeMap
2024-02-26 19:44:52 +01:00
Niels Dossche
c5a63a90fa Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix reference access in dimensions for DOMNodeList and DOMNodeMap
2024-02-26 19:44:39 +01:00
Niels Dossche
b8a1041fd2 Fix reference access in dimensions for DOMNodeList and DOMNodeMap
Closes GH-13511.
2024-02-26 19:42:54 +01:00
Niels Dossche
85217a044a Mark DOMXPath as uncloneable
This never resulted in a working XPath object anyway, as trying to query
or evaluate anything resulted in an "Invalid XPath context" error.
Supporting this is more trouble than it's worth, so just block the clone
operation.
2024-02-23 19:35:38 +01:00
Niels Dossche
9fc7be8c53 Use bools 2024-02-10 14:01:42 +01:00
Niels Dossche
79e31c2392 Don't use heap allocations for registering DOM property handlers 2024-02-10 14:01:42 +01:00
Niels Dossche
f537ed9dfb Cleanup useless stuff in php_dom.c 2024-02-10 14:01:42 +01:00
Niels Dossche
90785dd865
[RFC] Improve callbacks in ext/dom and ext/xsl (#12627) 2024-01-13 00:00:26 +01:00
Tim Düsterhus
2b30f18708
Remove unused includes of php_random.h (#13131)
Before this change php_random.h was listed in 146 different *.dep files for a

    env CC=clang ./configure --without-sqlite3 --without-pdo-sqlite

build, after this change it's only listed in 110 of them, preventing uselessly
recompiling those files when working on ext/random, mostly caused by the include
in ext/standard/basic_functions.h.
2024-01-12 18:35:18 +01:00
Niels Dossche
ec79fc9d9c Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-12870: Creating an xmlns attribute results in a DOMException
2023-12-07 22:51:02 +01:00
Niels Dossche
e658f80501 Fix GH-12870: Creating an xmlns attribute results in a DOMException
There were multiple things here since forever, see the GH thread [1]
for discussion.

There were already many fixes to this function previously, and as a
consequence of one of those fixes this started throwing exceptions for a
correct use-case. It turns out that even when reverting to the previous
behaviour there are still bugs. Just fix all of them while we have the
chance.

[1] https://github.com/php/php-src/issues/12870

Closes GH-12888.
2023-12-07 22:42:32 +01:00
Niels Dossche
9a34b57b28 ext/dom: Use zend_hash_add_new variant when registering classes and properties 2023-11-20 21:09:10 +01:00
Niels Dossche
1492be5286
[RFC] DOM HTML5 parsing and serialization support (#12111) 2023-11-13 20:18:19 +01:00
Niels Dossche
3e33eda39a Fix cloning attribute with namespace disappearing namespace
Closes GH-12547.
2023-10-29 17:22:41 +01:00
Niels Dossche
7f46c87fa1
Cleanup unused variable in php_dom.c (#12463) 2023-10-17 22:46:05 +02:00
Niels Dossche
0b7a0e9f41 Merge branch 'PHP-8.3'
* PHP-8.3:
  Restore old namespace reconciliation behaviour
2023-09-27 22:41:09 +02:00
Niels Dossche
6a2b885155 Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Restore old namespace reconciliation behaviour
2023-09-27 22:40:37 +02:00
Niels Dossche
19886d3af5 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Restore old namespace reconciliation behaviour
2023-09-27 22:33:45 +02:00
Niels Dossche
e127f87114 Restore old namespace reconciliation behaviour
The xmlDOMWrapReconcileNamespaces method we used to fix the namespace
corruption issues in 8.1.21/8.2.8 caused regressions.
Primarily, there is a similar corruption that the xmlReconciliateNs method
used to have in which a namespace is suddenly shifted
(SAML-Toolkits/php-saml#562) and the side-effect of removing redundant
namespaces causes problems when a specific serialization is required.

Closes GH-12308.
2023-09-27 22:32:01 +02:00
Niels Dossche
48443183af
Use zend_result as return for properties in ext/dom (#12113) 2023-09-03 00:42:49 +02:00
Niels Dossche
d46dc5694c Fix various namespace prefix conflict resolution bugs and namespace shift bugs
There are two linked issues:

- Conflicts couldn't be resolved by changing the prefix name.
- Lacking a prefix would shift the namespace as the default namespace,
  causing elements to suddenly become part of the namespace instead of
  the attributes.

The output could still be improved by removing redundant namespace
declarations, but that's another issue. At least the output is
correct now.

Closes GH-11777.
2023-08-15 20:42:42 +02:00
Niels Dossche
bb092ab4c6 Fix #80927: Removing documentElement after creating attribute node: possible use-after-free
Closes GH-11892.
2023-08-12 18:49:12 +02:00
Niels Dossche
d04f48b6ac Implement DOMNode::parentElement and DOMNameSpaceNode::parentElement
ref: https://dom.spec.whatwg.org/#parent-element

Closes GH-11679.
2023-07-17 13:15:31 +02:00
Niels Dossche
d38cc9b9b6 Implement DOMNode::isConnected and DOMNameSpaceNode::isConnected
ref: https://dom.spec.whatwg.org/#dom-node-isconnected

Closes GH-11677.
2023-07-17 13:14:13 +02:00
Niels Dossche
72e2e25066 Implement DOMElement::id
ref: https://dom.spec.whatwg.org/#dom-element-id

Closes GH-11701.
2023-07-14 14:37:11 +02:00
Niels Dossche
b24b3510f9 Implement DOMElement::className
ref: https://dom.spec.whatwg.org/#dom-element-classname

Closes GH-11691.
2023-07-13 19:05:09 +02:00
Niels Dossche
003ebdd039 Fix GH-9628: Implicitly removing nodes from \DOMDocument breaks existing references
Change the way lifetime works in ext/libxml and ext/dom

Previously, a node could be freed even when holding a userland reference
to it. This resulted in exceptions when trying to access that node after
it has been implicitly or explicitly removed. After this patch, a node
will only be freed when the last userland reference disappears.

Fixes GH-9628.
Closes GH-11576.
2023-07-03 21:31:57 +02:00
Niels Dossche
45c93c173c Remove always-false check
There are no prop handlers with a NULL key.
2023-07-03 20:20:05 +02:00
nielsdos
941a7e59d9 Avoid allocation when getting the node content, if possible
Closes GH-11543.
2023-06-27 18:00:44 +02:00
Niels Dossche
21c1fb6a67
Two tiny ext/dom cleanups (#11541)
* Remove obsolete workaround code

This code was for libxml2 <= 2.6.14. PHP requires at least libxml2
2.9.0.

* Use an inline check instead of calling into the library for an empty string check
2023-06-27 17:46:00 +02:00
Niels Dossche
961e57eb60 Fix GH-11500: Namespace reuse in createElementNS() generates wrong output
When you construct a DOM tree containing subtrees which are constructed
top-down, this won't remove the redundant namespaces. That's because the
following conditions hold:
1) The namespace are reused from the doc->oldNs list.
2) Therefore during reconciliation no nsDef field is set, so no redundant
   namespaces are removed by our reconciliation code.

Furthermore, it would only be fixed up automatically if the tree wasn't
added in bottom-up way, or if it had been constructed bottom-up from the
start.

Fix it by setting a flag to remove redundant namespaces in the libxml2
reconciliation call.
Since removing redundant namespaces may have a performance cost, we only do
this after performing a simple check.

Closes GH-11528.
2023-06-26 19:48:40 +02:00
nielsdos
12e4628815 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11455: Segmentation fault with custom object date properties
  Revert "Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM"
2023-06-19 19:45:24 +02:00
nielsdos
de0223113a Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Revert "Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM"
2023-06-19 19:38:30 +02:00
nielsdos
c174ebfce0 Revert "Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM"
This reverts commit 7eb3e9cd17.

Although the fix follows the spec, it causes issues because a lot of old
code assumes the incorrect behaviour PHP had since a long time.
We cannot do this yet, especially not in a stable release.
We revert this for the time being.
See GH-11428.
2023-06-19 19:37:46 +02:00
Niels Dossche
a37ad648b8 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix #80332: Completely broken array access functionality with DOMNamedNodeMap
2023-06-18 15:21:12 +02:00
Niels Dossche
862487e95e Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix #80332: Completely broken array access functionality with DOMNamedNodeMap
2023-06-18 15:02:46 +02:00
Niels Dossche
9f7d88802e Fix #80332: Completely broken array access functionality with DOMNamedNodeMap
The problem is the usage of zval_get_long(). In particular, if the
string is non-numeric the result of zval_get_long() will be 0 without
giving an error or warning. This is misleading for users: users get the
impression that they can use strings to access the map because it
coincidentally works for the first item (which is at index 0). Of
course, this fails with any other index which causes confusion and bugs.

This patch adds proper support for using string offsets while accessing
the map. It does so by detecting if it's a non-numeric string, and then
using the getNamedItem() method instead of item(). I had to split up the
array access implementation code for DOMNodeList and DOMNamedNodeMap
first to be able to do this.

Closes GH-11468.
2023-06-18 14:59:19 +02:00
Niels Dossche
47708765d6 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM
2023-06-17 13:42:10 +02:00
Niels Dossche
bb3e5a8f55 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM
2023-06-17 13:36:44 +02:00
nielsdos
7eb3e9cd17 Fix GH-11404: DOMDocument::savexml and friends ommit xmlns="" declaration for null namespace, creating incorrect xml representation of the DOM
The NULL namespace is only correct when there is no default namespace
override. When there is, we need to manually set it to the empty string
namespace.

Closes GH-11428.
2023-06-17 13:36:00 +02:00
nielsdos
08f6072263 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix bug #55294 and #47530 and #47847: namespace reconciliation issues
2023-06-15 21:53:16 +02:00
nielsdos
923e72615f Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix bug #55294 and #47530 and #47847: namespace reconciliation issues
2023-06-15 21:50:47 +02:00
nielsdos
b30be40b86 Fix bug #55294 and #47530 and #47847: namespace reconciliation issues
We'll use the DOM wrapper version of libxml2 instead of the regular one.
It's conforming to the behaviour we expect of DOM.
Most of this patch is tests.

I based and extended the tests on the code attached with the aforementioned
bug reports. Therefore the credits for the tests:
Co-authored-by: hilse at web dot de
Co-authored-by: robin2008 at altruists dot org
Co-authored-by: sgunderson at bigfoot dot com

We'll also change the searching point of the internal reconciliation to
start at the top of the added tree to avoid redundant work now that the
function is changed.

Closes GH-11454.
2023-06-15 21:50:00 +02:00
nielsdos
18f2f0a0ab Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11433: Unable to set CURLOPT_ACCEPT_ENCODING to NULL
  Fix "invalid state error" with cloned namespace declarations
  Fix lifetime issue with getAttributeNodeNS()
2023-06-13 17:38:04 +02:00
nielsdos
c160693515 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-11433: Unable to set CURLOPT_ACCEPT_ENCODING to NULL
  Fix "invalid state error" with cloned namespace declarations
  Fix lifetime issue with getAttributeNodeNS()
2023-06-13 17:35:16 +02:00
Niels Dossche
10d94aca4c Fix "invalid state error" with cloned namespace declarations
Closes GH-11429.
2023-06-13 17:30:18 +02:00
Niels Dossche
30c5ae4219 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix #70359 and #78577: segfaults with DOMNameSpaceNode
2023-06-09 21:46:05 +02:00
Niels Dossche
2cbb0c0cc0 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix #70359 and #78577: segfaults with DOMNameSpaceNode
2023-06-09 21:41:49 +02:00
nielsdos
f2d673fb18 Fix #70359 and #78577: segfaults with DOMNameSpaceNode
* Fix type confusion and parent reference
* Manually manage the lifetime of the parent
* Add regression tests
* Break out to a helper, and apply the use-after-free fix to xpath

Closes GH-11402.
2023-06-09 21:35:55 +02:00
nielsdos
a38e3c999f Fix #79700: Bad performance with namespaced nodes due to wrong libxml assumption
* Use a prepending strategy instead of appending in dom_set_old_ns()

Looping to the end of the list is wasteful. We can just put the new
nodes at the front of the list. I don't believe we can fully prepend,
because libxml2 may assume that the xml namespace is the first one, so
we'll put the new ones as the second one.

* Reuse namespaces from doc->oldNs if possible in dom_get_ns()

* Add a test for reconciling a reused namespace

* Explain why there can't be a cycle between oldNs and nsDef

Closes GH-11376.

Also fixes #77894.
2023-06-08 19:43:58 +02:00