Commit graph

94 commits

Author SHA1 Message Date
Niels Dossche
402b1c29b6
Add Dom\Element::$outerHTML getter
Reference: https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-outerhtml-property
2024-10-05 23:24:27 +02:00
Niels Dossche
88393cfaf7
Fix GH-13988: Storing DOMElement consume 4 times more memory in PHP 8.1 than in PHP 8.0
We avoid creating backing storage by using the feature introduced in
f78d5cfcd2.

Closes GH-15593.
2024-08-27 20:14:25 +02:00
Niels Dossche
d9eb3783bd
Remove DOMImplementation::getFeature() (#15233)
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#remove_domimplementationgetfeature_feature_version
2024-08-04 23:30:52 +02:00
Niels Dossche
0aec0faa27
Deprecate DOM_PHP_ERR (#15234)
* Deprecate DOM_PHP_ERR

RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_dom_php_err_constant

* Apply suggestions from code review

Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>

---------

Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>
2024-08-04 22:00:01 +02:00
Niels Dossche
6980eba863
Support templated content
The template element in HTML 5 is special in the sense that it does not
add its contents into the DOM tree, but instead keeps them in a separate
shadow DOM document fragment. Interacting with the DOM tree cannot touch
the elements in the document fragment.

Closes GH-14906.
2024-07-15 11:10:51 +02:00
Niels Dossche
cf914f4184
Implement PHP-specific extensions to Dom (#14754)
See RFC: https://wiki.php.net/rfc/dom_additions_84
2024-07-04 13:50:19 +02:00
Niels Dossche
fc09f4b2bc
Implement Dom\TokenList (#13664)
Part of RFC: https://wiki.php.net/rfc/dom_additions_84

Closes GH-11688.
2024-07-02 21:34:23 +02:00
Niels Dossche
768900b180 Implement Dom $innerHTML property 2024-07-02 11:15:38 -07:00
Niels Dossche
88da914910 Implement CSS selectors 2024-06-29 13:00:26 -07:00
Peter Kokot
c44834d8ad
Trim trailing whitespace (#14721) 2024-06-29 18:41:45 +02:00
Niels Dossche
48c9f1e2c3 Implement Dom\HTMLElement class 2024-06-26 12:17:12 -07:00
Niels Dossche
04af960397 Implement Dom\Document::$title getter 2024-06-26 12:17:12 -07:00
Niels Dossche
287cf91724 Implement Dom\Document::$head 2024-06-26 12:17:12 -07:00
Niels Dossche
a1485df55a Implement Dom\Document::$body getter 2024-06-26 12:17:12 -07:00
Niels Dossche
e4250cec79 Introduce Dom\AdjacentPosition and use it in the insert adjacent methods
See https://wiki.php.net/rfc/dom_additions_84#allowing_php-specific_developer_experience_improvements
2024-06-24 12:36:35 -07:00
Niels Dossche
a068a9a5bb Make XMLDocument::xinclude() return values and error conditions sane
See https://wiki.php.net/rfc/dom_additions_84#api_amendments
2024-06-24 12:36:35 -07:00
Niels Dossche
20fafa7524
Remove XML_GLOBAL_NAMESPACE (#14265)
This constant is only available if it is defined by libxml2, but it is
never defined because the minimum version of libxml2 that we support had
removed XML_GLOBAL_NAMESPACE already.
2024-05-18 16:24:08 +02:00
Niels Dossche
6e7adb3c48
Update ext/dom names after policy change (#14171) 2024-05-09 10:40:53 +02:00
Niels Dossche
47ec32044e
Make documentURI and URL not readonly (#13982)
These aren't actually readonly right now because `@readonly` means
nothing, and the setters are configured in php_dom.c. So no functional
changes here.

DOM spec marks these as readonly, but the problem is that this reduces
usefulness in XML contexts (like WSDL scheme handling). In context of a
browser, for which DOM was designed, this actually makes sense to have
as readonly because it is tied to the origin of the page etc. But PHP is
not a browser. This also wasn't readonly in "old DOM".
2024-04-17 17:35:29 +02:00
Niels Dossche
ac039cf716
Implement HTMLCollection::namedItem() 2024-04-14 14:45:45 +02:00
Alexander M. Turek
ef93086765
DOM stubs: Reference interfaces from the global namespace correctly (#13801) 2024-03-25 12:06:18 +01:00
Niels Dossche
cc0260e014
Change return type of DOM\HTMLDocument::saveHTML() (#13701)
Strict error checking is always true for classes in "new DOM".
This means that we always throw an error when calling
`php_dom_throw_error`, and therefore the false return value is not
actually possible.
Also change the stub to reflect this.
2024-03-13 21:49:40 +01:00
Niels Dossche
14b6c981c3
[RFC] Add a way to opt-in ext/dom spec compliance (#13031)
RFC: https://wiki.php.net/rfc/opt_in_dom_spec_compliance
2024-03-09 16:56:00 +01:00
divinity76
2f9320c00f
DOMXPath::quote(string $str): string (#13456)
Method to quote strings in XPath, similar to PDO::quote() / mysqli::real_escape_string.

Sample usage: $xp->query("//span[contains(text()," . $xp->quote($string) . ")]")

The algorithm is derived from Robert Rossney's research into XPath quoting published at https://stackoverflow.com/a/1352556/1067003
But using an improved implementation I wrote myself, originally for https://github.com/chrome-php/chrome/pull/575
2024-02-22 20:30:21 +01:00
Niels Dossche
90785dd865
[RFC] Improve callbacks in ext/dom and ext/xsl (#12627) 2024-01-13 00:00:26 +01:00
Niels Dossche
d8268f1aba
Change return type of registerNodeClass to true (#12960) 2023-12-16 19:06:56 +01:00
Niels Dossche
9c306470fb
Handle libxml2 OOM more consistently (#11927)
This is a continuation of commit c2a58ab07d, in which several OOM error
handling was converted to throwing an INVALID_STATE_ERR DOMException.
Some places were missed and they still returned false without an
exception, or threw a PHP_ERR DOMException.
Convert all of these to INVALID_STATE_ERR DOMExceptions. This also
reduces confusion of users going through documentation [1].

Unfortunately, not all node creations are checked for a NULL pointer.
Some places therefore will not do anything if an OOM occurs (well,
except crash).
On the one hand it's nice to handle these OOM cases.
On the other hand, this adds some complexity and it's very unlikely to
happen in the real world. But then again, "unlikely" situations have
caused trouble before. Ideally all cases should be checked.

[1] https://github.com/php/doc-en/issues/1741
2023-12-04 23:49:25 +01:00
Máté Kocsis
164995effe
Fix ext/dom object hierarchy
gen_stub.php references classes inside namespaces relatively
2023-11-27 22:09:31 +01:00
Niels Dossche
1492be5286
[RFC] DOM HTML5 parsing and serialization support (#12111) 2023-11-13 20:18:19 +01:00
Niels Dossche
646bcfaf08 Merge branch 'PHP-8.3'
* PHP-8.3:
  Fix GH-8996: DOMNode serialization on PHP ^8.1
  Fix GH-12380: JIT+private array property access inside closure accesses private property in child class
2023-10-09 22:12:44 +02:00
Niels Dossche
58a1103bee Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2:
  Fix GH-8996: DOMNode serialization on PHP ^8.1
  Fix GH-12380: JIT+private array property access inside closure accesses private property in child class
2023-10-09 22:12:05 +02:00
Niels Dossche
5e1058b426 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix GH-8996: DOMNode serialization on PHP ^8.1
  Fix GH-12380: JIT+private array property access inside closure accesses private property in child class
2023-10-09 22:10:54 +02:00
Niels Dossche
24e5e4ec0d Fix GH-8996: DOMNode serialization on PHP ^8.1
PHP 8.1 introduced a seemingly unintentional BC break in ca94d55a19 by
blocking the (un)serialization of DOM objects.
This was done because the serialization never really worked and just
resulted in an empty object, which upon unserialization just resulted in
an object that you can't use.

Users can however implement their own serialization methods, but the
commit made that impossible as the ACC flag gets passed down to the
child class. An approach was tried in #10307 with a new ACC flag to
selectively allow serialization with subclasses if they implement the
right methods. However, that was found to be too ad hoc.

Instead, let's abuse how the __sleep and __wakeup methods work to throw
the exception instead. If the child class implements the __serialize /
__unserialize method, then the throwing methods won't be called.
Similarly, if the child class implements __sleep and __wakeup, then
they're overridden and it doesn't matter that they throw.

For the user, this PR has the exact same behaviour for (sub)classes that
don't implement the serialization methods: an exception will be thrown.
For code that previously implemented subclasses with these methods, this
approach will make that code work again. This approach should be both BC
preserving and unbreak user's code.

Closes GH-12388.

For the test:
Co-authored-by: wazelin <contact@sergeimikhailov.com>
2023-10-09 22:10:05 +02:00
Niels Dossche
f2fede56c8 Deduplicate ParentNode and ChildNode interface implementations using @implementation-alias
The entry points are duplicated: they add bloat and make it easier to forget
to change something. Make maintenance easier by using @implementation-alias.
Also, this has the nice side-effect of slightly reducing the amount of
code and binary size.

Closes GH-12158.
2023-09-10 22:35:58 +02:00
Niels Dossche
880faa39e8
Add DOMNode::compareDocumentPosition() (#12146)
Reference: https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
2023-09-09 01:14:26 +02:00
Niels Dossche
8874384921 Merge branch 'PHP-8.2'
* PHP-8.2:
  Fix json_encode result on DOMDocument
2023-08-01 17:35:10 +02:00
Niels Dossche
82eda28616 Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1:
  Fix json_encode result on DOMDocument
2023-08-01 17:31:55 +02:00
Niels Dossche
6e468bbd3b Fix json_encode result on DOMDocument
According to https://www.php.net/manual/en/class.domdocument:
  When using json_encode() on a DOMDocument object the result will be
  that of encoding an empty object.

But this was broken in 8.1. The output was `{"config": null}`.
That's because the config property is defined with a default value of
NULL, hence it was included. The other properties are not included
because they don't have a default property, and nothing is ever written
to their backing field. Hence, the JSON encoder excludes them.
Similarly, `(array) $doc` would yield the same `config` key in the
array.

Closes GH-11840.
2023-08-01 17:28:51 +02:00
Niels Dossche
ae66a0d142 Corrections to return type of loading DOM documents 2023-07-29 14:58:01 +02:00
Niels Dossche
db5e8ae6cf Implement DOMElement::toggleAttribute()
ref: https://dom.spec.whatwg.org/#dom-element-toggleattribute

Closes GH-11696.
2023-07-17 20:06:49 +02:00
Niels Dossche
a73f38f407
Implement DOMElement::insertAdjacent{Element,Text} (#11700)
* Implement DOMElement::insertAdjacent{Element,Text}

ref: https://dom.spec.whatwg.org/#dom-element-insertadjacentelement
ref: https://dom.spec.whatwg.org/#dom-element-insertadjacenttext

Closes GH-11700.
2023-07-17 17:42:47 +02:00
Niels Dossche
2f318cfb06 Implement DOMNode::isEqualNode()
Since we still support obsoleted nodes in our implementation, this uses
the old spec to match the old nodes; and this uses the new spec for
nodes still defined in the living spec.
When unclear, the behaviour was cross-verified with Firefox.

References:
https://dom.spec.whatwg.org/#dom-node-isequalnode (for everything still in the living spec)
https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/DOM3-Core.html#core-Node3-isEqualNode (for old nodes removed from the living spec)

Closes GH-11690.
2023-07-17 15:29:36 +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
6560c9bf8e Implement DOMParentNode::replaceChildren()
ref: https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
2023-07-14 14:34:29 +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
d17069e191 Implement DOMNode::getRootNode()
ref: https://dom.spec.whatwg.org/#dom-node-getrootnode

Closes GH-11693.
2023-07-13 16:27:28 +02:00
Niels Dossche
10d7e8dc3a Implement DOMElement::getAttributeNames()
ref: https://dom.spec.whatwg.org/#dom-element-getattributenames
2023-07-13 16:18:10 +02:00
Niels Dossche
ea794e9cde Implement DOMNode::contains()
ref: https://dom.spec.whatwg.org/#dom-node-contains
2023-07-12 19:29:07 +02:00