amehime / hexo-theme-shoka

Just For https://shoka.lostyu.me/
MIT License
896 stars 206 forks source link

当social只有一项时stylus渲染错误 #64

Open JolyneAnasui opened 2 years ago

JolyneAnasui commented 2 years ago

测试styl代码:

.social {
  .item {

    $social = hexo-config();

    for type in $social {
      for i in split(" || ", slice(type, 1)) {
          color = slice(i, 1, -1)
      }

      &.{slice(type, 0, 1)} {
        &::before {
          background-color: unquote(color);
        }
        i {
          color: unquote(color);
        }
}}}}

测试JS代码:

'use strict';

var fs = require('fs');
let data = fs.readFileSync('./loop.styl').toString();

const stylus = require('stylus');
const v1 = {github: 'https://github.com/test || github || "abaaba"',};
const v2 = {github: 'https://github.com/test || github || "abaaba"', email: 'mailto:test || envelope || "#55acd5"'};

function defineConfig(style) {
    style.define('hexo-config', () => {
        return v1;
    });
}

const stylusConfig = stylus(data);

const css = stylusConfig
.use(defineConfig)
.set('include css', true)
.render();

console.log(css);

使用v2时渲染结果正常,但当使用v1时渲染结果如下

$ node test.js
.social .item.g::before {
  background-color: thu;
}
.social .item.g i {
  color: thu;
}
.social .item.h::before {
  background-color: abaaba;
}
.social .item.h i {
  color: abaaba;
}

看样子是在for type in $social循环里把字典的key和value当成了两个量来迭代,第一个量是"github",slice(type, 0, 1)返回"g",即为类名,split匹配失败返回"ithub",然后slice(i, 1, -1)返回“thu”,即为color;第二个量就是整个value字符串,与第一个量同理

实际项目中,social只填一项github然后hexo g,在生成的css中也可以看到错误生成的.item g.item h,导致hover时没能正确显示背景色