wishthis/node_modules/rtlcss/lib/plugin.js

516 lines
17 KiB
JavaScript
Raw Normal View History

2022-01-21 08:28:41 +00:00
'use strict'
2022-05-29 09:24:36 +00:00
var config = require('./config.js')
var util = require('./util.js')
2022-01-21 08:28:41 +00:00
module.exports = {
2022-05-29 09:24:36 +00:00
'name': 'rtlcss',
'priority': 100,
'directives': {
'control': {
'ignore': {
'expect': { 'atrule': true, 'comment': true, 'decl': true, 'rule': true },
'endNode': null,
'begin': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
// find the ending node in case of self closing directive
if (!this.endNode && metadata.begin && metadata.end) {
2022-05-29 09:24:36 +00:00
var n = node
2022-01-21 08:28:41 +00:00
while (n && n.nodes) {
n = n.nodes[n.nodes.length - 1]
}
this.endNode = n
}
2022-05-29 09:24:36 +00:00
var prevent = true
2022-01-21 08:28:41 +00:00
if (node.type === 'comment' && node.text.match(/^\s*!?\s*rtl:end:ignore/)) {
prevent = false
}
return prevent
},
2022-05-29 09:24:36 +00:00
'end': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
// end if:
// 1. block directive and the node is comment
// 2. self closing directive and node is endNode
2022-05-29 09:24:36 +00:00
if (metadata.begin !== metadata.end && node.type === 'comment' || metadata.begin && metadata.end && node === this.endNode) {
2022-01-21 08:28:41 +00:00
// clear ending node
this.endNode = null
return true
}
return false
}
},
2022-05-29 09:24:36 +00:00
'rename': {
'expect': {'rule': true},
'begin': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
node.selector = context.util.applyStringMap(node.selector, false)
return false
},
2022-05-29 09:24:36 +00:00
'end': function (node, context) {
2022-01-21 08:28:41 +00:00
return true
}
},
2022-05-29 09:24:36 +00:00
'raw': {
'expect': {'self': true},
'begin': function (node, metadata, context) {
var nodes = context.postcss.parse(metadata.param, { from: node.source.input.from })
2022-01-21 08:28:41 +00:00
node.parent.insertBefore(node, nodes)
return true
},
2022-05-29 09:24:36 +00:00
'end': function (node, context) {
2022-01-21 08:28:41 +00:00
return true
}
},
2022-05-29 09:24:36 +00:00
'remove': {
'expect': {'atrule': true, 'rule': true, 'decl': true},
'begin': function (node, metadata, context) {
var prevent = false
2022-01-21 08:28:41 +00:00
switch (node.type) {
case 'atrule':
case 'rule':
case 'decl':
prevent = true
node.remove()
}
return prevent
},
2022-05-29 09:24:36 +00:00
'end': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
return true
}
},
2022-05-29 09:24:36 +00:00
'options': {
'expect': {'self': true},
'stack': [],
'begin': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
this.stack.push(util.extend({}, context.config))
2022-05-29 09:24:36 +00:00
var options
2022-01-21 08:28:41 +00:00
try {
options = JSON.parse(metadata.param)
} catch (e) {
2022-05-29 09:24:36 +00:00
throw node.error('Invlaid options object', { 'details': e })
2022-01-21 08:28:41 +00:00
}
context.config = config.configure(options, context.config.plugins)
context.util = util.configure(context.config)
return true
},
2022-05-29 09:24:36 +00:00
'end': function (node, metadata, context) {
var config = this.stack.pop()
2022-01-21 08:28:41 +00:00
if (config && !metadata.begin) {
context.config = config
context.util = util.configure(context.config)
}
return true
}
},
2022-05-29 09:24:36 +00:00
'config': {
'expect': {'self': true},
'expr': {
'fn': /function([^\(]*)\(([^\(\)]*?)\)[^\{]*\{([^]*)\}/ig,
'rx': /\/([^\/]*)\/(.*)/ig
},
'stack': [],
'begin': function (node, metadata, context) {
2022-01-21 08:28:41 +00:00
this.stack.push(util.extend({}, context.config))
2022-05-29 09:24:36 +00:00
var configuration
2022-01-21 08:28:41 +00:00
try {
2022-05-29 09:24:36 +00:00
configuration = eval('(' + metadata.param + ')') // eslint-disable-line no-eval
2022-01-21 08:28:41 +00:00
} catch (e) {
2022-05-29 09:24:36 +00:00
throw node.error('Invlaid config object', { 'details': e })
2022-01-21 08:28:41 +00:00
}
context.config = config.configure(configuration.options, configuration.plugins)
context.util = util.configure(context.config)
return true
},
2022-05-29 09:24:36 +00:00
'end': function (node, metadata, context) {
var config = this.stack.pop()
2022-01-21 08:28:41 +00:00
if (config && !metadata.begin) {
context.config = config
context.util = util.configure(context.config)
}
return true
}
}
},
2022-05-29 09:24:36 +00:00
'value': [
2022-01-21 08:28:41 +00:00
{
2022-05-29 09:24:36 +00:00
'name': 'ignore',
'action': function (decl, expr, context) {
2022-01-21 08:28:41 +00:00
return true
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'prepend',
'action': function (decl, expr, context) {
var prefix = ''
decl.raws.value.raw.replace(expr, function (m, v) {
2022-01-21 08:28:41 +00:00
prefix += v
})
2022-05-29 09:24:36 +00:00
decl.value = decl.raws.value.raw = prefix + decl.raws.value.raw
2022-01-21 08:28:41 +00:00
return true
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'append',
'action': function (decl, expr, context) {
decl.value = decl.raws.value.raw = decl.raws.value.raw.replace(expr, function (match, value) {
return match + value
2022-01-21 08:28:41 +00:00
})
return true
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'insert',
'action': function (decl, expr, context) {
decl.value = decl.raws.value.raw = decl.raws.value.raw.replace(expr, function (match, value) {
return value + match
})
2022-01-21 08:28:41 +00:00
return true
}
},
{
2022-05-29 09:24:36 +00:00
'name': '',
'action': function (decl, expr, context) {
decl.raws.value.raw.replace(expr, function (match, value) {
decl.value = decl.raws.value.raw = value + match
2022-01-21 08:28:41 +00:00
})
return true
}
}
]
},
2022-05-29 09:24:36 +00:00
'processors': [
2022-01-21 08:28:41 +00:00
{
2022-05-29 09:24:36 +00:00
'name': 'variable',
'expr': /^--/im,
'action': function (prop, value) {
return { 'prop': prop, 'value': value }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'direction',
'expr': /direction/im,
'action': function (prop, value, context) {
return { 'prop': prop, 'value': context.util.swapLtrRtl(value) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'left',
'expr': /left/im,
'action': function (prop, value, context) {
return { 'prop': prop.replace(this.expr, function () { return 'right' }), 'value': value }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'right',
'expr': /right/im,
'action': function (prop, value, context) {
return { 'prop': prop.replace(this.expr, function () { return 'left' }), 'value': value }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'four-value syntax',
'expr': /^(margin|padding|border-(color|style|width))$/ig,
'cache': null,
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'match': /[^\s\uFFFD]+/g
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var state = context.util.guardFunctions(value)
var result = state.value.match(this.cache.match)
2022-01-21 08:28:41 +00:00
if (result && result.length === 4 && (state.store.length > 0 || result[1] !== result[3])) {
2022-05-29 09:24:36 +00:00
var i = 0
state.value = state.value.replace(this.cache.match, function () {
return result[(4 - i++) % 4]
})
2022-01-21 08:28:41 +00:00
}
2022-05-29 09:24:36 +00:00
return { 'prop': prop, 'value': context.util.unguardFunctions(state) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'border radius',
'expr': /border-radius/ig,
'cache': null,
'flip': function (value) {
var parts = value.match(this.cache.match)
var i
2022-01-21 08:28:41 +00:00
if (parts) {
switch (parts.length) {
case 2:
i = 1
if (parts[0] !== parts[1]) {
2022-05-29 09:24:36 +00:00
value = value.replace(this.cache.match, function () {
return parts[i--]
})
2022-01-21 08:28:41 +00:00
}
break
case 3:
// preserve leading whitespace.
2022-05-29 09:24:36 +00:00
value = value.replace(this.cache.white, function (m) {
return m + parts[1] + ' '
})
2022-01-21 08:28:41 +00:00
break
case 4:
i = 0
if (parts[0] !== parts[1] || parts[2] !== parts[3]) {
2022-05-29 09:24:36 +00:00
value = value.replace(this.cache.match, function () {
return parts[(5 - i++) % 4]
})
2022-01-21 08:28:41 +00:00
}
break
}
}
return value
},
2022-05-29 09:24:36 +00:00
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'match': /[^\s\uFFFD]+/g,
'slash': /[^\/]+/g,
'white': /(^\s*)/
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var state = context.util.guardFunctions(value)
state.value = state.value.replace(this.cache.slash, function (m) {
return this.flip(m)
}.bind(this))
return { 'prop': prop, 'value': context.util.unguardFunctions(state) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'shadow',
'expr': /shadow/ig,
'cache': null,
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'replace': /[^,]+/g
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var colorSafe = context.util.guardHexColors(value)
var funcSafe = context.util.guardFunctions(colorSafe.value)
funcSafe.value = funcSafe.value.replace(this.cache.replace, function (m) { return context.util.negate(m) })
2022-01-21 08:28:41 +00:00
colorSafe.value = context.util.unguardFunctions(funcSafe)
2022-05-29 09:24:36 +00:00
return { 'prop': prop, 'value': context.util.unguardHexColors(colorSafe) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'transform and perspective origin',
'expr': /(?:transform|perspective)-origin/ig,
'cache': null,
'flip': function (value, context) {
2022-01-21 08:28:41 +00:00
if (value === '0') {
value = '100%'
} else if (value.match(this.cache.percent)) {
value = context.util.complement(value)
} else if (value.match(this.cache.length)) {
value = context.util.flipLength(value)
}
return value
},
2022-05-29 09:24:36 +00:00
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'match': context.util.regex(['calc', 'percent', 'length'], 'g'),
'percent': context.util.regex(['calc', 'percent'], 'i'),
'length': context.util.regex(['length'], 'gi'),
'xKeyword': /(left|right)/i
2022-01-21 08:28:41 +00:00
}
}
if (value.match(this.cache.xKeyword)) {
value = context.util.swapLeftRight(value)
} else {
2022-05-29 09:24:36 +00:00
var state = context.util.guardFunctions(value)
var parts = state.value.match(this.cache.match)
2022-01-21 08:28:41 +00:00
if (parts && parts.length > 0) {
parts[0] = this.flip(parts[0], context)
2022-05-29 09:24:36 +00:00
state.value = state.value.replace(this.cache.match, function () { return parts.shift() })
2022-01-21 08:28:41 +00:00
value = context.util.unguardFunctions(state)
}
}
2022-05-29 09:24:36 +00:00
return { 'prop': prop, 'value': value }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'transform',
'expr': /^(?!text\-).*?transform$/ig,
'cache': null,
'flip': function (value, process, context) {
var i = 0
return value.replace(this.cache.unit, function (num) {
return process(++i, num)
})
2022-01-21 08:28:41 +00:00
},
2022-05-29 09:24:36 +00:00
'flipMatrix': function (value, context) {
return this.flip(value, function (i, num) {
2022-01-21 08:28:41 +00:00
if (i === 2 || i === 3 || i === 5) {
return context.util.negate(num)
}
return num
}, context)
},
2022-05-29 09:24:36 +00:00
'flipMatrix3D': function (value, context) {
return this.flip(value, function (i, num) {
2022-01-21 08:28:41 +00:00
if (i === 2 || i === 4 || i === 5 || i === 13) {
return context.util.negate(num)
}
return num
}, context)
},
2022-05-29 09:24:36 +00:00
'flipRotate3D': function (value, context) {
return this.flip(value, function (i, num) {
2022-01-21 08:28:41 +00:00
if (i === 1 || i === 4) {
return context.util.negate(num)
}
return num
}, context)
},
2022-05-29 09:24:36 +00:00
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'negatable': /((translate)(x|3d)?|rotate(z|y)?)$/ig,
'unit': context.util.regex(['calc', 'number'], 'g'),
'matrix': /matrix$/i,
'matrix3D': /matrix3d$/i,
'skewXY': /skew(x|y)?$/i,
'rotate3D': /rotate3d$/i
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var state = context.util.guardFunctions(value)
2022-01-21 08:28:41 +00:00
return {
2022-05-29 09:24:36 +00:00
'prop': prop,
'value': context.util.unguardFunctions(state, function (v, n) {
2022-01-21 08:28:41 +00:00
if (n.length) {
if (n.match(this.cache.matrix3D)) {
v = this.flipMatrix3D(v, context)
} else if (n.match(this.cache.matrix)) {
v = this.flipMatrix(v, context)
} else if (n.match(this.cache.rotate3D)) {
v = this.flipRotate3D(v, context)
} else if (n.match(this.cache.skewXY)) {
v = context.util.negateAll(v)
} else if (n.match(this.cache.negatable)) {
v = context.util.negate(v)
}
}
return v
2022-05-29 09:24:36 +00:00
}.bind(this))
2022-01-21 08:28:41 +00:00
}
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'transition',
'expr': /transition(-property)?$/i,
'action': function (prop, value, context) {
return { 'prop': prop, 'value': context.util.swapLeftRight(value) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'background',
'expr': /background(-position(-x)?|-image)?$/i,
'cache': null,
'flip': function (value, context) {
var state = util.saveTokens(value, true)
var parts = state.value.match(this.cache.match)
2022-01-21 08:28:41 +00:00
if (parts && parts.length > 0) {
2022-05-29 09:24:36 +00:00
var keywords = (state.value.match(this.cache.position) || '').length
2022-01-21 08:28:41 +00:00
if (/* edge offsets */ parts.length >= 3 || /* keywords only */ keywords === 2) {
state.value = util.swapLeftRight(state.value)
} else {
parts[0] = parts[0] === '0'
? '100%'
: (parts[0].match(this.cache.percent)
2022-05-29 09:24:36 +00:00
? context.util.complement(parts[0])
: (parts[0].match(this.cache.length)
? context.util.flipLength(parts[0])
: context.util.swapLeftRight(parts[0])))
state.value = state.value.replace(this.cache.match, function () { return parts.shift() })
2022-01-21 08:28:41 +00:00
}
}
return util.restoreTokens(state)
},
2022-05-29 09:24:36 +00:00
'update': function (context, value, name) {
2022-01-21 08:28:41 +00:00
if (name.match(this.cache.gradient)) {
value = context.util.swapLeftRight(value)
if (value.match(this.cache.angle)) {
value = context.util.negate(value)
}
2022-05-29 09:24:36 +00:00
} else if (context.config.processUrls === true || context.config.processUrls.decl === true && name.match(this.cache.url)) {
2022-01-21 08:28:41 +00:00
value = context.util.applyStringMap(value, true)
}
return value
},
2022-05-29 09:24:36 +00:00
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'match': context.util.regex(['position', 'percent', 'length', 'calc'], 'ig'),
'percent': context.util.regex(['calc', 'percent'], 'i'),
'position': context.util.regex(['position'], 'g'),
'length': context.util.regex(['length'], 'gi'),
'gradient': /gradient$/i,
'angle': /\d+(deg|g?rad|turn)/i,
'url': /^url/i
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var colorSafe = context.util.guardHexColors(value)
var funcSafe = context.util.guardFunctions(colorSafe.value)
var parts = funcSafe.value.split(',')
var lprop = prop.toLowerCase()
2022-01-21 08:28:41 +00:00
if (lprop !== 'background-image') {
2022-05-29 09:24:36 +00:00
for (var x = 0; x < parts.length; x++) {
2022-01-21 08:28:41 +00:00
parts[x] = this.flip(parts[x], context)
}
}
funcSafe.value = parts.join(',')
colorSafe.value = context.util.unguardFunctions(funcSafe, this.update.bind(this, context))
return {
2022-05-29 09:24:36 +00:00
'prop': prop,
'value': context.util.unguardHexColors(colorSafe)
2022-01-21 08:28:41 +00:00
}
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'keyword',
'expr': /float|clear|text-align/i,
'action': function (prop, value, context) {
return { 'prop': prop, 'value': context.util.swapLeftRight(value) }
2022-01-21 08:28:41 +00:00
}
},
{
2022-05-29 09:24:36 +00:00
'name': 'cursor',
'expr': /cursor/i,
'cache': null,
'update': function (context, value, name) {
if (context.config.processUrls === true || context.config.processUrls.decl === true && name.match(this.cache.url)) {
2022-01-21 08:28:41 +00:00
value = context.util.applyStringMap(value, true)
}
return value
},
2022-05-29 09:24:36 +00:00
'flip': function (value) {
return value.replace(this.cache.replace, function (s, m) {
return s.replace(m, m.replace(this.cache.e, '*').replace(this.cache.w, 'e').replace(this.cache.star, 'w'))
}.bind(this))
2022-01-21 08:28:41 +00:00
},
2022-05-29 09:24:36 +00:00
'action': function (prop, value, context) {
2022-01-21 08:28:41 +00:00
if (this.cache === null) {
this.cache = {
2022-05-29 09:24:36 +00:00
'replace': /\b(ne|nw|se|sw|nesw|nwse)-resize/ig,
'url': /^url/i,
'e': /e/i,
'w': /w/i,
'star': /\*/i
2022-01-21 08:28:41 +00:00
}
}
2022-05-29 09:24:36 +00:00
var state = context.util.guardFunctions(value)
var parts = state.value.split(',')
for (var x = 0; x < parts.length; x++) {
parts[x] = this.flip(parts[x])
}
state.value = parts.join(',')
2022-01-21 08:28:41 +00:00
return {
2022-05-29 09:24:36 +00:00
'prop': prop,
'value': context.util.unguardFunctions(state, this.update.bind(this, context))
2022-01-21 08:28:41 +00:00
}
}
}
]
}