Skip to content

Commit e4250ce

Browse files
committed
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
1 parent a068a9a commit e4250ce

File tree

7 files changed

+140
-25
lines changed

7 files changed

+140
-25
lines changed

ext/dom/dom_ce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ extern PHP_DOM_EXPORT zend_class_entry *dom_xpath_class_entry;
6666
extern PHP_DOM_EXPORT zend_class_entry *dom_modern_xpath_class_entry;
6767
#endif
6868
extern PHP_DOM_EXPORT zend_class_entry *dom_namespace_node_class_entry;
69+
extern PHP_DOM_EXPORT zend_class_entry *dom_adjacent_position_class_entry;
6970

7071
#endif /* DOM_CE_H */

ext/dom/element.c

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "php.h"
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
24+
#include "zend_enum.h"
2425
#include "php_dom.h"
2526
#include "namespace_compat.h"
2627
#include "internal_helpers.h"
@@ -1573,17 +1574,12 @@ static xmlNodePtr dom_insert_adjacent(const zend_string *where, xmlNodePtr thisp
15731574
/* {{{ URL: https://dom.spec.whatwg.org/#dom-element-insertadjacentelement
15741575
Since:
15751576
*/
1576-
static void dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *element_ce)
1577+
static void dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS, const zend_string *where, zval *element_zval)
15771578
{
1578-
zend_string *where;
1579-
zval *element_zval, *id;
1579+
zval *id;
15801580
xmlNodePtr thisp, otherp;
15811581
dom_object *this_intern, *other_intern;
15821582

1583-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SO", &where, &element_zval, element_ce) != SUCCESS) {
1584-
RETURN_THROWS();
1585-
}
1586-
15871583
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
15881584
DOM_GET_OBJ(otherp, element_zval, xmlNodePtr, other_intern);
15891585

@@ -1599,29 +1595,39 @@ static void dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAMETERS, ze
15991595

16001596
PHP_METHOD(DOMElement, insertAdjacentElement)
16011597
{
1602-
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_element_class_entry);
1598+
zend_string *where;
1599+
zval *element_zval;
1600+
1601+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SO", &where, &element_zval, dom_element_class_entry) != SUCCESS) {
1602+
RETURN_THROWS();
1603+
}
1604+
1605+
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, element_zval);
16031606
}
16041607

16051608
PHP_METHOD(Dom_Element, insertAdjacentElement)
16061609
{
1607-
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, dom_modern_element_class_entry);
1610+
zval *element_zval, *where_zv;
1611+
1612+
ZEND_PARSE_PARAMETERS_START(2, 2)
1613+
Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1614+
Z_PARAM_OBJECT_OF_CLASS(element_zval, dom_modern_element_class_entry)
1615+
ZEND_PARSE_PARAMETERS_END();
1616+
1617+
const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
1618+
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, element_zval);
16081619
}
16091620
/* }}} end DOMElement::insertAdjacentElement */
16101621

16111622
/* {{{ URL: https://dom.spec.whatwg.org/#dom-element-insertadjacenttext
16121623
Since:
16131624
*/
1614-
PHP_METHOD(DOMElement, insertAdjacentText)
1625+
static void dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAMETERS, const zend_string *where, const zend_string *data)
16151626
{
1616-
zend_string *where, *data;
16171627
dom_object *this_intern;
16181628
zval *id;
16191629
xmlNodePtr thisp;
16201630

1621-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &where, &data) == FAILURE) {
1622-
RETURN_THROWS();
1623-
}
1624-
16251631
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
16261632

16271633
if (UNEXPECTED(ZEND_SIZE_T_INT_OVFL(ZSTR_LEN(data)))) {
@@ -1635,6 +1641,31 @@ PHP_METHOD(DOMElement, insertAdjacentText)
16351641
xmlFreeNode(otherp);
16361642
}
16371643
}
1644+
1645+
PHP_METHOD(DOMElement, insertAdjacentText)
1646+
{
1647+
zend_string *where, *data;
1648+
1649+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &where, &data) == FAILURE) {
1650+
RETURN_THROWS();
1651+
}
1652+
1653+
dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, data);
1654+
}
1655+
1656+
PHP_METHOD(Dom_Element, insertAdjacentText)
1657+
{
1658+
zval *where_zv;
1659+
zend_string *data;
1660+
1661+
ZEND_PARSE_PARAMETERS_START(2, 2)
1662+
Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1663+
Z_PARAM_STR(data)
1664+
ZEND_PARSE_PARAMETERS_END();
1665+
1666+
const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
1667+
dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, data);
1668+
}
16381669
/* }}} end DOMElement::insertAdjacentText */
16391670

16401671
/* {{{ URL: https://dom.spec.whatwg.org/#dom-element-toggleattribute

ext/dom/php_dom.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "php.h"
2424
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
25+
#include "zend_enum.h"
2526
#include "php_dom.h"
2627
#include "nodelist.h"
2728
#include "html_collection.h"
@@ -84,6 +85,7 @@ PHP_DOM_EXPORT zend_class_entry *dom_xpath_class_entry;
8485
PHP_DOM_EXPORT zend_class_entry *dom_modern_xpath_class_entry;
8586
#endif
8687
PHP_DOM_EXPORT zend_class_entry *dom_namespace_node_class_entry;
88+
PHP_DOM_EXPORT zend_class_entry *dom_adjacent_position_class_entry;
8789
/* }}} */
8890

8991
static zend_object_handlers dom_object_handlers;
@@ -729,6 +731,8 @@ PHP_MINIT_FUNCTION(dom)
729731

730732
zend_hash_init(&classes, 0, NULL, NULL, true);
731733

734+
dom_adjacent_position_class_entry = register_class_Dom_AdjacentPosition();
735+
732736
dom_domexception_class_entry = register_class_DOMException(zend_ce_exception);
733737

734738
dom_parentnode_class_entry = register_class_DOMParentNode();

ext/dom/php_dom.stub.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,14 @@ public function count(): int {}
12801280
public function getIterator(): \Iterator {}
12811281
}
12821282

1283+
enum AdjacentPosition : string
1284+
{
1285+
case BeforeBegin = "beforebegin";
1286+
case AfterBegin = "afterbegin";
1287+
case BeforeEnd = "beforeend";
1288+
case AfterEnd = "afterend";
1289+
}
1290+
12831291
class Element extends Node implements ParentNode, ChildNode
12841292
{
12851293
/** @readonly */
@@ -1330,9 +1338,8 @@ public function removeAttributeNode(Attr $attr) : Attr {}
13301338
public function getElementsByTagName(string $qualifiedName): HTMLCollection {}
13311339
public function getElementsByTagNameNS(?string $namespace, string $localName): HTMLCollection {}
13321340

1333-
public function insertAdjacentElement(string $where, Element $element): ?Element {}
1334-
/** @implementation-alias DOMElement::insertAdjacentText */
1335-
public function insertAdjacentText(string $where, string $data): void {}
1341+
public function insertAdjacentElement(AdjacentPosition $where, Element $element): ?Element {}
1342+
public function insertAdjacentText(AdjacentPosition $where, string $data): void {}
13361343

13371344
/** @readonly */
13381345
public ?Element $firstElementChild;

ext/dom/php_dom_arginfo.h

Lines changed: 39 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Element::insertAdjacentElement()
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = Dom\XMLDocument::createFromString('<?xml version="1.0"?><container><p>foo</p></container>');
9+
$container = $dom->documentElement;
10+
$p = $container->firstElementChild;
11+
12+
var_dump($p->insertAdjacentElement(Dom\AdjacentPosition::BeforeBegin, $dom->createElement('A'))->tagName);
13+
echo $dom->saveXML(), "\n";
14+
15+
var_dump($p->insertAdjacentElement(Dom\AdjacentPosition::AfterBegin, $dom->createElement('B'))->tagName);
16+
echo $dom->saveXML(), "\n";
17+
18+
var_dump($p->insertAdjacentElement(Dom\AdjacentPosition::BeforeEnd, $dom->createElement('C'))->tagName);
19+
echo $dom->saveXML(), "\n";
20+
21+
var_dump($p->insertAdjacentElement(Dom\AdjacentPosition::AfterEnd, $dom->createElement('D'))->tagName);
22+
echo $dom->saveXML(), "\n";
23+
24+
?>
25+
--EXPECT--
26+
string(1) "A"
27+
<?xml version="1.0" encoding="UTF-8"?>
28+
<container><A/><p>foo</p></container>
29+
string(1) "B"
30+
<?xml version="1.0" encoding="UTF-8"?>
31+
<container><A/><p><B/>foo</p></container>
32+
string(1) "C"
33+
<?xml version="1.0" encoding="UTF-8"?>
34+
<container><A/><p><B/>foo<C/></p></container>
35+
string(1) "D"
36+
<?xml version="1.0" encoding="UTF-8"?>
37+
<container><A/><p><B/>foo<C/></p><D/></container>

ext/dom/tests/modern/spec/Element_insertAdjacentText.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ dom
88
$dom = Dom\HTMLDocument::createEmpty();
99
$foo = $dom->appendChild($dom->createElement("foo"));
1010
try {
11-
$foo->insertAdjacentText("beforebegin", "bar");
11+
$foo->insertAdjacentText(Dom\AdjacentPosition::BeforeBegin, "bar");
1212
} catch (DOMException $e) {
1313
echo $e->getMessage(), "\n";
1414
}
1515

16-
$foo->insertAdjacentText("afterbegin", "bar");
17-
$foo->insertAdjacentText("beforeend", "baz");
16+
$foo->insertAdjacentText(Dom\AdjacentPosition::AfterBegin, "bar");
17+
$foo->insertAdjacentText(Dom\AdjacentPosition::BeforeEnd, "baz");
1818

1919
echo $dom->saveHtml(), "\n";
2020

0 commit comments

Comments
 (0)