Comparing version 1.3.0 to 1.4.0
140
.eslintrc.js
@@ -1,2 +0,6 @@ | ||
module.exports = { | ||
/*eslint sort-keys: 2*/ | ||
/*eslint object-property-newline: 2*/ | ||
/*eslint quote-props: [2, "consistent"]*/ | ||
const self = module.exports = { | ||
env: { | ||
@@ -6,54 +10,87 @@ es6: true, | ||
}, | ||
extends: 'eslint:recommended', | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
"ecmaFeatures": { | ||
"experimentalObjectRestSpread": true, | ||
}, | ||
ecmaVersion: 9, | ||
}, | ||
extends: 'eslint:recommended', | ||
rules: { | ||
curly: 2, | ||
'no-console': 0, | ||
'no-var': 2, | ||
'no-tabs': 2, | ||
'no-trailing-spaces': 2, | ||
'no-dupe-keys': 2, | ||
'no-else-return': 2, | ||
'no-useless-call': 2, | ||
'no-useless-return': 2, | ||
'no-unused-expressions': [2, {allowShortCircuit: true}], | ||
'no-lonely-if': 2, | ||
'array-bracket-spacing': [2, 'never'], | ||
'array-element-newline': [2, 'consistent'], | ||
'arrow-parens': [2, 'as-needed'], | ||
'arrow-spacing': 2, | ||
'object-shorthand': [2, 'properties'], | ||
'no-empty': [2, { | ||
allowEmptyCatch: true, | ||
'brace-style': [2, 'stroustrup'], | ||
'camelcase': [2, { | ||
ignoreDestructuring: true, | ||
properties: 'never', | ||
}], | ||
"no-unused-vars": [2, { | ||
// "args": "all", | ||
"argsIgnorePattern": "^(req|next)$", | ||
}], | ||
'eol-last': [2, 'always'], | ||
'no-multiple-empty-lines': [2, {max: 2, maxEOF: 1, maxBOF: 1}], | ||
quotes: [2, 'single', {avoidEscape: true, allowTemplateLiterals: true}], | ||
'keyword-spacing': 2, | ||
'space-before-blocks': 2, | ||
'linebreak-style': [2, 'unix'], | ||
'object-curly-spacing': [2, 'never'], | ||
'array-bracket-spacing': [2, 'never'], | ||
'brace-style': [2, 'stroustrup'], | ||
semi: [2, 'always'], | ||
'quote-props': [2, 'as-needed'], | ||
'comma-dangle': [2, { | ||
arrays: 'always-multiline', | ||
functions: 'never', | ||
objects: 'always-multiline', | ||
functions: 'never', | ||
}], | ||
'comma-spacing': [2, { | ||
after: true, | ||
before: false, | ||
after: true, | ||
}], | ||
'curly': 2, | ||
'eol-last': [2, 'always'], | ||
'eqeqeq': 2, | ||
'key-spacing': [2, { | ||
afterColon: true, | ||
beforeColon: false, | ||
afterColon: true, | ||
}], | ||
'keyword-spacing': 2, | ||
'linebreak-style': [2, 'unix'], | ||
'multiline-comment-style': [2, 'starred-block'], | ||
'no-console': 2, | ||
'no-dupe-keys': 2, | ||
'no-else-return': 2, | ||
'no-empty': [2, { | ||
allowEmptyCatch: true, | ||
}], | ||
'no-lonely-if': 2, | ||
'no-multi-spaces': 2, | ||
'no-multiple-empty-lines': [2, { | ||
max: 2, | ||
maxBOF: 1, | ||
maxEOF: 1, | ||
}], | ||
'no-new-object': 2, | ||
'no-template-curly-in-string': 2, | ||
'no-tabs': 2, | ||
'no-throw-literal': 2, | ||
'no-trailing-spaces': 2, | ||
'no-unneeded-ternary': 2, | ||
'no-unused-expressions': [2, {allowShortCircuit: true}], | ||
'no-unused-vars': [2, { | ||
args: 'all', | ||
argsIgnorePattern: '^(req|res|next)$|^_', | ||
varsIgnorePattern: '^_$', | ||
}], | ||
'no-useless-call': 2, | ||
'no-useless-concat': 2, | ||
'no-useless-return': 2, | ||
'no-var': 2, | ||
'object-curly-newline': [2, {consistent: true}], | ||
'object-curly-spacing': [2, 'never'], | ||
'object-shorthand': [2, 'properties'], | ||
'operator-linebreak': [2, 'before', { | ||
overrides: { | ||
':': 'ignore', | ||
'?': 'ignore', | ||
}, | ||
}], | ||
'prefer-arrow-callback': 2, | ||
'prefer-const': [2, {destructuring: 'all'}], | ||
'prefer-destructuring': [2, { | ||
array: false, | ||
object: true, | ||
}], | ||
'prefer-object-spread': 2, | ||
'quote-props': [2, 'as-needed'], | ||
'quotes': [2, 'single', { | ||
allowTemplateLiterals: true, | ||
avoidEscape: true, | ||
}], | ||
'semi': [2, 'always'], | ||
'space-before-blocks': 2, | ||
'space-before-function-paren': [2, { | ||
@@ -64,3 +101,28 @@ anonymous: 'always', | ||
}], | ||
} | ||
'space-unary-ops': [2, { | ||
nonwords: false, | ||
overrides: {'!': true}, | ||
words: true, | ||
}], | ||
'yoda': 2, | ||
}, | ||
}; | ||
self.overrides = [{ | ||
files: ['test.js'], | ||
globals: { | ||
after: true, | ||
api: true, | ||
before: true, | ||
beforeEach: true, | ||
describe: true, | ||
expect: true, | ||
it: true, | ||
models: true, | ||
testbot: true, | ||
}, | ||
rules: { | ||
'no-unused-expressions': 0, | ||
'prefer-arrow-callback': 0, | ||
}, | ||
}]; |
@@ -40,3 +40,3 @@ const parser = require('css-what'); | ||
const patt = new RegExp(`:(${names.join('|')})(?:\\(([^)]+)\\)|(?![\\w-]))`, 'g'); | ||
return restore(string.replace(patt, (m, name, data) => { | ||
return restore(string.replace(patt, (_, name, data) => { | ||
const handler = pseudos[name]; | ||
@@ -43,0 +43,0 @@ data = (typeof data === 'string') ? restore(data.trim()) : undefined; |
38
index.js
@@ -7,3 +7,3 @@ const {tokenStream, decorateToken, translateCaseMap, | ||
const expressions = []; | ||
for (let stream of streams) { | ||
for (const stream of streams) { | ||
stream.forEach(token => decorateToken(token)); | ||
@@ -38,4 +38,4 @@ if (stream[0].isPseudoRoot) { | ||
let filters = []; | ||
let commitFilters = () => { | ||
let flattened = flattenFilters(filters); | ||
const commitFilters = () => { | ||
const flattened = flattenFilters(filters); | ||
if (flattened) { | ||
@@ -48,4 +48,4 @@ xpath.push(`[${flattened}]`); | ||
for (let i = 0; i < tokens.length; i++) { | ||
let token = tokens[i]; | ||
let previous = (i > 0) ? tokens[i-1] : {}; | ||
const token = tokens[i]; | ||
const previous = (i > 0) ? tokens[i-1] : {}; | ||
@@ -72,3 +72,3 @@ if (previous.isNonSiblingAxis && ! token.isTagOrUniversal && ! token.isPseudoComment) { | ||
else if (previous.isSibling) { | ||
let nodeName = token.isTag ? token.name : '*'; | ||
const nodeName = token.isTag ? token.name : '*'; | ||
xpath.push(`/following-sibling::${nodeName}`); | ||
@@ -101,3 +101,3 @@ if (token.isTagOrUniversal) { | ||
default: { | ||
let {data} = token; | ||
const {data} = token; | ||
if (token.isPseudoNot) { | ||
@@ -129,5 +129,5 @@ filters.push(`not(${subExpression(data, {operator: 'or'})})`); | ||
function subExpression(tokens, options={}) { | ||
let stack = []; | ||
const stack = []; | ||
tokens.forEach(stream => { | ||
let filters = []; | ||
const filters = []; | ||
stream.forEach(token => { | ||
@@ -142,7 +142,7 @@ filters.push(...resolveAsFilters(decorateToken(token))); | ||
function resolveAsFilters(token) { | ||
let elements = []; | ||
const elements = []; | ||
let {name, value, action, data} = token; | ||
if (token.isTag) { | ||
let {az, AZ} = translateCaseMap(name); | ||
const {az, AZ} = translateCaseMap(name); | ||
elements.push(`translate(name(), '${az}', '${AZ}') = '${name.toUpperCase()}'`); | ||
@@ -203,3 +203,3 @@ } | ||
case 'nth-child': { | ||
let aliases = { | ||
const aliases = { | ||
odd: '2n+1', | ||
@@ -215,12 +215,12 @@ even: '2n', | ||
} | ||
let nthExpr = /^([-+])?(\d+)?n(?:\+(\d+))?$/.exec(data); | ||
const nthExpr = /^([-+])?(\d+)?n(?:\+(\d+))?$/.exec(data); | ||
if (nthExpr) { | ||
let [, sign, nth=0, offset=0] = nthExpr; | ||
nth = parseInt(nth, 10); | ||
let reverseNth = sign === '-'; | ||
const reverseNth = sign === '-'; | ||
if (reverseNth) { | ||
nth *= -1; | ||
} | ||
let expr = []; | ||
let position = 'position()' + (offset ? ` - ${offset}` : ''); | ||
const expr = []; | ||
const position = 'position()' + (offset ? ` - ${offset}` : ''); | ||
expr.push(position); | ||
@@ -254,3 +254,3 @@ expr.push(reverseNth ? '<=' : '>='); | ||
if (! /case/.test(name)) { | ||
let {az, AZ} = translateCaseMap(searchText); | ||
const {az, AZ} = translateCaseMap(searchText); | ||
text = az ? `translate(normalize-space(), '${AZ}', '${az}')` : `normalize-space()`; | ||
@@ -261,4 +261,4 @@ searchText = searchText.toLowerCase(); | ||
// Respecting authored quote method. | ||
let quoteMatch = /^(['"])([\s\S]*)\1$/.exec(searchText); | ||
let quote = quoteMatch ? quoteMatch[1] : '"'; | ||
const quoteMatch = /^(['"])([\s\S]*)\1$/.exec(searchText); | ||
const quote = quoteMatch ? quoteMatch[1] : '"'; | ||
if (quoteMatch) { | ||
@@ -265,0 +265,0 @@ searchText = quoteMatch[2].trim(); |
{ | ||
"name": "csstoxpath", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "CSS to XPath", | ||
@@ -28,5 +28,5 @@ "main": "index.js", | ||
"chai": "~4.1.2", | ||
"eslint": "~4.12.1", | ||
"eslint": "~5.3.0", | ||
"mocha": "~4.0.1" | ||
} | ||
} |
78
test.js
@@ -1,2 +0,2 @@ | ||
/* global describe it */ | ||
/*eslint no-console: 0*/ | ||
@@ -12,6 +12,6 @@ const cssToXpath = require('./index'); | ||
['.a', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]`, | ||
], | ||
['.a.b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and contains(concat(' ', normalize-space(@class), ' '), ' b ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and contains(concat(' ', normalize-space(@class), ' '), ' b ')]`, | ||
], | ||
@@ -21,3 +21,3 @@ | ||
['#a > .b:last-child', | ||
`//*[@id = 'a']/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ') and (position() = last())]` | ||
`//*[@id = 'a']/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ') and (position() = last())]`, | ||
], | ||
@@ -32,9 +32,9 @@ | ||
['.a + b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(translate(name(), 'b', 'B') = 'B') and (position() = 1)]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(translate(name(), 'b', 'B') = 'B') and (position() = 1)]`, | ||
], | ||
['.a + .b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(position() = 1) and @class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(position() = 1) and @class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]`, | ||
], | ||
['.a + b.b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(translate(name(), 'b', 'B') = 'B') and (position() = 1) and @class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[(translate(name(), 'b', 'B') = 'B') and (position() = 1) and @class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]`, | ||
], | ||
@@ -44,9 +44,9 @@ | ||
['.a ~ b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::b` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::b`, | ||
], | ||
['.a ~ .b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::*[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]`, | ||
], | ||
['.a ~ b.b', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::b[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ')]/following-sibling::b[@class and contains(concat(' ', normalize-space(@class), ' '), ' b ')]`, | ||
], | ||
@@ -66,3 +66,3 @@ | ||
['.a:first-child', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and (position() = 1)]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and (position() = 1)]`, | ||
], | ||
@@ -73,3 +73,3 @@ | ||
['.a:last-child', | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and (position() = last())]` | ||
`//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' a ') and (position() = last())]`, | ||
], | ||
@@ -175,8 +175,8 @@ | ||
`//a/comment()[contains(translate(normalize-space(), 'FO', 'fo'), "foo")]`], | ||
['a:comment:text-contains-case("Foo")', `//a/comment()[contains(normalize-space(), "Foo")]`] | ||
['a:comment:text-contains-case("Foo")', `//a/comment()[contains(normalize-space(), "Foo")]`], | ||
]; | ||
let groups = {}; | ||
const groups = {}; | ||
let activeGroup; | ||
for (let item of samples) { | ||
for (const item of samples) { | ||
if (typeof item === 'string') { | ||
@@ -190,7 +190,7 @@ activeGroup = item; | ||
for (let name in groups) { | ||
for (const name in groups) { | ||
describe(name, function () { | ||
for (let [css, xpath] of groups[name]) { | ||
for (const [css, xpath] of groups[name]) { | ||
it(`should convert '${css}'`, function () { | ||
let converted = cssToXpath(css); | ||
const converted = cssToXpath(css); | ||
if (xpath) { | ||
@@ -214,7 +214,7 @@ expect(converted).to.equal(xpath); | ||
['a, b', `(translate(name(), 'a', 'A') = 'A') or (translate(name(), 'b', 'B') = 'B')`], | ||
[':text("foo")', `translate(normalize-space(), 'FO', 'fo') = "foo"`] | ||
[':text("foo")', `translate(normalize-space(), 'FO', 'fo') = "foo"`], | ||
]; | ||
for (let [css, xpath] of samples) { | ||
for (const [css, xpath] of samples) { | ||
it(`should convert sub-expression '${css}'`, function () { | ||
let converted = cssToXpath.subExpression(css); | ||
const converted = cssToXpath.subExpression(css); | ||
if (xpath) { | ||
@@ -232,3 +232,3 @@ expect(converted).to.equal(xpath); | ||
describe('Unsupported selectors', function () { | ||
let unsupported = [ | ||
const unsupported = [ | ||
':nth-last-child(1)', | ||
@@ -243,5 +243,5 @@ ':nth-of-type', | ||
':required', | ||
'a:lang(en)' | ||
'a:lang(en)', | ||
]; | ||
for (let css of unsupported) { | ||
for (const css of unsupported) { | ||
it(`should error for unsupported selector '${css}'`, function () { | ||
@@ -254,3 +254,3 @@ expect(() => cssToXpath(css)).to.throw(Error); | ||
describe('Author pseudo preprocessing', function () { | ||
let pseudos = { | ||
const pseudos = { | ||
foo: 'foo', | ||
@@ -262,3 +262,3 @@ first: ':first-child:not(:last-child)', | ||
if (data) { | ||
let args = data | ||
const args = data | ||
.split(/\s*,\s*/g) | ||
@@ -273,14 +273,14 @@ .filter(i => i) | ||
let samples = [ | ||
const samples = [ | ||
[':first', [ | ||
`:first-child:not(:last-child)`, | ||
`//*[(position() = 1) and (not(position() = last()))]` | ||
`//*[(position() = 1) and (not(position() = last()))]`, | ||
]], | ||
[':first[b]', [ | ||
`:first-child:not(:last-child)[b]`, | ||
`//*[(position() = 1) and (not(position() = last())) and @b]` | ||
`//*[(position() = 1) and (not(position() = last())) and @b]`, | ||
]], | ||
['a :first c', [ | ||
`a :first-child:not(:last-child) c`, | ||
`//a//*[(position() = 1) and (not(position() = last()))]//c` | ||
`//a//*[(position() = 1) and (not(position() = last()))]//c`, | ||
]], | ||
@@ -290,29 +290,29 @@ // Should be ignored. | ||
`a :first-child c`, | ||
`//a//*[position() = 1]//c` | ||
`//a//*[position() = 1]//c`, | ||
]], | ||
[':child', [ | ||
`:nth-child(n)`, | ||
`//*[position() >= 0]` | ||
`//*[position() >= 0]`, | ||
]], | ||
[':child(1)', [ | ||
`:nth-child(1)`, | ||
`//*[position() = 1]` | ||
`//*[position() = 1]`, | ||
]], | ||
['a:child(1, 2, 3):not(.c)', [ | ||
`a:any(:nth-child(1), :nth-child(2), :nth-child(3)):not(.c)`, | ||
`//a[((position() = 1) or (position() = 2) or (position() = 3)) and (not(@class and contains(concat(' ', normalize-space(@class), ' '), ' c ')))]` | ||
`//a[((position() = 1) or (position() = 2) or (position() = 3)) and (not(@class and contains(concat(' ', normalize-space(@class), ' '), ' c ')))]`, | ||
]], | ||
[`:radio:nth(2)`, [ | ||
`input[type="radio"]:nth-child(2)`, | ||
`//input[(@type = 'radio') and (position() = 2)]` | ||
`//input[(@type = 'radio') and (position() = 2)]`, | ||
]], | ||
[':foo("(:foo)")', [ | ||
`foo`, | ||
`//foo` | ||
]] | ||
`//foo`, | ||
]], | ||
]; | ||
for (let [css, [expectedPostProcess, expectedXPath]] of samples) { | ||
for (const [css, [expectedPostProcess, expectedXPath]] of samples) { | ||
it(`should preprocess CSS with custom pseudos '${css}' and convert to correct XPath`, function () { | ||
let actualPostProcess = cssToXpath.applyCustomPsuedos(css, pseudos); | ||
const actualPostProcess = cssToXpath.applyCustomPsuedos(css, pseudos); | ||
if (expectedPostProcess) { | ||
@@ -319,0 +319,0 @@ expect(actualPostProcess).to.equal(expectedPostProcess); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31656
725