mediawiki-extensions-Discus.../includes/NodeFilter.php
Bartosz Dziewoński af68c835bb Update exception handling for new code conventions
Change code to match the documented consensus formed on T321683:
https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Exception_handling

* Do not directly throw Exception, Error or MWException
* Document checked exceptions with @throws
* Do not document unchecked exceptions

For this extension, I think it makes sense to consider DOMException an
unchecked exception too (in addition to the usual LogicException and
RuntimeException).

Depends-On: Id07e301c3f20afa135e5469ee234a27354485652
Depends-On: I869af06896b9757af18488b916211c5a41a8c563
Depends-On: I42d9b7465d1406a22ef1b3f6d8de426c60c90e2c
Change-Id: Ic9d9efd031a87fa5a93143f714f0adb20f0dd956
2023-01-22 18:17:11 +00:00

61 lines
1.5 KiB
PHP

<?php
namespace MediaWiki\Extension\DiscussionTools;
use DOMException;
use Wikimedia\Parsoid\DOM\Node;
/**
* Partial implementation of W3 DOM4 NodeFilter interface.
*
* See also:
* - https://dom.spec.whatwg.org/#interface-nodefilter
*
* Adapted from https://github.com/Krinkle/dom-TreeWalker-polyfill/blob/master/src/TreeWalker-polyfill.js
*/
class NodeFilter {
// Constants for acceptNode()
public const FILTER_ACCEPT = 1;
public const FILTER_REJECT = 2;
public const FILTER_SKIP = 3;
// Constants for whatToShow
public const SHOW_ALL = 0xFFFFFFFF;
public const SHOW_ELEMENT = 0x1;
public const SHOW_ATTRIBUTE = 0x2;
public const SHOW_TEXT = 0x4;
public const SHOW_CDATA_SECTION = 0x8;
public const SHOW_ENTITY_REFERENCE = 0x10;
public const SHOW_ENTITY = 0x20;
public const SHOW_PROCESSING_INSTRUCTION = 0x40;
public const SHOW_COMMENT = 0x80;
public const SHOW_DOCUMENT = 0x100;
public const SHOW_DOCUMENT_TYPE = 0x200;
public const SHOW_DOCUMENT_FRAGMENT = 0x400;
public const SHOW_NOTATION = 0x800;
public $filter;
private $active = false;
/**
* See https://dom.spec.whatwg.org/#dom-nodefilter-acceptnode
*
* @param Node $node
* @return int Constant NodeFilter::FILTER_ACCEPT,
* NodeFilter::FILTER_REJECT or NodeFilter::FILTER_SKIP.
*/
public function acceptNode( $node ) {
if ( $this->active ) {
throw new DOMException( 'INVALID_STATE_ERR' );
}
$this->active = true;
$result = call_user_func( $this->filter, $node );
$this->active = false;
return $result;
}
}