CSS 内联类的优先级不正确

发布于 2024-11-29 11:40:55 字数 1119 浏览 0 评论 0原文

如果您查看这个CssToInlineStyles。它本质上是试图让 CSS 全部内联。然而,当涉及到已经内联的 CSS 被样式块中的定义覆盖时,它的优先级完全错误!

我认为在 第 312 行 我需要添加一个条件确保已经存在的内联样式永远不会被 CSS 属性中的任何定义覆盖。但看来事情并没有那么容易!

有什么想法吗?我尝试联系班主任,但没有回复。如果我解决了这个问题,我将在 github 上提出拉取请求。

更新

如果样式块中有以下内容:

p{font-family:Arial;font-size:0.9em;color:#53534a;} 
.webv {font-size: 10px; padding:5px 0 0 150px}

那么如果您有像这样的 ap 标记:

<p class="webv" >Testing</p>

那么类 .webv 需要根据 font-size 生效。

更新 2

Hakre 的解决方案运行良好,但有一种情况不起作用。例如,如果你有这个样式块:

  *{padding:0;margin:0;}
  h1{padding-bottom:10px;}

h1 的填充仍然是 0,第二个 h1 不会接管它应该接管的!所以它看起来像这样:

<h1 style="padding:0; margin:0;">Test</h1>

If you look at this CssToInlineStyles class on github. It is essentially trying to make CSS all inline. However, it has priority all wrong when it comes to the already inline CSS being overwritten by the definitions in the style block!

I think on line 312 I need to put in a condition to make sure the inline styles already present should never be overwritten by any definition in the CSS properties. But it looks like it isn't that easy!

Any ideas? I've tried contacting the owner of the class, no reply. If I fix this, I'll make a pull request on github.

Update

If you have the following in a style block:

p{font-family:Arial;font-size:0.9em;color:#53534a;} 
.webv {font-size: 10px; padding:5px 0 0 150px}

Then if you have a p tag like so:

<p class="webv" >Testing</p>

Then the class .webv needs to take effect in terms of font-size.

Update 2

Hakre's solution works well but there is one case it doesn't work. For example if you have this style block:

  *{padding:0;margin:0;}
  h1{padding-bottom:10px;}

The padding for the h1 is still 0, the second h1 doesn't take over which it should! So it looks like this:

<h1 style="padding:0; margin:0;">Test</h1>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-12-06 11:40:56

幸运的是,这个类的所有者已经解决了这个问题!更新后的类可以在此处找到。

Fortunately the owner of this class has fixed this issue! The updated class can be found here.

-柠檬树下少年和吉他 2024-12-06 11:40:55

如果您不想覆盖它们,您首先需要从 HTML 源获取现有属性。

然后,您可以在设置新值之前检查是否已设置这些值以防止覆盖,或者稍后将它们覆盖为默认值。

由于之前已经提取了现有属性,因此它的工作方式可能是:(

foreach($rule['properties'] as $key => $value)
    if(!isset($properties[$key])) $properties[$key] = $value;

这将覆盖 NULL 属性,如果出现问题,请使用 array_key_exists 而不是 isset)。

您应该向类/函数添加一个标志来更改行为,这样当前的行为就不会中断,但您可以将您的需求设为可选。

编辑:由于可以应用多个规则,下一个规则不会覆盖上一个规则的某些内容(它应该但被上面的编辑阻止),因此这需要更多的控制来保留原始属性:

$originalProperties = $properties;
foreach($rule['properties'] as $key => $value) $properties[$key] = $value;
$properties = $originalProperties + $properties;

编辑2:第二个建议也不会起作用。基本上需要获取非常原始的值,因为该函数在迭代规则和元素时会更改样式属性。同一元素可以修改两次,因此 $originalProperties 并不是真正的原始属性,而是可以修改的属性。

也许这适用于 spl_object_hash为每个元素创建一致的 id/key,但我不确定。也许 DomElement 在内部提供了类似的东西,但我还没有找到这样的东西(应该更适合)。每个具体元素都有一个特定的键/id,只有在第一次出现时,才能保留非常原始的属性:

$elementId = spl_object_hash($element);
if(!isset($originalProperties[$elementId]))
{
    $originalProperties[$elementId] = $properties;
    $originalElements[$elementId] = $element; // Needed to make spl_object_hash work
}
foreach($rule['properties'] as $key => $value) $properties[$key] = $value;
$properties = $originalProperties[$elementId] + $properties;

If you don't want to have them overwritten you first need to obtain the existing properties from the HTML source.

You then can either check if those are already set before setting the new to prevent overwriting, or you just overwrite them later on to the default ones.

As existing properties are already extracted before, it might work as:

foreach($rule['properties'] as $key => $value)
    if(!isset($properties[$key])) $properties[$key] = $value;

(this will overwrite NULL properties, in case it's a problem, use array_key_exists instead of isset).

You should add a flag to the class/function to change the behavior, so the current behavior does not break but you make your needs optional.

Edit: As multiple rules can apply, the next rule will not overwrite something of the previous rule (which it should but is prevented by the edit above), so this needs more control to preserve the original properties:

$originalProperties = $properties;
foreach($rule['properties'] as $key => $value) $properties[$key] = $value;
$properties = $originalProperties + $properties;

Edit 2: The second suggestion won't work as well. Basically there is need to get the very original values because that function changes the style attribute while it iterates over rules and elements. The same element can become modified twice, so the $originalProperties are not really the original properties but can be modified ones.

Maybe this works with spl_object_hash to create a consistent id/key for each element, but I'm not sure. Maybe DomElement offers something comparable internally, but I have not found such (which should fit better). With a specific key/id per each concrete element, only on first appearance, the very original properties can be preserved:

$elementId = spl_object_hash($element);
if(!isset($originalProperties[$elementId]))
{
    $originalProperties[$elementId] = $properties;
    $originalElements[$elementId] = $element; // Needed to make spl_object_hash work
}
foreach($rule['properties'] as $key => $value) $properties[$key] = $value;
$properties = $originalProperties[$elementId] + $properties;
卖梦商人 2024-12-06 11:40:55

您可以在 CSS 定义后使用 !important 以确保其优先。

例如

.yourstyle
{
    color: #000 !important;
}

You can use !important after the CSS definition to make sure it takes priority.

E.g.

.yourstyle
{
    color: #000 !important;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文