Commit graph

312 commits

Author SHA1 Message Date
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
Tim Starling
74910b1403 Factor out dom_remove_all_children()
A few callers remove all children of a node. The way it was done in
node.c was unsafe, because it left nodep->last dangling. It just happens
to not crash if xmlNodeSetContent() is called immediately afterwards.
2023-06-05 20:04:40 +02:00
Niels Dossche
e8fb0edc69 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix bug #77686: Removed elements are still returned by getElementById
  Fix bug #81642: DOMChildNode::replaceWith() bug when replacing a node with itself
  Fix bug #67440: append_node of a DOMDocumentFragment does not reconcile namespaces
2023-06-04 16:34:42 +02:00
Niels Dossche
5b79c53682 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix bug #77686: Removed elements are still returned by getElementById
  Fix bug #81642: DOMChildNode::replaceWith() bug when replacing a node with itself
  Fix bug #67440: append_node of a DOMDocumentFragment does not reconcile namespaces
2023-06-04 16:27:03 +02:00
Niels Dossche
b1d8e240e6 Fix bug #67440: append_node of a DOMDocumentFragment does not reconcile namespaces
The test was amended from the original issue report. For the test:
Co-authored-by: php@deep-freeze.ca

The problem is that the regular dom_reconcile_ns() only works on a
single node. We actually have to reconciliate the whole tree in case a
fragment was added. This also required to move some code around such
that this special case could be handled separately.

Closes GH-11362.
2023-06-04 16:19:04 +02:00
Niels Dossche
c3f0797385
Implement iteration cache, item cache and length cache for node list iteration (#11330)
* Implement iteration cache, item cache and length cache for node list iteration

The current implementation follows the spec requirement that the list
must be "live". This means that changes in the document must be
reflected in the existing node lists without requiring the user to
refetch the node list.
The consequence is that getting any item, or the length of the list,
always starts searching from the root element of the node list. This
results in O(n) time to get any item or the length. If there's a for
loop over the node list, this means the iterations will take O(n²) time
in total. This causes real-world performance issues with potential for
downtime (see GH-11308 and its references for details).

We fix this by introducing a caching strategy. We cache the last
iterated object in the iterator, the last requested item in the node
list, and the last length computation. To invalidate the cache, we
simply count the number of modifications made to the containing
document. If the modification number does not match what the number was
during caching, we know the document has been modified and the cache is
invalid. If this ever overflows, we saturate the modification number and
don't do any caching anymore. Note that we don't check for overflow on
64-bit systems because it would take hundreds of years to overflow.

Fixes GH-11308.
2023-06-03 00:13:14 +02:00
nielsdos
c6655fb719 Implement dom_get_doc_props_read_only()
I was surprised to see that getting the stricterror property showed in
in the Callgrind profile of some tests. Turns out we sometimes allocate
them. Fix this by returning the default in case no changes were made yet.

Closes GH-11345.
2023-05-30 17:48:29 +02:00
nielsdos
1ed68686de Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix DOMElement::append() and DOMElement::prepend() hierarchy checks
  Fix spec compliance error for DOMDocument::getElementsByTagNameNS
  Fix GH-11336: php still tries to unlock the shared memory ZendSem with opcache.file_cache_only=1 but it was never locked
  Fix GH-11338: SplFileInfo empty getBasename with more than one slash
2023-05-30 17:41:54 +02:00
nielsdos
0e7ad40900 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix DOMElement::append() and DOMElement::prepend() hierarchy checks
  Fix spec compliance error for DOMDocument::getElementsByTagNameNS
  Fix GH-11336: php still tries to unlock the shared memory ZendSem with opcache.file_cache_only=1 but it was never locked
  Fix GH-11338: SplFileInfo empty getBasename with more than one slash
2023-05-30 17:38:18 +02:00
Niels Dossche
154c251013 Fix spec compliance error for DOMDocument::getElementsByTagNameNS
Spec link: https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
Spec says we should match any namespace when '*' is provided. This was
however not the case: elements that didn't have a namespace were not
returned. This patch fixes the error by modifying the namespace check.

Closes GH-11343.
2023-05-30 17:35:38 +02:00
Ilija Tovilo
9d5f2f1343
Use new ZSTR_INIT_LITERAL macro (#10879) 2023-03-20 16:19:05 +01:00
Bob Weinand
5a0b68bed7 Revert "Store default object handlers alongside the class entry"
This reverts commit 9e6eab3c13.

Reverted along a01dd9feda.
2022-09-14 11:18:14 +02:00
Bob Weinand
a01dd9feda Revert "Port all internally used classes to use default_object_handlers"
This reverts commit 94ee4f9834.

The commit was a bit too late to be included in PHP 8.2 RC1. Given it's a massive ABI break, we decide to postpone the change to PHP 8.3.
2022-09-14 11:13:23 +02:00
Bob Weinand
94ee4f9834 Port all internally used classes to use default_object_handlers
Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2022-08-31 16:45:27 +02:00
Bob Weinand
9e6eab3c13 Store default object handlers alongside the class entry
Object handlers being separate from class entries is a legacy inherited from PHP 5. Today it has little benefit to keep them separate: in fact, accessing object handlers usually requires not-so-safe hacks.
While it is possible to swap handlers in a custom installed create_object handler, this mostly is tedious, as well as it requires allocating the object handlers struct at runtime, possibly caching it etc..

This allows extensions, which intend to observe other classes to install their own class handlers.
The life cycle of internal classes may now be simply observed by swapping the class handlers in post_startup stage.
The life cycle of userland classes may be observed by iterating over the new classes in zend_compile_file and zend_compile_string and then swapping their handlers.

In general, this would also be a first step in directly tying the object handlers to classes. Especially given that I am not aware of any case where the object handlers would be different between various instances of a given class.

Signed-off-by: Bob Weinand <bobwei9@hotmail.com>
2022-08-31 16:45:27 +02:00
Go Kudo
4d8dd8d258
Implement Random Extension
https://wiki.php.net/rfc/rng_extension
https://wiki.php.net/rfc/random_extension_improvement
2022-07-19 10:27:38 +01:00
Máté Kocsis
debd38f851 Add support for sensitive parameters in stubs 2022-06-04 18:15:05 +02:00
Máté Kocsis
9c446a9354
Declare ext/dom constants in stubs (#8676) 2022-06-01 16:14:08 +02:00
Dmitry Stogov
90b7bde615 Use more compact representation for packed arrays.
- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
2021-11-03 15:18:26 +03:00
Máté Kocsis
25cbd08470
Make a few unuseful DOMEntity properties readonly (#7406) 2021-08-26 10:37:45 +02:00
Patrick Allaert
aff365871a Fixed some spaces used instead of tabs 2021-06-29 11:30:26 +02:00
Nikita Popov
0ce2359233 Remove dom_nnodemap dtor_obj handler
Don't see any obvious reason why this would have to happen in a
dtor_obj handler. If there is a reason, we're lacking test
coverage for it.
2021-06-09 12:31:34 +02:00
Máté Kocsis
7da2151f7a
Declare typed properties in ext/dom
Closes GH-7013
2021-05-26 12:23:43 +02:00
Máté Kocsis
a5e938dc86
Declare dynamic properties in ext/dom
Closes GH-6644
2021-05-18 15:12:35 +02:00