Debian Codegraph @

vendor/wikimedia/idle-dom/WebIDL.md

raw
1 # PHP WebIDL Binding
2
3 This document describes the PHP WebIDL binding used by IDLeDOM.
4
5 Since there does not seem to be an official WebIDL binding for PHP,
6 this documents the implementation choices that IDLeDOM has made to
7 map WebIDL names and types to PHP. Where possible correspondence has
8 been maintained with the PHP `DOMDocument`/etc classes, although the PHP
9 `dom` extension classes appear to be an adhoc binding of the `libxml` C
10 library not a rigorous WebIDL mapping.
11
12 ## Namespace
13
14 All WebIDL interfaces are defined in the namespace
15 `Wikimedia\IDLeDOM\`. For example, the WebIDL interface 'Document'
16 corresponds to the PHP interface `Wikimedia\IDLeDOM\Document`.
17 A particular implementation of the DOM will implement these interfaces
18 in a different namespace; for example the implementation of `Document`
19 might be in a class `Wikimedia\Dodo\Document` which implements
20 `Wikimedia\IDLeDOM\Document`.
21
22 It is expected that users will obtain a `DOMImplementation` or `Document`
23 object by some mechanism, and use that to create all concrete DOM objects.
24 Code which attempts to maintain implementation-independence will
25 avoid direct references to the particular implementation class,
26 and instead refer to the IDLeDOM interface.
27
28 It is expected that users can use type aliasing (ie, PHP
29 [`class_alias`](https://www.php.net/manual/en/function.class-alias.php))
30 to use a compatible DOM implementation—that is, implementations
31 which adhere to this binding specification but don't inherit from
32 the IDLeDOM interfaces.
33
34 TODO: It is hoped that `class_alias` can also be used to be used to
35 provide a migration path for code currently using `DOMDocument`, or
36 to allow code to support either the built-in `DOMDocument` classes
37 or an `IDLeDOM`-compatible implementation.
38
39 ## Names
40
41 To form a PHP identifier from a name in the WebIDL spec, first replace
42 any non-alphanumeric characters with `_`. (This isn't generally needed
43 except when turning enumeration values into identifiers.)
44
45 Since PHP has a number of reserved words in the language, identifiers
46 of PHP constructs corresponding to WebIDL definitions then need to be escaped
47 to avoid conflicts. There are also reserved method names used
48 for PHP interfaces like `ArrayAccess` and `Countable` which could
49 conflict with WebIDL definitions. Finally, WebIDL properties are turned
50 into getter and setter methods which share a namespace with WebIDL
51 operations; this could also produce a conflict.
52
53 Conflicts are resolved as follows:
54
55 > If the WebIDL name conflicts with a PHP reserved word or other
56 > previously-assigned name, then the PHP escaped name is the prefix
57 > `idl_`, followed by the smallest number of underscore (`_`) characters
58 > needed to avoid a conflict (perhaps zero), followed by the WebIDL name.
59 > Otherwise there is no conflict, and the PHP escaped name is simply
60 > the name.
61
62 The PHP reserved words include any identifier starting with two
63 underscores (`__`), which are [reserved in PHP as
64 magical](https://www.php.net/manual/en/language.oop5.magic.php). The
65 keyword `class`, all [predefined
66 constants](https://www.php.net/manual/en/reserved.constants.php), and
67 [other reserved
68 words](https://www.php.net/manual/en/reserved.other-reserved-words.php)
69 (including soft reserved words) are also reserved. (Other than
70 `class` we don't need to reserve the [PHP keywords and compile-time
71 constants](https://www.php.net/manual/en/reserved.keywords.php)
72 because starting in PHP 7.0.0 they are allowed as property, constant,
73 and method names.)
74
75 In WebIDL [`dictionary`] types, the following names are also reserved:
76 `offsetExists`, `offsetGet`, `offsetSet`, and `offsetUnset`. (These
77 are used to implement [`ArrayAccess`].)
78
79 In WebIDL [`callback`] types, the name `invoke` is reserved. (This name
80 is not reserved in [`callback interface`] types.)
81
82 In WebIDL [`callback`], [`callback interface`], and [`dictionary`] types,
83 the name `cast` is reserved.
84
85 In WebIDL [`enumeration`] types, the name `cast` is reserved.
86
87 In WebIDL [`interface`] types, the names `getIterator` and `count` are
88 reserved. (These are used to implement [`IteratorAggregate`] and
89 [`Countable`].)
90
91 If the WebIDL defines an "unnamed" indexed getter, named getter,
92 indexed setter, named setter, indexed deleter, named deleter, or
93 stringifier operation, then the names `item`, `namedItem`, `setItem`,
94 `setNamedItem`, `removeItem`, `removeNamedItem`, or `toString`
95 (respectively) are reserved.
96
97 ### Resolution order
98
99 When determining the PHP escaped name corresponding to an WebIDL name,
100 conflicts are resolved in the following order:
101
102 1. All reserved names, as described above based on WebIDL type, are
103 marked unavailable.
104 2. Names in a [directly
105 inherited](https://heycam.github.io/webidl/#dfn-inherit) interface or
106 dictionary are resolved, recursively, and marked unavailable.
107 3. Names in mixins are resolved recursively, in alphabetic order by mixin
108 name, and marked unavailable.
109 4. Enumeration values are resolved and marked unavailable.
110 5. Constant members are resolved and marked unavailable.
111 6. Attribute getter and setter names, and dictionary field getter and
112 setter names, are resolved and marked unavailable.
113 7. Operation names are resolved and marked unavailable.
114
115 Concretely, given the following WebIDL fragment:
116 ```
117 interface Foo {
118 undefined setBat();
119 }
120 interface Bar extends Foo {
121 const setBat = 0;
122 attribute boolean bat;
123 // conflicts with inherited members shouldn't occur,
124 // but suppose this managed to squeak through and
125 // wasn't intended as an override:
126 undefined setBat();
127 }
128 ```
129 The following names would be assigned and reserved:
130 * `setBat`: the operation on `Foo`
131 * `idl_setBat`: the constant on `Bar`
132 * `getBat`: the getter of attribute `bat` of `Bar`
133 * `idl__setBat`: the setter of attribute `bat` of `Bar`
134 * `idl___setBat`: the operation on `Bar`
135
136 Note in particular the assymmetry in the names of the getter and setter
137 on attribute `bat`. This is unfortunate, but fortunately conflicts such
138 as these are extremely rare.
139
140 ## Types
141
142 This sub-section describes how types in the WebIDL map to types in PHP.
143
144 ### any
145
146 The [`any`] WebIDL type corresponds to a PHP `?mixed` value. No boxing of
147 types is required in PHP.
148
149 ### undefined
150
151 Methods on PHP objects that implement an operation whose WebIDL specifies
152 a [`undefined`] return type must be declared to have a return type of
153 `void`. Other uses of [`undefined`] will be mapped to `null` in PHP.
154
155 ### boolean
156
157 The WebIDL [`boolean`](https://heycam.github.io/webidl/#idl-boolean) type
158 maps exactly to the PHP
159 [`bool`](https://www.php.net/manual/en/language.types.boolean.php)
160 type.
161
162 ### integer types
163
164 The WebIDL `byte`, `octet`, `short`, `unsigned short`, `long` and `unsigned long`
165 types correspond to the PHP
166 [`int`](https://www.php.net/manual/en/language.types.integer.php)
167 type.
168
169 Note that while the WebIDL `unsigned long` type is unsigned, with a range
170 of [0, 4294967295], the PHP `int` type is signed, and on 32-bit
171 platforms may have a range of [−2147483648, 2147483647]. To encode an
172 WebIDL `unsigned long` type in a PHP int, the following steps are
173 followed *on all platforms regardless of the value of `PHP_INT_MAX` on
174 that platform*:
175
176 1. Let `x` be the WebIDL `unsigned long` value to encode.
177 2. If `x` < 2147483648, return a PHP `int` whose value is `x`.
178 3. Otherwise `x` ≥ 2147483648. Return a PHP int whose value is `x − 4294967296`.
179
180 Note that this is the same as casting to a 32-bit int in most languages.
181
182 To decode an WebIDL `unsigned long` value from a PHP `int`, the following
183 steps must be followed:
184
185 1. Let `x` be the PHP int value to decode.
186 2. If `x` ≥ 0, return an WebIDL `unsigned long` whose value is `x`.
187 3. Otherwise `x` < 0. Return an WebIDL `unsigned long` whose value is `x + 4294967296`.
188
189 Note that in PHP this is the same as performing a bit-wise AND of the
190 `int` value with the long constant `0xffffffff`.
191
192 ### `long long`, `unsigned long long`, and `bigint`
193
194 The WebIDL `long long`, `unsigned long long`, and `bigint` types map to a PHP
195 arbitrary-length integer using either the
196 [GMP](https://www.php.net/manual/en/book.gmp.php) or
197 [BCMath](https://www.php.net/manual/en/book.bc.php). Precise details
198 will be determined when there is need.
199
200 ### floating point types
201
202 The WebIDL `float`, `unrestricted float`, `double`, and `unrestricted
203 double` types map to the PHP `float` type.
204
205 ### `sequence<T>`
206
207 The WebIDL `sequence<T>` type corresponds to a PHP array, with elements
208 having the appropriate mapped type for `T`. In PHP type hints, the
209 type will be
210 [`array`](https://www.php.net/manual/en/language.types.array.php), and
211 in [phan](https://github.com/phan/phan/wiki) type annotations, the
212 type will be
213 [`list<T>`](https://github.com/phan/phan/wiki/About-Union-Types),
214 which denotes a list of objects with consecutive integer keys starting
215 from 0.
216
217 A PHP object implementing an interface with an operation declared to
218 return a `sequence<T>` value must not return `null` from the
219 corresponding method. Similarly, a getter method for an WebIDL attribute
220 must not return `null` if the attribute is declared to be of type
221 `sequence<T>`.
222
223 ### `sequence<octet>` and `sequence<unsigned short>`
224
225 As a special case, WebIDL `sequence<octet>` and `sequence<unsigned
226 short>` are represented by PHP
227 [string](https://www.php.net/manual/en/language.types.string.php) in
228 ASCII and UTF-16 encodings, respectively. As with other `sequence`
229 types, `null` is not a valid value of these WebIDL types.
230
231 TODO: No DOM interface yet uses these types.
232
233 ### DOMString, ByteString, USVString, CSSOMString
234
235 PHP uses `string` for all WebIDL string types. Strings are expected
236 to be encoded in UTF-8.
237
238 As described in a note
239 [in the CSS Object Model spec](https://drafts.csswg.org/cssom/#cssomstring)
240 the main difference between DOMString and USVString has to do with how
241 surrogate code units are handled. DOMString allows surrogates, while
242 USVString replaces them with U+FFFD. As rationale the spec states
243 that "well-formed UTF-8 specifically disallows surrogate code points".
244
245 Since we represent strings natively as UTF-8, it is true that
246 surrogates don't appear in our native representation. However, for
247 those interfaces (`CharacterData` and `Text`, for example) for which
248 the spec requires offsets to be in UTF-16 units, it is possible that
249 following the spec will generate unpaired surrogates. For example, if
250 you extract a substring (using UTF-16 offsets) that contains an
251 unmatched surrogate, that lonely surrogate would need to be converted
252 back to UTF-8 before being returned -- but UTF-8 cannot properly
253 represent these characters.
254
255 The behavior of a PHP WebIDL implementation following this
256 specification should be considered undefined in these situations.
257
258 Indeed, for the special case of the `CharacterData` APIs, this spec
259 makes it implementation-defined whether the offsets will be measured
260 in UTF-16 code units or unicode code points (see the [PHP
261 compatibility](#compatibility) section for more details). If
262 measuring in unicode code points, the creation of unpaired surrogates
263 will not occur.
264
265 However, if the implementation elects to use offsets measured in
266 UTF-16 code units, one may use (for example) PHP's built-in
267 `mb_convert_encoding` to convert between UTF-16 and UTF-8. The
268 `mb_convert_encoding` function will drop an unpaired initial surrogate
269 and return an ASCII question mark character for an unpaired trailing
270 surrogate. This may be surprising:
271 ```php
272 # In JavaScript/UTF-16 U+10437 is represented as U+D801 U+DC37
273 $text = $document->createTextNode("\u{10437}");
274 assertEquals(4, strlen($text->data)); // 4 UTF-8 characters
275 assertEquals(2, $text->length); // 2 UTF-16 characters
276 $text2 = $text->splitText(1);
277 assertEquals('', $text->data); // leading unpaired surrogate deleted
278 assertEquals('?', $text2->data); // trailing unpaired surrogate converted to ?
279 ```
280
281 Alternatively, an implementation using UTF-16 offsets might choose
282 to fall back to a [WTF-8] encoding of the unpaired surrogate. String
283 concatenation of unpaired surrogates is likely to lead to further
284 unexpected behavior, however, and PHP does not contain robust support
285 for [WTF-8]. (Another alternative would be [CESU-8], but that alters
286 the encoding of characters outside the Basic Multilingual Plane even
287 where surrogate-splitting is not involved and so would be explicitly
288 in violation of this spec, which mandates UTF-8 for all results not
289 involving unpaired surrogates.)
290
291 Perhaps the safest option for implementations using UTF-16 offsets is
292 to throw an exception if an unpaired surrogate is encountered.
293
294 ### object
295
296 The WebIDL [`object`] type maps to the PHP [`object`
297 type](https://www.php.net/manual/en/language.types.object.php).
298
299 ### symbol
300
301 The WebIDL [`symbol`] type is not supported.
302
303 ### Enumeration types
304
305 WebIDL [enumeration](https://heycam.github.io/webidl/#idl-enums) types
306 correspond to the PHP `string` type. (There is a PHP interface created
307 to hold the enumeration values as class constants, but enumeration
308 values are `string`s, not objects implementing this interface.)
309
310 See the [section on Enumeration interfaces below](#enumeration-interfaces).
311
312 ### Exception types
313
314 Exceptions are represented by the objects implementing the interfaces
315 defined in [the Exceptions section below](#Exceptions). They will
316 also implement the PHP `Throwable` type.
317
318 ## Objects implementing interfaces
319
320 A PHP object that implements an WebIDL [interface] must be of a PHP class
321 that implements the PHP interface that corresponds to the WebIDL
322 interface.
323
324 ## Interfaces
325
326 Every WebIDL interface corresponds to a PHP interface, with the
327 following members:
328
329 ### Constants
330
331 For each [constant](https://www.w3.org/TR/WebIDL-1/#idl-constants) defined
332 on the WebIDL [interface], there is a corresponding
333 PHP class constant:
334
335 * The constant has `public` visibility.
336 * The type is the PHP type that corresponds to the type of the constant,
337 as defined in the [type section above].
338 * The name is the [PHP escaped] [identifier] of the constant.
339 * The value is the PHP value that is equivalent to the constant's WebIDL
340 value, as defined in the [type section above].
341
342 ### Operations
343
344 For each [operation] defined on the WebIDL [interface], there must be a
345 corresponding method declared on the PHP interface with the following
346 properties:
347
348 * The method has `public` visibility.
349 * The return type of the method is the PHP type that corresponds to
350 the operation [return
351 type](https://heycam.github.io/webidl/#dfn-return-type), according
352 to the [type section above].
353 * The name of the method is the [PHP escaped] [identifier] of the
354 operation.
355 * The method has an argument for each argument on the operation, with
356 PHP types corresponding to the type of each WebIDL argument type as
357 defined in the [type section above]. PHP [variable-length argument
358 lists](https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list)
359 will be used where the WebIDL method argument uses the Variadic
360 extended attribute.
361 * Type hints shall be used for arguments and return values, except for
362 types corresponding to DOM interfaces. This provides
363 flexibility in the face of PHP 7's weak covariance/contravariance
364 support and avoids expensive runtime type checks against interface types.
365
366 Interfaces with an [`iterable`] member defined shall extend
367 [`IteratorAggregate`] and define a `getIterator` method which implements
368 the `iterable` member (if unnamed) or dispatches to the `iterable`
369 member (if named).
370
371 XXX: WebIDL interfaces with a [pair
372 iterator](https://heycam.github.io/webidl/#dfn-pair-iterator) are not
373 yet supported.
374
375 Interfaces with a [`stringifier`] member defined will implement the
376 PHP magic method [`__toString()`], which implements the [stringification
377 behavior](https://heycam.github.io/webidl/#dfn-stringification-behavior)
378 of the interface (if unnamed) or dispatches to the [`stringifier`]
379 member (if named).
380
381 Interfaces with either a [*named property
382 getter*](https://heycam.github.io/webidl/#dfn-named-property-getter),
383 [*indexed property
384 getter*](https://heycam.github.io/webidl/#dfn-indexed-property-getter),
385 [*named property
386 setter*](https://heycam.github.io/webidl/#dfn-named-property-setter),
387 [*indexed property
388 getter*](https://heycam.github.io/webidl/#dfn-indexed-property-getter),
389 or [*named property
390 deleter*](https://heycam.github.io/webidl/#dfn-named-property-deleter)
391 shall extend [`ArrayAccess`]. The `ArrayAccess` methods will dispatch
392 to the *named property getter*/*named property setter*/*named property
393 deleter* if the given offset
394 [`is_string`](https://www.php.net/manual/en/function.is-string.php),
395 and will dispatch to the *indexed property getter*/*indexed property
396 setter* if the offset
397 [`is_numeric`](https://www.php.net/manual/en/function.is-numeric.php).
398 Otherwise behavior is undefined, but implementations should throw an
399 exception.
400
401 WebIDL interfaces often contain a `readonly attribute unsigned long
402 length` member. In combination with an *indexed property getter*, the
403 presence of a `length` property is sufficient to make the interface an
404 ["array-like"](https://tc39.es/ecma262/#sec-lengthofarraylike) in
405 JavaScript, usable in a number of array contexts including
406 [for...of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
407 loops. These members have been tagged in our WebIDL with the
408 (nonstandard) `[PHPCountable]` extended attribute. Interfaces
409 containing a `[PHPCountable]` member shall extend the PHP
410 [`Countable`] interface, and `Countable::count()` shall invoke the
411 getter for the `[PHPCountable]` member and return the result.
412
413 Interfaces which have a *indexed property getter* and a
414 `[PHPCountable]` member should be iterable, as an "array-like" would
415 be in JavaScript. Therefore these interfaces shall have a unnamed
416 [`iterable`] member; these additional members added in the PHP binding
417 have been tagged in our WebIDL with the (nonstandard) `[PHPExtension]`
418 extended attribute.
419
420 ### Attributes
421
422 For each [attribute] defined on the WebIDL [interface], there shall be a
423 corresponding *getter method* declared on the PHP interface with the
424 following properties:
425
426 * The method has `public` visibility.
427 * The return type of the method is the PHP type that corresponds to
428 the attribute type, according to the rules in the [type section
429 above].
430 * Type hints on the return value will be used, except for types
431 corresponding to DOM interfaces (as described in the "operations"
432 section above).
433 * The "WebIDL name" of the method is `get`, followed by the first
434 character of the [identifier] of the attribute uppercased (as if
435 the identifier were passed to the
436 [`ucfirst`](https://www.php.net/manual/en/function.ucfirst)
437 function). This name is then [PHP escaped] to resolve conflicts.
438 * The method has no arguments.
439
440 For each [attribute] defined on the WebIDL [interface] that is not [read
441 only], there shall be a corresponding *setter method* declared on the
442 PHP interface with the following properties:
443
444 * The method has `public` visibility.
445 * The return type of the method is `void`.
446 * The "WebIDL name" of the method is `set`, followed by the first
447 character of the [identifier] of the attribute uppercased (as if
448 the identifier were passed to the
449 [`ucfirst`](https://www.php.net/manual/en/function.ucfirst)
450 function). This name is then [PHP escaped] to resolve conflicts.
451 * The method has a single argument whose type is the PHP type that
452 corresponds to the attribute type, according to the rules in the
453 [type section above].
454 * Type hints on the argument will be used, except for types
455 corresponding to DOM interfaces (as described in the "operations"
456 section above).
457
458 For each [attribute] defined on the WebIDL [interface] that is [read
459 only] and is declared with the [`PutForwards`] [extended attribute],
460 there shall be a corresponding setter method declared on the PHP
461 interface with the following properties:
462
463 * The method has `public` visibility.
464 * The return type of the method is `void`.
465 * The "WebIDL name" of the method is `set`, followed by the first
466 character of the [identifier] of the attribute uppercased (as if
467 the identifier were passed to the
468 [`ucfirst`](https://www.php.net/manual/en/function.ucfirst)
469 function). This name is then [PHP escaped] to resolve conflicts.
470 * The method has a single argument whose type is the PHP type that
471 corresponds to the type of the attribute identified by the
472 [`PutForwards`] extended attribute on the interface type that this
473 attribute is declared to be of, according to the rules in the [type
474 section above].
475 * Type hints on the argument will be used, except for types
476 corresponding to DOM interfaces (as described in the "operations"
477 section above).
478
479 #### Reflected attributes
480
481 Certain IDL attributes are defined in the relevant specifications to
482 reflect the value of Element attributes. These do not affect the
483 interface declaration of the IDL attribute. Since implementation
484 details are out-of-scope for this document, we will briefly note that
485 IDLeDOM uses extended attributes named `[Reflect]`, `[ReflectURL]`,
486 `[ReflectEnum]`, `[ReflectDefault]`, `[ReflectMissing]`, and
487 `[ReflectInvalid]` in the IDL specification to guide the automatic
488 creation of getter and setter methods for reflected attributes.
489
490 #### Attribute compatibility
491
492 In every interface with attributes, the PHP [magic
493 methods](https://www.php.net/manual/en/language.oop5.magic.php)
494 [`__get()`], [`__set()`], [`__isset()`], and [`__unset()`] should be defined
495 with the following behavior:
496 * The `__get($name)` method should invoke the
497 PHP getter method for `$name` and return the result.
498 * The `__set($name, $value)` method should invoke the PHP setter method
499 for `$name`, unless the attribute is declared [read only]
500 in which case an appropriate exception should be thrown.
501 * The `__isset($name)` method should invoke `__get($name)` and return `false`
502 if the value returned is `null`, or `true` otherwise.
503 * The `__unset($name)` method should invoke `__set($name, null)`.
504
505 The behavior of [`__get()`] and [`__set()`] if `$name` is not a valid
506 attribute of the interface is undefined. Implementations may elect to
507 throw an exception, or else to permit creation of dynamic properties
508 by storing and fetching values indexed by these names in an auxiliary
509 array.
510
511 Note: the helper traits provided by `WebIDL` choose to invoke a helper
512 method (`_getMissingProp` or `_setMissingProp`) when `$name` is not a
513 valid attribute of the interface. The default implementation of these
514 methods in the helper traits throws an exception, but by providing a
515 specific implementation of these methods other behaviors can be
516 chosen by a DOM implementation.
517
518 XXX: perhaps a 'standard' extension point should be provided as well?
519
520 *The use of these compatibility methods is not recommended in
521 performance-critical code*. They are provided to provide
522 compatibliity with existing code written to use the built-in
523 `DOMDocument` interface and for the convenience of using property
524 syntax, but the invocation of magic methods has a steep performance
525 cost in PHP.
526
527 XXX: ensure that there is no cost associated with simply *defining* the
528 magic methods, even if they are not used.
529
530 ## Dictionaries
531
532 WebIDL [`dictionary`] objects are PHP abstract classes which extend
533 one other PHP class or interface: dictionaries which directly inherit
534 from another dictionary shall extend the PHP abstract class
535 corresponding to that inherited dictionary, and dictionaries with no
536 inherited dictionaries shall extend the PHP [`ArrayAccess`] interface.
537
538 Dictionary classes shall have getter methods for every field of the
539 dictionary:
540 * The method has `public` visibility.
541 * The method is abstract.
542 * The return type of the method is the PHP type that corresponds to
543 the field type, according to the rules in the [type section
544 above].
545 * Type hints on the return value will be used, except for types
546 corresponding to DOM interfaces (as described in the "operations"
547 section above).
548 * The "WebIDL name" of the method is `get`, followed by the first
549 character of the [identifier] of the field name uppercased (as if
550 the identifier were passed to the
551 [`ucfirst`](https://www.php.net/manual/en/function.ucfirst)
552 function). This name is then [PHP escaped] to resolve conflicts.
553 * The method has no arguments.
554
555 In every class corresponding to a dictionary, the PHP [magic
556 methods](https://www.php.net/manual/en/language.oop5.magic.php)
557 [`__get()`] and [`__isset()`] (and optionally [`__set()`] and [`__unset()`])
558 should be defined with the following behavior:
559 * The `__get($name)` method should invoke the
560 PHP getter method for the field `$name` and return the result.
561 * The `__set($name, $value)` method should throw an appropriate exception
562 (if `__set()` is defined).
563 * The `__isset($name)` method should invoke `__get($name)` and return `false`
564 if the value returned is `null`, or `true` otherwise.
565 * The `__unset($name)` method should throw an appropriate exception
566 (if `__unset()` is defined).
567
568 The [`ArrayAccess`] methods of a dictionary should have the following behavior:
569 * The `offsetExists` method should return `true` iff the dictionary has a
570 field with the given WebIDL name (no PHP escaping is done on the name).
571 * The `offsetGet` method should invoke the PHP getter method for the
572 named field and return the result.
573 * The `offsetSet` method should throw an appropriate exception.
574 * The `offsetUnset` method should throw an appropriate exception.
575
576 In addition, classes corresponding to a dictionary should define a static
577 method named `cast` with a single argument:
578 * The `cast` method should return an object extending the class
579 corresponding to the dictionary.
580 * The argument of the `cast` method should be either an associative array
581 or an object extending the class corresponding to the dictionary.
582 * If invoked with an argument extending the class corresponding to
583 the dictionary, the argument should be directly returned.
584 * If invoked with an associative array, an object extending the class
585 corresponding to the dictionary should be returned, where the getter for
586 each field returns the array value associated with the key equal to the
587 field's WebIDL name (not PHP escaped) if there is one, otherwise the getter
588 should return the field's default value.
589
590 ## Callbacks and Callback Interfaces
591
592 WebIDL [`callback`] objects shall be treated as if they were
593 [`callback interface`]s with a single regular operation member named
594 `invoke`.
595
596 A [`callback interface`] shall correspond to a PHP interface with
597 constant and operation members as defined above for [`interface`]
598 types.
599
600 In addition, the PHP interface corresponding to a [`callback
601 interface`] shall define the PHP magic method [`__invoke()`]. This
602 magic method shall invoke the single regular operation of the
603 [`callback interface`] with the same arguments it is given, and return
604 the result. (This ensure that the `callback interface` is a PHP
605 [callable].)
606
607 In addition, classes implementing the interface shall define a static
608 method named `cast` with a single argument:
609 * The `cast` method shall return an object implementing the interface
610 corresponding to the `callback interface`.
611 * The argument of the `cast` method should be either a PHP [callable]
612 or an object implementing the PHP interface corresponding to the
613 `callback interface`.
614 * If invoked with an argument implementing the interface corresponding to
615 the `callback interface`, the argument shall be directly returned.
616 * If invoked with a PHP [callable], an object implementing the interface
617 corresponding to the `callback interface` shall be returned, where
618 invoking the single regular operation of the `callback interface` will
619 invoke the [callable] with the same arguments, and return the result.
620
621 ## Enumeration interfaces
622
623 WebIDL [enumerations](https://heycam.github.io/webidl/#idl-enums)
624 shall correspond to a PHP final class with an string constant for every
625 enumeration value.
626
627 For each enumeration value:
628 * The constant shall have `public` visibility.
629 * The type shall be the PHP `string` type.
630 * The name shall be the [PHP escaped] [identifier] of the enumeration value.
631
632 In addition, the enumeration class will have a static method named
633 `cast` which will throw a `TypeError` exception if its argument is not
634 a string matching a valid enumeration value; otherwise it will return
635 its argument.
636
637 For example, the following WebIDL:
638 ```
639 enum ShadowRootMode { "open", "closed" };
640 ```
641 corresponds to the PHP interface:
642 ```
643 final class ShadowRootMode {
644 public const open = "open";
645 public const closed = "closed";
646
647 public static cast(string $value): string {
648 if ( $value === "open" || $value === "closed" ) {
649 return $value;
650 }
651 throw new class() extends \Exception implements TypeError {
652 };
653 }
654 }
655 ```
656
657 ## Exceptions
658
659 A conforming PHP implementation must have a PHP interface corresponding to
660 every [WebIDL exception] that is supported. For [simple exceptions], the
661 interface name must be the [PHP escaped] [identifier] of the WebIDL
662 simple exception name: `Error`, `EvalError`, `RangeError`, `ReferenceError`,
663 `TypeError`, or `URIError`. These PHP interfaces must have only the `public`
664 modifier. All simple exception interface classes will also extend an empty
665 marker interface named `SimpleException`.
666
667 In addition, the PHP implementation must have a PHP interface named
668 `DOMException` corresponding to the [WebIDL exception] DOMException type.
669 This PHP interface must [correspond in the usual way](#interfaces)
670 to the [WebIDL interface for DOMException given in the WebIDL spec](https://heycam.github.io/webidl/#idl-DOMException). `DOMException` will not extend
671 `SimpleException`.
672
673 Note that because these exceptions are represented as interface types,
674 an implementation may chose to (for example) subclass PHP built-in
675 types such as
676 [`RangeException`](https://www.php.net/manual/en/class.rangeexception)
677 and
678 [`DOMException`](https://www.php.net/manual/en/class.domexception.php)
679 as long as these subclasses also implement the appropriate interface
680 types.
681
682 # Compatibility
683
684 In this section we describe some specific differences between the
685 binding as described above, the names resulting from the `DOMDocument`
686 classes, and the standard JavaScript bindings. In general IDL
687 interfaces, mixins, attributes, or operations which occur in IDLeDOM
688 for PHP interoperability or compatibility with PHP's built-in
689 `DOMDocument` classes have been marked with the extended attribute
690 `[PHPExtension]`.
691
692 PHP contains a writeable
693 [`encoding`](https://www.php.net/manual/en/class.domdocument.php#domdocument.props.encoding)
694 attribute in the definition of the built-in `DOMDocument`. We have
695 copied that non-standard attribute to our IDL for `Document`.
696
697 PHP contains methods on
698 [`DOMDocument`](https://www.php.net/manual/en/class.domdocument.php)
699 named
700 [`loadHTML`](https://www.php.net/manual/en/domdocument.loadhtml.php),
701 [`loadXML`](https://www.php.net/manual/en/domdocument.loadxml.php),
702 [`saveHTML`](https://www.php.net/manual/en/domdocument.savehtml.php), and
703 [`saveXML`](https://www.php.net/manual/en/domdocument.savexml.php)
704 which are roughly equivalent to the standard `DOMParser` and `XMLSerializer`
705 classes (which are otherwise missing from PHP).
706 Similarly,
707 [`DOMDocumentFragment`](https://www.php.net/manual/en/class.domdocumentfragment.php)
708 has a non-standard method named
709 [`appendXML`](https://www.php.net/manual/en/domdocumentfragment.appendxml.php).
710 It is recommended that implementers implement these for compatibility
711 with legacy code. (We don't support calling `DOMDocument::loadHTML()` or
712 `DOMDocument::loadXML()` statically, which generates an `E_STRICT` error
713 in modern PHP.)
714
715 PHP contains methods on
716 [`DOMElement`](https://www.php.net/manual/en/class.domelement.php)
717 named
718 [`setIdAttribute`](https://www.php.net/manual/en/domelement.setidattribute.php),
719 [`setIdAttributeNode`](https://www.php.net/manual/en/domelement.setidattributenode.php),
720 and
721 [`setIdAttributeNS`](https://www.php.net/manual/en/domelement.setidattributens.php).
722 In legacy code using `DOMDocument` calling one of these methods is
723 necessary in order to make
724 [`DOMDocument::getElementById`](https://www.php.net/manual/en/domdocument.getelementbyid.php)
725 work properly, so these methods have been added to our IDL for
726 `Element`. It is recommended that implementers implement them as
727 no-ops for compatibility with legacy code.
728
729 Various methods on [`DOMCharacterData`](https://www.php.net/manual/en/class.domcharacterdata.php)
730 are defined in the spec to take or return offsets in UTF-16 code units.
731 Originally the PHP DOM extension also used UTF-16 code units (and the
732 PHP documentation still claims it does) however in PHP5 the implementation
733 was changed to measure in unicode *code points* instead. This primarily
734 affects the handling of UTF-16 surrogate pairs.
735
736 As discussed in the definition of the [string types](#domstring-bytestring-usvstring-cssomstring)
737 above, the implementation is allowed to choose whether it implements
738 the DOM standard/pre-PHP5 behavior or else the modern PHP5-and-later
739 behavior for these methods. If the DOM standard/pre-PHP5 behavior is
740 implemented, there are additional issues with the handling of unpaired
741 surrogates which can't be represented in UTF-8.
742
743 [PHP escaped]: #names
744 [`dictionary`]: https://heycam.github.io/webidl/#idl-dictionaries
745 [`callback`]: https://heycam.github.io/webidl/#idl-callback-functions
746 [`callback interface`]: https://heycam.github.io/webidl/#idl-callback-interfaces
747 [`enumeration`]: https://heycam.github.io/webidl/#idl-enums
748 [`interface`]: https://heycam.github.io/webidl/#idl-interfaces
749 [`iterable`]: https://heycam.github.io/webidl/#idl-iterable
750 [`stringifier`]: https://heycam.github.io/webidl/#idl-stringifiers
751 [`ArrayAccess`]: https://www.php.net/manual/en/class.arrayaccess.php
752 [`Countable`]: https://www.php.net/manual/en/class.countable.php
753 [`IteratorAggregate`]: https://www.php.net/manual/en/class.iteratoraggregate.php
754 [`any`]: https://heycam.github.io/webidl/#idl-any
755 [`undefined`]: https://heycam.github.io/webidl/#idl-undefined
756 [`object`]: https://heycam.github.io/webidl/#idl-object
757 [`symbol`]: https://heycam.github.io/webidl/#idl-symbol
758 [type section above]: #types
759 [operation]: https://www.w3.org/TR/WebIDL-1/#idl-operations
760 [identifier]: https://www.w3.org/TR/WebIDL-1/#idl-names
761 [interface]: https://www.w3.org/TR/WebIDL-1#idl-interfaces
762 [attribute]: https://www.w3.org/TR/WebIDL-1/#idl-attributes
763 [WebIDL exception]: https://www.w3.org/TR/WebIDL-1/#idl-exceptions
764 [simple exceptions]: https://heycam.github.io/webidl/#dfn-simple-exception
765 [read only]: https://www.w3.org/TR/WebIDL-1/#dfn-read-only
766 [extended attribute]: https://www.w3.org/TR/WebIDL-1/#dfn-extended-attribute
767 [`PutForwards`]: https://www.w3.org/TR/WebIDL-1/#PutForwards
768 [constant]: https://www.w3.org/TR/WebIDL-1/#dfn-constant
769 [exception member]: https://www.w3.org/TR/WebIDL-1/#exception-member
770 [callable]: https://www.php.net/manual/en/language.types.callable.php
771 [`__get()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.get
772 [`__set()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.set
773 [`__isset()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.isset
774 [`__unset()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.unset
775 [`__invoke()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.invoke
776 [`__toString()`]: https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
777 [WTF-8]: https://en.wikipedia.org/wiki/UTF-8#WTF-8
778 [CESU-8]: https://en.wikipedia.org/wiki/UTF-8#CESU-8