编程语言
首页 > 编程语言> > php – 检测webkit / moz /等.要显示的前缀

php – 检测webkit / moz /等.要显示的前缀

作者:互联网

我一直在为PHP做一个CSS minifier,除了一件事:

我想让它检测用户的浏览器,并确定用户是否需要border-radius,-webkit-border-radius,-moz-border-radius等.这样我就不必使用非常长且烦人的组.

这将检测-webkit,-moz等所有实例的border-radius并合并它们或者我将{vendor} border-radius放入CSS文件中,然后将其替换为-webkit-, – moz-,或无.第一个是理想的,因为我希望它可以扩展到其他项目.

对于我的生活,我无法弄清楚一个有效的PHP实现,它可以准确地检测出使用哪个前缀(而且我已经用谷歌搜索/搜索了我想到的任何地方).

任何帮助将非常感激!

解决方法:

这里的主要问题是PHP(服务器)不会知道您的浏览器的CSS功能(客户端).远程接近识别将被发送到PHP的浏览器的唯一信息是用户代理字符串.即使这样,您仍然需要根据您通过解析用户代理字符串找到的内容来研究特定版本的特定浏览器或引擎的CSS功能,并根据该信息对某些决策代码进行硬编码.

我认为这是尝试使用服务器端代码来确定客户端的CSS功能的主要责任.可能还有其他人,但不幸的是,这似乎是最大的障碍.

在客户端,存在像-prefix-free这样的脚本,使得添加前缀成为一项微不足道的工作;只使用未加前缀的属性和规则来提供缩小的CSS,并让脚本根据它对浏览器的了解(服务器没有)为您添加前缀.

此条目在其FAQ中的第一段似乎也值得一读:

“Something like this belongs to the server-side”

A server side script would need to add all prefixes, making the size of the CSS file considerably larger. Also, it should maintain a list of features that need prefixes, or add them all and unnecessarily bloat the stylesheet. -prefix-free automatically detects what needs a prefix and what doesn’t.

以及链接到的interview with its author

“This is something better done on the server. Do it once instead of on every pageload”

What -prefix-free exactly does, is impossible to do in the server. -prefix-free detects which features need a prefix and only adds it if needed. Also, it automatically detects which properties are available that need a prefix. It doesn’t have to keep lists of which prefixes to add for which features, everything gets feature detected, and thus is very future proof. With preprocessors, lists need to be maintained about this sort of stuff. Such lists are doomed to be incomplete and quickly get out of date. Every server-side prefixer I ever tried fails in a number of cases.

(强调我的.)

标签:php,css,css3,user-agent,vendor-prefix
来源: https://codeday.me/bug/20190530/1182369.html