recrit/svg-style-scoper
Composer 安装命令:
composer require recrit/svg-style-scoper
包简介
Scope the styles within the SVG markup to avoid SVG the styles affecting other SVGs or other elements on the page.
README 文档
README
Scope the styles within an SVG's inline markup to avoid the styles affecting other inline SVGs or other elements on the page.
Problem
A website wants to allow users to upload SVG files and display them as inline SVG markup on the site. Content editors often use the same app to create the SVGs which leads to the same HTML classes being used in the SVG markup for multiple SVGs on the same page. The last inline SVG displayed on the page will override the styles of all other SVGs above it that use the same classes.
Example: All elements on the page using the class "cls-0" would have a white fill.
<style type="text/css">
.cls-0 {fill-rule:evenodd;clip-rule:evenodd;fill:#FFFFFF;}
</style>
Solution
The style tag "scoped" attribute has been deprecated, so it cannot be used to limit the scope of the SVG styles.
This library alters the SVG styles so that they only apply to the contents within the SVG.
- All style declarations within the SVG's style tag are appended with a hash. Specifically, only styles that start with a class and ID based styles. See below for all alterations to the SVG styles.
- The appended hash makes the styles unique to the SVG and any other placement of the exact same inline SVG displayed on the page.
- The appended hash preserves the original CSS selector specificity. Keeping the same specificity lets any page level styles (not in the SVG) to continue to work as expected. Page level style example: a site's theme changing the color of the SVG path.
Alterations to the SVG styles
| # | Original CSS | Altered CSS | Updated SVG inner element attributes | Goal |
|---|---|---|---|---|
| 1 | .cls-0 {} | .cls-0-HASH {} | class="cls-0-HASH" | Replace the class "cls-0". |
| 2 | .cls-1 + .cls-x {} | .cls-1-HASH + .cls-x {} | class="cls-1-HASH" | Replace the class "cls-1" only. |
| 3 | .cls-1 .cls-0 {} | .cls-1-HASH .cls-0-HASH {} | class="cls-0-HASH", class="cls-1-HASH" | Replace the class "cls-1" and "cls-0" since "cls-0" was replaced in #1 above. |
| 4 | #id-0 {} | #id-0-HASH {} | id="id-0-HASH" | Replace the ID "id-0". |
| 5 | path {} | REMOVED | none | Bad practice, eliminate the style. |
| 6 | * path {} | REMOVED | none | Bad practice, eliminate the style. |
recrit/svg-style-scoper 适用场景与选型建议
recrit/svg-style-scoper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 681 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 recrit/svg-style-scoper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 recrit/svg-style-scoper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 681
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-11