php - 遍历 DOM 树

标签 php dom traversal

由于大多数(所有?)执行 HTML 清理的 PHP 库(例如 HTML Purifier)都严重依赖于正则表达式,因此我认为尝试编写一个使用 DOMDocument 和相关类的 HTML 清理器将是一个值得尝试的实验。虽然我还处于非常早期的阶段,但该项目到目前为止显示出一些希望。

我的想法围绕一个类展开,该类使用 DOMDocument 遍历提供的标记中的所有节点,将它们与白名单进行比较,并删除不在白名单上的任何内容。 (第一个实现是非常基本的,只是根据节点的类型删除节点,但我希望将来能够变得更复杂并分析节点的属性,链接是否指向不同域中的项目等)。

我的问题是如何遍历 DOM 树?据我了解,DOM* 对象有一个 childNodes 属性,所以我需要递归整个树吗?此外,早期使用 DOMNodeLists 进行的实验表明,您需要非常注意删除内容的顺序,否则您可能会留下项目或触发异常。

如果有人有在 PHP 中操作 DOM 树的经验,我将不胜感激您对这个主题的任何反馈。

编辑:我为我的 HTML 清理类构建了以下方法。它递归地遍历 DOM 树并检查找到的元素是否在白名单中。如果不是,则将其删除。

我遇到的问题是,如果删除一个节点,DOMNodeList 中所有后续节点的索引都会更改。简单地从下往上工作可以避免这个问题。目前它仍然是一种非常基本的方法,但我认为它显示出希望。它的运行速度肯定比 HTMLPurifier 快得多,尽管不可否认 Purifier 做了更多的事情。

/**
 * Recursivly remove elements from the DOM that aren't whitelisted
 * @param DOMNode $elem
 * @return array List of elements removed from the DOM
 * @throws Exception If removal of a node failed than an exception is thrown
 */
private function cleanNodes (DOMNode $elem)
{
    $removed    = array ();
    if (in_array ($elem -> nodeName, $this -> whiteList))
    {
        if ($elem -> hasChildNodes ())
        {
            /*
             * Iterate over the element's children. The reason we go backwards is because
             * going forwards will cause indexes to change when elements get removed
             */
            $children   = $elem -> childNodes;
            $index      = $children -> length;
            while (--$index >= 0)
            {
                $removed = array_merge ($removed, $this -> cleanNodes ($children -> item ($index)));
            }
        }
    }
    else
    {
        // The element is not on the whitelist, so remove it
        if ($elem -> parentNode -> removeChild ($elem))
        {
            $removed [] = $elem;
        }
        else
        {
            throw new Exception ('Failed to remove node from DOM');
        }
    }
    return ($removed);
}

最佳答案

首先,您可以看一下这个自定义的 RecursiveDomIterator:

代码:

class RecursiveDOMIterator implements RecursiveIterator
{
    /**
     * Current Position in DOMNodeList
     * @var Integer
     */
    protected $_position;

    /**
     * The DOMNodeList with all children to iterate over
     * @var DOMNodeList
     */
    protected $_nodeList;

    /**
     * @param DOMNode $domNode
     * @return void
     */
    public function __construct(DOMNode $domNode)
    {
        $this->_position = 0;
        $this->_nodeList = $domNode->childNodes;
    }

    /**
     * Returns the current DOMNode
     * @return DOMNode
     */
    public function current()
    {
        return $this->_nodeList->item($this->_position);
    }

    /**
     * Returns an iterator for the current iterator entry
     * @return RecursiveDOMIterator
     */
    public function getChildren()
    {
        return new self($this->current());
    }

    /**
     * Returns if an iterator can be created for the current entry.
     * @return Boolean
     */
    public function hasChildren()
    {
        return $this->current()->hasChildNodes();
    }

    /**
     * Returns the current position
     * @return Integer
     */
    public function key()
    {
        return $this->_position;
    }

    /**
     * Moves the current position to the next element.
     * @return void
     */
    public function next()
    {
        $this->_position++;
    }

    /**
     * Rewind the Iterator to the first element
     * @return void
     */
    public function rewind()
    {
        $this->_position = 0;
    }

    /**
     * Checks if current position is valid
     * @return Boolean
     */
    public function valid()
    {
        return $this->_position < $this->_nodeList->length;
    }
}

您可以将它与 RecursiveIteratorIterator 结合使用。使用示例在页面上。

但一般来说,使用 XPath 搜索黑名单节点比遍历 DOM 树更容易。还要记住,DOM 已经非常擅长通过自动转义 nodeValues 中的 xml 实体来防止 XSS。

您必须注意的另一件事是,对 DOMDocument 的任何操作都会立即影响您可能从 XPath 查询中获得的任何 DOMNodeList,并且这可能会导致在操作它们时跳过节点。参见 DOMNode replacement with PHP's DOM classes举个例子。

关于php - 遍历 DOM 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6356115/

相关文章:

php - mysql,php,如何获取用户数?

php - 按数组值排序

php - 根据 PHP 中的 2 个条件更新 MongoDB 中的数量

c++ - 检查二维网格的元素是否与另一个元素共享对角线、水平线或垂直线

java - 遍历自定义链表

javascript - 如何禁用 get 方法中的提交按钮?

javascript - 如何根据屏幕大小将事件监听器附加到 DOM

css - 用于构建 DOM/CSS 类引用的软件

javascript - 如何更新动态 URL 的 URL

java - 遍历Map中的传递键