| 1 | 'use strict';
|
|---|
| 2 | const BasePlugin = require('../plugin');
|
|---|
| 3 | const { IE_5_5, IE_6, IE_7 } = require('../dictionary/browsers');
|
|---|
| 4 | const { PROPERTY } = require('../dictionary/identifiers');
|
|---|
| 5 | const { ATRULE, DECL } = require('../dictionary/postcss');
|
|---|
| 6 |
|
|---|
| 7 | const hacks = '!_$_&_*_)_=_%_+_,_._/_`_]_#_~_?_:_|'.split('_');
|
|---|
| 8 |
|
|---|
| 9 | module.exports = class LeadingStar extends BasePlugin {
|
|---|
| 10 | /** @param {import('postcss').Result=} result */
|
|---|
| 11 | constructor(result) {
|
|---|
| 12 | super([IE_5_5, IE_6, IE_7], [ATRULE, DECL], result);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * @param {import('postcss').Declaration | import('postcss').AtRule} node
|
|---|
| 17 | * @return {void}
|
|---|
| 18 | */
|
|---|
| 19 | detect(node) {
|
|---|
| 20 | if (node.type === DECL) {
|
|---|
| 21 | // some values are not picked up by before, so ensure they are
|
|---|
| 22 | // at the beginning of the value
|
|---|
| 23 | hacks.forEach((hack) => {
|
|---|
| 24 | if (!node.prop.indexOf(hack)) {
|
|---|
| 25 | this.push(node, {
|
|---|
| 26 | identifier: PROPERTY,
|
|---|
| 27 | hack: node.prop,
|
|---|
| 28 | });
|
|---|
| 29 | }
|
|---|
| 30 | });
|
|---|
| 31 | const { before } = node.raws;
|
|---|
| 32 | if (!before) {
|
|---|
| 33 | return;
|
|---|
| 34 | }
|
|---|
| 35 | hacks.forEach((hack) => {
|
|---|
| 36 | if (before.includes(hack)) {
|
|---|
| 37 | this.push(node, {
|
|---|
| 38 | identifier: PROPERTY,
|
|---|
| 39 | hack: `${before.trim()}${node.prop}`,
|
|---|
| 40 | });
|
|---|
| 41 | }
|
|---|
| 42 | });
|
|---|
| 43 | } else {
|
|---|
| 44 | // test for the @property: value; hack
|
|---|
| 45 | const { name } = node;
|
|---|
| 46 | const len = name.length - 1;
|
|---|
| 47 | if (name.lastIndexOf(':') === len) {
|
|---|
| 48 | this.push(node, {
|
|---|
| 49 | identifier: PROPERTY,
|
|---|
| 50 | hack: `@${name.substr(0, len)}`,
|
|---|
| 51 | });
|
|---|
| 52 | }
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | };
|
|---|