Skip to content

Commit b748326

Browse files
committed
Add a Document::encoding setter for compatibility with PHP's DOMDocument
Change-Id: I4036e3f93b5ccb8340c22e3c1e7c6e5685f7539d
1 parent 670a1fa commit b748326

File tree

7 files changed

+53
-0
lines changed

7 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
the weak covariant return type checks in PHP 7.2.
66
* Generate helper functions for attributes with the [Reflect] extended
77
attribute (which reflect an HTML element attribute).
8+
* Add Document::encoding setter, for compatibility with PHP's
9+
DOMDocument. This is marked as [PHPExtension].
810

911
# IDLeDOM 0.3.0 (2021-04-12)
1012
* Use interface (instead of class) for enumerations. This allows

spec/DOM.webidl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ interface Document : Node {
269269
readonly attribute DOMString inputEncoding; // legacy alias of .characterSet
270270
readonly attribute DOMString contentType;
271271

272+
// This exists in PHP's built-in DOMDocument. PHP specs say:
273+
// Encoding of the document, as specified by the XML declaration.
274+
// This attribute is not present in the final DOM Level 3 specification,
275+
// but is the only way of manipulating XML document encoding in this
276+
// implementation.
277+
[PHPExtension] attribute DOMString encoding;
278+
272279
readonly attribute DocumentType? doctype;
273280
readonly attribute Element? documentElement;
274281
HTMLCollection getElementsByTagName(DOMString qualifiedName);

src/Document.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @property string $charset
3737
* @property string $inputEncoding
3838
* @property string $contentType
39+
* @property string $encoding
3940
* @property DocumentType|null $doctype
4041
* @property Element|null $documentElement
4142
* @phan-forbid-undeclared-magic-properties
@@ -83,6 +84,16 @@ public function getInputEncoding() : string;
8384
*/
8485
public function getContentType() : string;
8586

87+
/**
88+
* @return string
89+
*/
90+
public function getEncoding() : string;
91+
92+
/**
93+
* @param string $val
94+
*/
95+
public function setEncoding( string $val ) : void;
96+
8697
/**
8798
* @return DocumentType|null
8899
*/

src/Helper/Document.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function __get( string $name ) {
6666
return $this->getInputEncoding();
6767
case "contentType":
6868
return $this->getContentType();
69+
case "encoding":
70+
return $this->getEncoding();
6971
case "doctype":
7072
return $this->getDoctype();
7173
case "documentElement":
@@ -143,6 +145,8 @@ public function __isset( string $name ) : bool {
143145
return true;
144146
case "contentType":
145147
return true;
148+
case "encoding":
149+
return true;
146150
case "doctype":
147151
return $this->getDoctype() !== null;
148152
case "documentElement":
@@ -167,6 +171,9 @@ public function __set( string $name, $value ) : void {
167171
case "textContent":
168172
$this->setTextContent( $value );
169173
return;
174+
case "encoding":
175+
$this->setEncoding( $value );
176+
return;
170177
default:
171178
break;
172179
}
@@ -240,6 +247,8 @@ public function __unset( string $name ) : void {
240247
break;
241248
case "contentType":
242249
break;
250+
case "encoding":
251+
break;
243252
case "doctype":
244253
break;
245254
case "documentElement":

src/Helper/XMLDocument.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public function __get( string $name ) {
6666
return $this->getInputEncoding();
6767
case "contentType":
6868
return $this->getContentType();
69+
case "encoding":
70+
return $this->getEncoding();
6971
case "doctype":
7072
return $this->getDoctype();
7173
case "documentElement":
@@ -143,6 +145,8 @@ public function __isset( string $name ) : bool {
143145
return true;
144146
case "contentType":
145147
return true;
148+
case "encoding":
149+
return true;
146150
case "doctype":
147151
return $this->getDoctype() !== null;
148152
case "documentElement":
@@ -167,6 +171,9 @@ public function __set( string $name, $value ) : void {
167171
case "textContent":
168172
$this->setTextContent( $value );
169173
return;
174+
case "encoding":
175+
$this->setEncoding( $value );
176+
return;
170177
default:
171178
break;
172179
}
@@ -240,6 +247,8 @@ public function __unset( string $name ) : void {
240247
break;
241248
case "contentType":
242249
break;
250+
case "encoding":
251+
break;
243252
case "doctype":
244253
break;
245254
case "documentElement":

src/Stub/Document.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public function getContentType() : string {
9797
throw self::_unimplemented();
9898
}
9999

100+
/**
101+
* @return string
102+
*/
103+
public function getEncoding() : string {
104+
throw self::_unimplemented();
105+
}
106+
107+
/**
108+
* @param string $val
109+
*/
110+
public function setEncoding( string $val ) : void {
111+
throw self::_unimplemented();
112+
}
113+
100114
/**
101115
* @return DocumentType|null
102116
*/

src/XMLDocument.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @property string $charset
3737
* @property string $inputEncoding
3838
* @property string $contentType
39+
* @property string $encoding
3940
* @property DocumentType|null $doctype
4041
* @property Element|null $documentElement
4142
* @phan-forbid-undeclared-magic-properties

0 commit comments

Comments
 (0)