TYPO3 11 内容元素 带有 <style>

问题描述 投票:0回答:1
php typo3 typo3-11.x
1个回答
2
投票

经过更多研究(再次投入数小时),我得出的结论是,唯一的方法是扩展 HTMLSanitizer。

此解决方案使用了以下来源:

https://punkt.de/de/blog/2021/htmlsanitizer-in-typo3.html https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/9.5.x/Important-94484-IntroduceHTMLSanitizer.html https://gist.github.com/ohader/2239dab247e18d23e677fd1b816f4fd5

允许

<style>
<script>
<iframe>
的完整解决方案如下所示。

我仍然无法允许

<font>
采用这种方法!

它是在本地站点包(gpcf_theme)中实现的,并使用了composer。

<style>
<script>
<iframe>
添加到

lib.parseFunc.allowTags = ... style, ..., iframe, script, ...

在站点包的 TypoScript 部分。

style
似乎是标准的,并且已经是此列表的一部分。

本地站点包的路径为:

local_packages/gpcf_theme

带有指向它的符号链接:

public/typo3conf/ext/gpcf_theme -> ../../../local_packages/gpcf_theme

这些是重要的文件更改:

local_packages/gpcf_theme/composer.json:

{
        "name": "oheil/gpcf_theme",
        ...
        "autoload": {
                "psr-4": {
                        "Oheil\\GpcfTheme\\": "Classes/"
                }
        },
        ...
}

local_packages/gpcf_theme/ext_localconf.php:

<?php
defined('TYPO3_MODE') || die();

...
$GLOBALS['TYPO3_CONF_VARS']['SYS']['htmlSanitizer']['default'] = \Oheil\GpcfTheme\MyDefaultBuilder::class;
...

local_packages/gpcf_theme/Classes/MyDefaultBuilder.php

<?php

namespace Oheil\GpcfTheme;

use TYPO3\CMS\Core\Html\DefaultSanitizerBuilder;
use TYPO3\HtmlSanitizer\Behavior;
use TYPO3\HtmlSanitizer\Behavior\Attr;
use TYPO3\HtmlSanitizer\Behavior\Tag;

class MyDefaultBuilder extends \TYPO3\CMS\Core\Html\DefaultSanitizerBuilder
{
    protected function createBehavior(): \TYPO3\HtmlSanitizer\Behavior
    {
        // https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/9.5.x/Important-94484-IntroduceHTMLSanitizer.html

        return parent::createBehavior()
                ->withName('common')
                ->withTags(
                        (new Tag(
                                'style',
                                Tag::ALLOW_CHILDREN + Behavior::ENCODE_INVALID_TAG
                        ))->addAttrs(
                                (new Attr('type')),
                                ...$this->globalAttrs
                        ),
                        (new Tag(
                                'iframe',
                                Tag::ALLOW_CHILDREN
                        ))->addAttrs(
                                ...array_merge(
                                        $this->globalAttrs,
                                        [$this->srcAttr],
                                        $this->createAttrs('scrolling', 'marginwidth', 'marginheight', 'frameborder', 'vspace', 'hspace', 'height', 'width')
                                )
                        ),
                        (new Tag(
                                'script',
                                Tag::ALLOW_CHILDREN
                        ))->addAttrs(
                                ...array_merge(
                                        $this->globalAttrs,
                                        [$this->srcAttr]
                                )
                        ),
                        // more tags...
        );
    }
}

在这些更改之后,当然您需要清除 TYPO3 缓存并且(此处不确定)您需要执行以下操作:

composer remove "oheil/gpcf_theme"
composer require "oheil/gpcf_theme"

以上内容现在有效,但我仍然很高兴任何专家能够就问题所在或可以做得更好(也许更容易)提供更多见解?

为什么这对

<font>
不起作用? 示例:

<font class="font713099">Some Text</font>
© www.soinside.com 2019 - 2024. All rights reserved.