如何将 class 设置为特定块内的所有文本节点 parents

How to set class to all text node parents inside of specific block

我需要为页面上特定块内的每个文本节点设置 class 到 parent。

这是我正在尝试做的事情:

$pageHTML = '<html><head></head>
<body>

<header>
  <div>
    <nav>Menu</nav>
    <span>Another text</span>
  </div>
</header>

<section>Section</section>
<footer>Footer</footer>
</body>
</html>';

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($pageHTML);
libxml_use_internal_errors(false);

foreach($dom->getElementsByTagName('body')[0]->childNodes as $bodyChild) {

    if($bodyChild->nodeName == 'header') {

        $blockDoc = new DOMDocument();
        $blockDoc->appendChild($blockDoc->importNode($bodyChild, true));
        $xpath = new DOMXpath($blockDoc);

        foreach($xpath->query('//text()') as $textnode) {
            if(preg_match('/\S/', $textnode->nodeValue)) { // exclude non-characters
                 $textnode->parentNode->setAttribute('class','my_class');
            }
        }
    }
}

echo $dom->saveHTML((new \DOMXPath($dom))->query('/')->item(0));

我需要使用 my_class<header> 中获取 <nav><span>,但我没有获取。

据我所知,在将 class 设置为 class 后,我需要 return 将 parents 更改回 DOM,但我该怎么做?

试试这个代码,你必须使用getElementsByTagName而不是通过文本节点检查来通过名称获取节点。

$pageHTML = '<html>
<head></head>
<body>

<header>
  <div>
    <nav>Menu</nav>
    <span>Another text</span>
  </div>
</header>

<section>Section</section>
<footer>Footer</footer>
</body>
</html>';

$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($pageHTML);
libxml_use_internal_errors(false);

$elements = $dom->getElementsByTagName('header');
foreach ($elements as $node) {
    $nav = $node->getElementsByTagName('nav');
    $span = $node->getElementsByTagName('span');

    $nav->item(0)->setAttribute('class', 'my_class');
    $span->item(0)->setAttribute('class', 'my_class');
 }

echo $dom->saveHTML();

好的,我自己找到了答案:

...
$xpath = new DOMXpath($dom);

foreach($dom->getElementsByTagName('body')[0]->childNodes as $bodyChild) {

    if($bodyChild->nodeName == 'header') {

        foreach($xpath->query('.//text()', $bodyChild) as $textnode) {

            if(preg_match('/\S/', $textnode->nodeValue)) { // exclude non-characters
                $textnode->parentNode->setAttribute('class','my_class');
            }
        }
    }
}