作者:hedongsheng | 来源:互联网 | 2023-05-19 13:15
IvebeenworkingonaCSSminifierforPHPthatsdoneexceptforonething:我一直在为PHP设计一个CSS缩小器,除了一
I've been working on a CSS minifier for PHP that's done except for one thing:
我一直在为PHP设计一个CSS缩小器,除了一件事:
I'd like to have it detect the user's browser and determine whether the user needs border-radius, -webkit-border-radius, -moz-border-radius, etc. So that I dont have to use the very long and annoying groups.
我想让它检测用户的浏览器,并确定用户是否需要边界半径、-webkit-边界半径、- mozilla -边界半径等等,这样我就不必使用很长很烦人的组。
This would either detect all instances of -webkit, -moz, etc. border-radius and merge them OR I would place {vendor}border-radius into the CSS file which would then be replaced by either -webkit-, -moz-, or nothing. The first one is ideal because I'd like for it to be expandable to other projects.
这将会检测到所有的-webkit、-moz等的实例,并将它们合并到一起,否则我将把{供应商}边界的半径放入到CSS文件中,然后由webkit- - -moz- - -或什么都不做。第一个是理想的,因为我希望它可以扩展到其他项目。
For the life of me, I can't figure out a working PHP implementation that accurately detects which prefix to use (and I've googled/searched everywhere I could think of).
在我的生命中,我无法找到一个能够准确检测到使用哪个前缀的工作PHP实现(而且我在我所能想到的任何地方都搜索/搜索过)。
Any help would be much appreciated!
如有任何帮助,我们将不胜感激!
3 个解决方案
5
The main issue here is that PHP (the server) won't know the CSS capabilities of your browser off-hand (the client). The only information that's remotely close to identifying a browser that would get sent to PHP is the user-agent string. Even then, you would still need to research the CSS capabilities of a specific version of a specific browser or engine, based on what you find through parsing the user-agent string, and hard-code some decision-making code based on that information.
这里的主要问题是PHP(服务器)不知道您的浏览器的CSS功能(客户端)。唯一接近于标识将被发送到PHP的浏览器的信息是用户代理字符串。即使这样,您仍然需要研究特定浏览器或引擎的特定版本的CSS功能,基于您通过解析用户代理字符串发现的内容,并基于该信息硬编码一些决策代码。
I think that's the main liability of trying to use server-side code to determine the CSS capabilities of a client. There may be others, but this seems the biggest hurdle, unfortunately.
我认为这是尝试使用服务器端代码来确定客户端的CSS功能的主要责任。也许还有其他的,但不幸的是,这似乎是最大的障碍。
On the client side, there exist scripts like -prefix-free that make adding prefixes a trivial job; just serve your minified CSS with only the unprefixed properties and rules, and let the script add the prefixes for you based on what it knows about the browser (that the server doesn't).
在客户端,存在像-prefix-free这样的脚本,使添加前缀成为一项微不足道的工作;只需使用未加前缀的属性和规则来服务您的缩小的CSS,然后让脚本根据它对浏览器的了解(服务器不知道)为您添加前缀。
The first paragraph from this entry in its FAQ also seems worth a read:
这个条目的第一段也值得一读:
“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.
服务器端脚本需要添加所有前缀,使CSS文件的大小变得更大。此外,它应该维护需要前缀的特性列表,或者将它们全部添加到样式表中。-无前缀自动检测什么需要前缀,什么不需要前缀。
As well as an interview with its author, which it links to:
以及对其作者的采访,它链接到:
"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.
在服务器中不可能做的是-prefix-free。-无前缀检测哪些特性需要前缀,只在需要时添加。此外,它还自动检测哪些属性需要前缀。它不需要保存要添加哪些前缀的列表,所有的东西都被检测到,因此是非常未来的证明。对于预处理器,列表需要保持这种类型的内容。这样的清单注定是不完整的,而且很快就会过时。我尝试过的每个服务器端前缀在很多情况下都失败了。
(Emphases mine.)
(我重点。)