我需要一些帮助来编写一个很棒的类来获取样式表、检测浏览器特定的 CSS3 规则并添加对所有兼容浏览器的支持。这样,我们只需为一个浏览器编写样式表,然后在准备好生产时处理 CSS 文件。
到目前为止,这是我对这门课的看法:
class CssRewriter {
public function reformCss($file) {
// Get the CSS as a string
$cssString = file_get_contents($file);
// Use regex to capture all styles delimited by {...}
// Use regex to determine if the any of the captured styles are browser
// specific (starts with -moz, -webkit, etc)
// Determine which CSS3 rules are not present and add them to the style
// (so if you have -moz-linear-gradient, automatically add the webkit
// version)
}
}
最佳答案
哎呀。伙计,CSS 解析器并不像您想象的那么简单。依赖正则表达式只是要求一个打字错误被完全误解。
不是您正在寻找的答案,但很可能是更好的答案:您是否考虑过使用 Sass和混合?您不是第一个遇到 CSS 重复性问题的人,因此其他人已经为您面临 CSS 预处理器的挑战。
关于重写 CSS3 规则以实现跨浏览器兼容性的 PHP 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783996/