Commit Graph

44 Commits

Author SHA1 Message Date
Niels Dossche
eebc528cbf Fix broken cache invalidation with deallocated and reallocated document node
The original caching implementation had an oversight in combination with
the new lifetime management in DOM for 8.3.
The modification counter is stored on the document object itself, but as
that can get deallocated when all references disappear, stale cache data
can be used. Normally this isn't a problem, unless getElementsByTagName is
called not on the document but on a child node. Fix it by moving caching
data into the ref object, which will outlive all nodes from a document
even if the document object disappears.

Closes GH-12338.
2023-10-01 17:06:02 +02:00
Niels Dossche
620b6220c2 Optimize checks for DOMParentNode and DOMChildNode
Because we check the list with dom_sanity_check_node_list_for_insertion()
before dom_is_node_in_list(), then we don't have to check the object
type anymore in dom_is_node_in_list(), because
dom_sanity_check_node_list_for_insertion() will have already done that.

Closes GH-11914.
2023-08-09 21:31:40 +02:00
Niels Dossche
23ba4cde53 Align DOMChildNode parent checks with spec
Closes GH-11905.
2023-08-09 21:24:33 +02:00
Niels Dossche
cb927e0fc0 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix segfault when DOMParentNode::prepend() is called when the child disappears
2023-08-08 20:09:08 +02:00
Niels Dossche
d19e4da125 Fix segfault when DOMParentNode::prepend() is called when the child disappears
Closes GH-11906.
2023-08-08 20:06:39 +02:00
Niels Dossche
df6e8bd4fd Fix viable next sibling search for replaceWith
Closes GH-11888.
2023-08-07 20:23:06 +02:00
Niels Dossche
5d5e6b0675 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix viable next sibling search for replaceWith
2023-08-07 20:22:36 +02:00
Niels Dossche
815b5ad501 Fix viable next sibling search for replaceWith
Closes GH-11888.
2023-08-07 20:19:31 +02:00
Niels Dossche
3ad5029442 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11830: ParentNode methods should perform their checks upfront
  Fix manually calling __construct() on DOM classes
2023-08-07 19:52:04 +02:00
Niels Dossche
dddd309da4 Fix GH-11830: ParentNode methods should perform their checks upfront
Closes GH-11887.
2023-08-07 19:39:05 +02:00
Niels Dossche
e701b2fee7 Make DOMChildNode::remove() run in O(1) performance
This method had some useless logic in it. It checked whether the child
node is a child of its parent, which is always true of course.
But I know where this check comes from, if you follow the spec closely
you'll find that the spec used to have explicit child and parent
arguments for the removal algorithm [1].
That's because that algorithm is written in a generic way, where the
parent and child arguments might not come from the same subtree.
However, in this particular case it *is* always the case that the child
is a child of its parent. The checks weren't needed back then for
DOMChildNode::remove(), and are still not needed today.

[1] e.g. https://web.archive.org/web/20180601092634/https://dom.spec.whatwg.org/#concept-node-remove
2023-08-07 17:32:46 +02:00
Niels Dossche
6560c9bf8e Implement DOMParentNode::replaceChildren()
ref: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
2023-07-14 14:34:29 +02:00
Niels Dossche
62ef7bb27a Merge branch 'PHP-8.2'
* PHP-8.2:
  Handle fragments consisting out of multiple children without a single root correctly
2023-07-13 16:12:49 +02:00
Niels Dossche
5c26258eeb Handle fragments consisting out of multiple children without a single root correctly
Closes GH-11698.
2023-07-13 16:09:04 +02:00
Niels Dossche
4863d93c96 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11630: proc_nice_basic.phpt only works at certain nice levels
  Fix GH-11629: bug77020.phpt tries to send mail
  Fix GH-11625: DOMElement::replaceWith() doesn't replace node with DOMDocumentFragment but just deletes node or causes wrapping <></> depending on libxml2 version
2023-07-10 13:42:44 +02:00
Niels Dossche
15ff830373 Fix GH-11625: DOMElement::replaceWith() doesn't replace node with DOMDocumentFragment but just deletes node or causes wrapping <></> depending on libxml2 version
Depending on the libxml2 version, the behaviour is either to not
render the fragment correctly, or to wrap it inside <></>. Fix it by
unpacking fragments manually. This has the side effect that we need to
move the unlinking check in the replacement function to earlier because
the empty child list is now possible in non-error cases.
Also fixes a mistake in the linked list management.

Closes GH-11627.
2023-07-10 13:29:31 +02:00
Niels Dossche
8904ac7fef Add missing cache invalidation in dom_child_replace_with()
This was forgotten during an earlier merge.
2023-06-18 14:55:31 +02:00
nielsdos
8e0151707c [ci skip] Fix inaccurate comment 2023-06-07 18:49:22 +02:00
Niels Dossche
a720268214
Use uint32_t for the number of nodes (#11371) 2023-06-05 13:57:41 +01: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
23f7002527 Fix bug #81642: DOMChildNode::replaceWith() bug when replacing a node with itself
Closes GH-11363.
2023-06-04 16:19:48 +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
d8102e6ba4 Remove unnecessary tree setting in dom_zvals_to_fragment()
This is already done by xmlNewDocText().

Closes GH-11345.
2023-05-30 17:48:12 +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
b374ec399d Fix DOMElement::append() and DOMElement::prepend() hierarchy checks
We could end up in an invalid hierarchy, resulting in infinite loops and
eventual crashes if we don't check for the DOM hierarchy validity.

Closes GH-11344.
2023-05-30 17:36:26 +02:00
nielsdos
56b1c11b39 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix GH-11288 and GH-11289 and GH-11290 and GH-9142: DOMExceptions and segfaults with replaceWith
2023-05-25 23:06:51 +02:00
nielsdos
cba335d61e Fix GH-11288 and GH-11289 and GH-11290 and GH-9142: DOMExceptions and segfaults with replaceWith
This replaces the implementation of before and after with one following
the spec very strictly, instead of trying to figure out the state we're
in by looking at the pointers. Also relaxes the condition on text node
copying to prevent working on a stale node pointer.

Closes GH-11299.
2023-05-25 23:04:19 +02:00
Niels Dossche
fb5e77b636 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix incorrect error handling in dom_zvals_to_fragment()
2023-04-03 21:32:23 +02:00
Niels Dossche
0579beb842 Fix incorrect error handling in dom_zvals_to_fragment()
Discovered this pre-existing problem while testing GH-10682.
Note: this problem existed *before* that PR.

* Not all paths throw a hierarchy request error
* xmlFreeNode must be used instead of xmlFree for the fragment to also
  free its children.
* Free up nodes that couldn't be added when xmlAddChild fails.

I unified the error handling code that's exactly the same with a goto to
prevent at least some of such problems in the future.

Closes GH-10981.
2023-04-03 21:21:35 +02:00
Niels Dossche
da96aa8868 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix bug #80602: Segfault when using DOMChildNode::before()
2023-03-30 20:56:57 +02:00
NathanFreeman
2d6decc14c Fix bug #80602: Segfault when using DOMChildNode::before()
This furthermore fixes the logic error explained in
https://github.com/php/php-src/pull/8729#issuecomment-1161737132

Closes GH-10682.
2023-03-30 20:49:05 +02:00
Máté Kocsis
7936c8085e
Fix GH-8329 Print true/false instead of bool in error and debug messages (#8385) 2023-01-23 10:52:14 +01:00
KsaR
01b3fc03c3
Update http->https in license (#6945)
1. Update: http://www.php.net/license/3_01.txt to https, as there is anyway server header "Location:" to https.
2. Update few license 3.0 to 3.01 as 3.0 states "php 5.1.1, 4.1.1, and earlier".
3. In some license comments is "at through the world-wide-web" while most is without "at", so deleted.
4. fixed indentation in some files before |
2021-05-06 12:16:35 +02:00
Máté Kocsis
e0aab7413b
Promote DOM invalid state errors during property access
Closes GH-6780
2021-03-16 20:22:32 +01:00
Benjamin Eberlei
665e1f324a Fix bug #80600 DOMChildNode::remove does not work on DOMCharacterData.
Closes GH-6660
2021-02-02 20:26:52 +01:00
Benjamin Eberlei
9f6820f7f1 Fix #79968: Manipulation on unattached DOMChildNode should throw DOMException 2020-08-19 11:54:41 +02:00
George Peter Banyard
62b1d2cb69 Fix [-Wundef] warning in DOM extension 2020-05-16 15:31:15 +02:00
Máté Kocsis
dfd0acf0d7
Generate method entries for ext/dom
Closes GH-5374
2020-04-13 00:13:11 +02:00
Máté Kocsis
305b17e85f
Do not include the same stub multiple times
Closes GH-5322
2020-04-03 14:23:54 +02:00
Máté Kocsis
01b266aac4
Improve error messages of various extensions
Closes GH-5278
2020-03-23 18:59:04 +01:00
Benjamin Eberlei
0d913f9acf Fix another memory leak in dom_zvals_to_fragment. 2020-03-06 00:03:13 +01:00
Nikita Popov
a7a2e9857e Fix memory leak in dom_zvals_to_fragment 2020-03-03 10:36:04 +01:00
Benjamin Eberlei
5acd86df8e [RFC] Implement new DOM Living Standard APIs in ext/dom 2020-02-28 16:13:39 +01:00