CSS 内联类的优先级不正确
如果您查看这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
幸运的是,这个类的所有者已经解决了这个问题!更新后的类可以在此处找到。
Fortunately the owner of this class has fixed this issue! The updated class can be found here.
如果您不想覆盖它们,您首先需要从 HTML 源获取现有属性。
然后,您可以在设置新值之前检查是否已设置这些值以防止覆盖,或者稍后将它们覆盖为默认值。
由于之前已经提取了现有属性,因此它的工作方式可能是:(
这将覆盖
NULL
属性,如果出现问题,请使用array_key_exists
而不是isset)。
您应该向类/函数添加一个标志来更改行为,这样当前的行为就不会中断,但您可以将您的需求设为可选。
编辑:由于可以应用多个规则,下一个规则不会覆盖上一个规则的某些内容(它应该但被上面的编辑阻止),因此这需要更多的控制来保留原始属性:
编辑2:第二个建议也不会起作用。基本上需要获取非常原始的值,因为该函数在迭代规则和元素时会更改样式属性。同一元素可以修改两次,因此
$originalProperties
并不是真正的原始属性,而是可以修改的属性。也许这适用于
spl_object_hash
为每个元素创建一致的 id/key,但我不确定。也许 DomElement 在内部提供了类似的东西,但我还没有找到这样的东西(应该更适合)。每个具体元素都有一个特定的键/id,只有在第一次出现时,才能保留非常原始的属性:
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:
(this will overwrite
NULL
properties, in case it's a problem, usearray_key_exists
instead ofisset
).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:
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:您可以在 CSS 定义后使用
!important
以确保其优先。例如
You can use
!important
after the CSS definition to make sure it takes priority.E.g.