Skip to content

Commit

Permalink
closing #91, closing #176
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadYounes committed Jan 30, 2021
1 parent 38bfef4 commit ee9dc53
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
45 changes: 36 additions & 9 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports = {
try {
configuration = eval('(' + metadata.param + ')') // eslint-disable-line no-eval
} catch (e) {
throw node.error('Invlaid config object', { details: e })
throw node.error('Invalid config object', { details: e })
}
context.config = config.configure(configuration.options, configuration.plugins)
context.util = util.configure(context.config)
Expand All @@ -141,36 +141,63 @@ module.exports = {
name: 'prepend',
action: function (decl, expr, context) {
let prefix = ''
decl.raws.value.raw.replace(expr, function (m, v) {
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, function (m, v) {
prefix += v
})
decl.value = decl.raws.value.raw = prefix + decl.raws.value.raw
if (hasRawValue) {
decl.value = decl.raws.value.raw = prefix + decl.raws.value.raw
} else {
decl.value = prefix + decl.value
}
return true
}
},
{
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
let suffix = ''
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, function (m, v) {
suffix = v + suffix
})
if (hasRawValue) {
decl.value = decl.raws.value.raw = decl.raws.value.raw + suffix
} else {
decl.value = decl.value + suffix
}
return true
}
},
{
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
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : decl.value}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
const result = raw.replace(expr, function (match, value) {
return hasRawValue ? value + match : value
})
if (hasRawValue) {
decl.value = decl.raws.value.raw = result
} else {
decl.value = result
}
return true
}
},
{
name: '',
action: function (decl, expr, context) {
decl.raws.value.raw.replace(expr, function (match, value) {
decl.value = decl.raws.value.raw = value + match
const hasRawValue = decl.raws.value && decl.raws.value.raw
const raw = `${decl.raws.between.substr(1).trim()}${hasRawValue ? decl.raws.value.raw : ''}${decl.important ? decl.raws.important.substr(9).trim() : ''}`
raw.replace(expr, function (match, value) {
if (hasRawValue) {
decl.value = decl.raws.value.raw = value + match
} else {
decl.value = value
}
})
return true
}
Expand Down
23 changes: 14 additions & 9 deletions lib/rtlcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,23 @@ module.exports = function (options, plugins, hooks) {
if (!context.util.each(context.config.plugins,
function (plugin) {
return context.util.each(plugin.directives.value, function (directive) {
if (node.raws.value && node.raws.value.raw) {
const expr = context.util.regexDirective(directive.name)
if (expr.test(node.raws.value.raw)) {
expr.lastIndex = 0
if (directive.action(node, expr, context)) {
if (context.config.clean) {
const hasRawValue = node.raws.value && node.raws.value.raw
const expr = context.util.regexDirective(directive.name)
if (expr.test(`${node.raws.between}${hasRawValue ? node.raws.value.raw : ''}${node.important ? node.raws.important : ''}`)) {
expr.lastIndex = 0
if (directive.action(node, expr, context)) {
if (context.config.clean) {
node.raws.between = context.util.trimDirective(node.raws.between)
if (node.important) {
node.raws.important = context.util.trimDirective(node.raws.important)
}
if (hasRawValue) {
node.value = node.raws.value.raw = context.util.trimDirective(node.raws.value.raw)
}
flipped++
// break
return false
}
flipped++
// break
return false
}
}
})
Expand Down
24 changes: 12 additions & 12 deletions test/data/rtlcss-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module.exports = [
},
{
should: 'Should prepend value. (default)',
expected: 'div { font-family: "Droid Arabic Kufi", "Droid Sans", Tahoma; }',
input: 'div { font-family: "Droid Sans", Tahoma/*rtl:prepend:"Droid Arabic Kufi", */; }',
expected: 'div { font-family: "Droid Arabic Kufi", "Droid Sans", Tahoma }',
input: 'div { font-family: /*rtl:prepend:"Droid Arabic Kufi", */"Droid Sans", Tahoma }',
reversable: false
},
{
Expand All @@ -97,8 +97,8 @@ module.exports = [
},
{
should: 'Should prepend value (!important comment)',
expected: 'div { font-family: "Droid Arabic Kufi", "Droid Sans", Tahoma; }',
input: 'div { font-family: "Droid Sans", Tahoma/*!rtl:prepend:"Droid Arabic Kufi", */; }',
expected: 'div { font-family: "Droid Arabic Kufi", "Droid Sans", Tahoma!important; }',
input: 'div { font-family: "Droid Sans", Tahoma!important/*!rtl:prepend:"Droid Arabic Kufi", */; }',
reversable: false
},
{
Expand All @@ -110,8 +110,8 @@ module.exports = [
},
{
should: 'Should replace value.',
expected: 'div { font-family: "Droid Arabic Kufi"; }',
input: 'div { font-family: "Droid Sans", Tahoma/*rtl:"Droid Arabic Kufi"*/; }',
expected: 'div { font-family: "Droid Arabic Kufi" }',
input: 'div { font-family: /*rtl:"Droid Arabic Kufi"*/"Droid Sans", Tahoma }',
reversable: false
},
{
Expand All @@ -136,8 +136,8 @@ module.exports = [
},
{
should: 'Should append value. (default)',
expected: 'div { font-family: "Droid Sans", Tahoma, "Droid Arabic Kufi"; }',
input: 'div { font-family: "Droid Sans", Tahoma/*rtl:append:, "Droid Arabic Kufi"*/; }',
expected: 'div { font-family: "Droid Sans", Tahoma, "Droid Arabic Kufi" }',
input: 'div { font-family: /*rtl:append:, "Droid Arabic Kufi"*/"Droid Sans", Tahoma }',
reversable: false
},
{
Expand All @@ -162,8 +162,8 @@ module.exports = [
},
{
should: 'Should insert value. (default)',
expected: 'div { font-family: "Droid Sans", "Droid Arabic Kufi", Tahoma; }',
input: 'div { font-family: "Droid Sans"/*rtl:insert:, "Droid Arabic Kufi"*/, Tahoma; }',
expected: 'div { font-family: "Droid Arabic Kufi", "Droid Sans", Tahoma }',
input: 'div { font-family: /*rtl:insert:"Droid Arabic Kufi", */"Droid Sans", Tahoma }',
reversable: false
},
{
Expand Down Expand Up @@ -214,8 +214,8 @@ module.exports = [
},
{
should: 'Should ignore flipping - decl. level (default)',
expected: 'div { left:10px;text-align:left;}',
input: 'div { left:10px/*rtl:ignore*/;text-align:right;}',
expected: 'div { left:10px;text-align:right}',
input: 'div { left:10px/*rtl:ignore*/;text-align:/*rtl:ignore*/right}',
reversable: false
},
{
Expand Down

0 comments on commit ee9dc53

Please sign in to comment.