繁体   English   中英

防止 PHP DOMDocument 删除 @click 属性

[英]Prevent PHP DOMDocument from removing @click attributes

我有一个 HTML 代码,其中有一些 JS 库使用的@click@autocomplete:change等属性。

当我使用 DOMDocument 解析 HTML 时,这些属性被删除。

示例代码:

<?php

$content = <<<'EOT'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab" @click="activeType=listingType"></a>
        <input type="text" @autocomplete:change="handleAutocomplete">
    </body>
</html>
EOT;

// creating new document
$doc = new DOMDocument('1.0', 'utf-8');
$doc->recover = true;
$doc->strictErrorChecking = false;

//turning off some errors
libxml_use_internal_errors(true);

// it loads the content without adding enclosing html/body tags and also the doctype declaration
$doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

echo $doc->saveHTML();
?>

Output:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab"></a>
        <input type="text">
    </body>
</html>

如果无法让DOMDocument接受属性名称中的@ ,我们可以在loadHTML()之前将@替换为特殊字符串,并在saveHTML()之后替换回来

<?php

$content = <<<'EOT'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab" @click="activeType=listingType"></a>
        <input type="text" @autocomplete:change="handleAutocomplete">
    </body>
</html>
EOT;

// creating new document
$doc = new DOMDocument('1.0', 'utf-8');
$doc->recover = true;
$doc->strictErrorChecking = false;

//turning off some errors
libxml_use_internal_errors(true);

$content = str_replace('@', 'at------', $content);
// it loads the content without adding enclosing html/body tags and also the doctype declaration
$doc->LoadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$html = $doc->saveHTML();
$html = str_replace('at------', '@', $html);
echo $html;

output:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab" @click="activeType=listingType"></a>
        <input type="text" @autocomplete:change="handleAutocomplete">
    </body>
</html>

扩展 DomDocument class

并将@click替换为at-click并将@autocomplete替换为at-autocomplete

# this is a PHP 8 example 
class MyDomDocument extends DomDocument 
{
    private $replace = [
        '@click'=>'at-click',
        '@autocomplete'=>'at-autocomplete'
    ];

    public function loadHTML(string $content, int $options = 0)
    {
        $content = str_replace(array_keys($this->replace), array_values($this->replace), $content);
        return parent::loadHTML($content, $options);
    }

    #[\ReturnTypeWillChange]
    public function saveHTML(?DOMNode $node = null)
    {
        $content = parent::saveHTML($node);
        $content = str_replace(array_values($this->replace), array_keys($this->replace), $content);
        return $content;
    }
}

例子

$content = <<<'EOT'
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab" @click="activeType=listingType"></a>
        <input type="text" @autocomplete:change="handleAutocomplete">
    </body>
</html>
EOT;

$dom = new MyDomDocument();
$dom->loadHTML($content);

var_dump($dom->getElementsByTagName('a')[0]->getAttribute('at-click'));
var_dump($dom->getElementsByTagName('input')[0]->getAttribute('at-autocomplete:change'));

echo $dom->saveHTML();

Output

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head></head>
    <body>
        <a role="tab" @click="activeType=listingType"></a>
        <input type="text" @autocomplete:change="handleAutocomplete">
    </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM