防止HTML Tidy弄乱元标记(模式标记)

问题描述 投票:6回答:3

我正面临HTML Tidy的严重问题(最新版本 - https://html-tidy.org)。

简而言之:HTML整理转换这些HTML代码行

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
    <span property="itemListElement" typeof="ListItem">
        <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
            <span property="name">Codes</span>
        </a>
        <meta property="position" content="1">
    </span>
</div>

进入这些代码行 - 请仔细查看META TAGS的位置。

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
    <span property="itemListElement" typeof="ListItem">
        <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
            <span property="name">Codes</span>
        </a>
    </span>
    <meta property="position" content="1">
</div>

这导致了模式验证的一些严重问题。你可以在这里查看代码:https://search.google.com/structured-data/testing-tool/u/0/

由于此问题,客户端(URL:https://techswami.in)痕迹导航在搜索结果中不可见。

我在美化什么?

我的客户希望我让他/她的网站的源代码看起来“干净,可读和整洁”。

所以我使用这些代码行来使它适合他/她。

注意:此代码100%完美地适用于以下WordPress设置。

  • Nginx与FastCGI Cache / MariaDB
  • PHP7
  • Ubuntu 18.04.1
  • 最新的WordPress并与每个缓存插件兼容。

码:

if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
    $tidy = new Tidy();
    $options = array('indent' => true, 'markup' => true, 'indent-spaces' => 2, 'tab-size' => 8, 'wrap' => 180, 'wrap-sections' => true, 'output-html' => true, 'hide-comments' => true, 'tidy-mark' => false);
    $tidy->parseString("$buffer", $options);
    $tidy->cleanRepair();
    $buffer = $tidy;
    return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');

}

我需要你们帮忙什么?

你能否告诉我如何防止HTML Tidy弄乱META TAGS。我需要参数。

谢谢。

php html wordpress html5 htmltidy
3个回答
3
投票

<meta>标签应仅用于父元素:<head><meta charset><meta http-equiv>此外,property元素中没有<meta>属性。

这些很可能是HTML-Tidy清理标记的原因。

来源


2
投票

首先,衷心感谢所有试图帮助我的人。

我找到了解决方案,我的解决方案唯一的问题是它没有修复HTML-Tidy问题。

所以,现在我没有使用HTML-Tody,而是使用它:https://github.com/ivanweiler/beautify-html/blob/master/beautify-html.php

我的新代码是:

if( !is_user_logged_in() || !is_admin() ) {
    function callback($buffer) {
        $html = $buffer;
        $beautify = new Beautify_Html(array(
          'indent_inner_html' => false,
          'indent_char' => " ",
          'indent_size' => 2,
          'wrap_line_length' => 32786,
          'unformatted' => ['code', 'pre'],
          'preserve_newlines' => false,
          'max_preserve_newlines' => 32786,
          'indent_scripts'  => 'normal' // keep|separate|normal
        ));

        $buffer = $beautify->beautify($html);
        return $buffer;
    }
    function buffer_start() { ob_start("callback"); }
    function buffer_end() { if (ob_get_length()) ob_end_flush(); }
    add_action('wp_loaded', 'buffer_start');
    add_action('shutdown', 'buffer_end');
}

现在,与模式标记相关的每个问题都已修复,客户端的网站已经美化了源代码。


0
投票

仅仅为了透视,我尝试实现一个基于以下内容的最小自包含示例:

我最终得到以下代码:

<?php
ob_start();
?>

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
    <div class="wrap">
        <span property="itemListElement" typeof="ListItem">
            <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
                <span property="name">Codes</span>
            </a>
            <meta property="position" content="1">
        </span>
    </div>
</div>

<?php

$buffer = ob_get_clean();
$tidy = new Tidy();
$options = array(
    'indent' => true,
    'markup' => true,
    'indent-spaces' => 2,
    'tab-size' => 8,
    'wrap' => 180,
    'wrap-sections' => true,
    'output-html' => true,
    'hide-comments' => true,
    'tidy-mark' => false
);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();

echo $tidy;
?>

关于Tidy如何重构HTML的输出非常丰富。在这里:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta property="position" content="1">
    <title></title>
  </head>
  <body>
    <div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
      <div class="wrap">
        <span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class=
        "taxonomy category"><span property="name">Codes</span></a> </span>
      </div>
    </div>
  </body>
</html>

元标记并没有消失,相反,它被推到了它应该属于的地方,正如其他评论者所指出的那样。

如果你想让Tidy只处理HTML结构,请添加选项'input-xml'并将其设置为true,如下所示:

$options = array(
    'indent' => true,
    'markup' => true,
    'indent-spaces' => 2,
    'tab-size' => 8,
    'wrap' => 180,
    'wrap-sections' => true,
    'output-html' => true,
    'hide-comments' => true,
    'tidy-mark' => false,
    'input-xml' => true
);

这将输出以下内容:

<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
  <div class="wrap">
    <span property="itemListElement" typeof="ListItem">
      <a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
        <span property="name">Codes</span>
      </a>
      <meta property="position" content="1"></meta>
    </span>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.