Comparing version 1.5.0 to 1.6.0
@@ -81,8 +81,20 @@ 'use strict'; | ||
.replace(patt, (_, name, data) => { | ||
const handler = pseudos[name]; | ||
const handlerIsFn = typeof handler === 'function'; | ||
data = typeof data === 'string' | ||
? restore(data.trim()) | ||
? data.trim() | ||
: undefined; | ||
return typeof handler === 'function' | ||
? handler(data) | ||
let args = []; | ||
if (data) { | ||
args = (handlerIsFn && handler.length > 1 | ||
? data.split(/\s*,\s*/) | ||
: [data]) | ||
.map(restore); | ||
} | ||
return handlerIsFn | ||
? handler(...args) | ||
: handler; | ||
@@ -140,2 +152,17 @@ }); | ||
self.quotedString = str => { | ||
const single = "'"; | ||
const double = '"'; | ||
const strip = /^["']|["']$/g; | ||
if ( | ||
(str.startsWith(single) && str.endsWith(single)) | ||
|| (str.startsWith(double) && str.endsWith(double)) | ||
) { | ||
str = str.replace(strip, ''); | ||
} | ||
return `"${str.replace(/"/g, '"')}"`; | ||
}; | ||
const literalCapture = string => { | ||
@@ -142,0 +169,0 @@ const literals = []; |
'use strict'; | ||
const {tokenStreams, decorateToken, translateCaseMap, | ||
arrayFilterUnique, applyCustomPsuedos} = require('./helpers'); | ||
arrayFilterUnique, applyCustomPsuedos, quotedString} = require('./helpers'); | ||
@@ -56,3 +56,6 @@ const self = module.exports = function cssToXPath(css, {pseudos}={}) { | ||
self.applyCustomPsuedos = applyCustomPsuedos; | ||
Object.assign(self, { | ||
applyCustomPsuedos, | ||
quotedString, | ||
}); | ||
@@ -59,0 +62,0 @@ function xpathExpression(tokens) { |
{ | ||
"name": "csstoxpath", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "CSS to XPath", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
18
test.js
@@ -274,2 +274,4 @@ 'use strict'; | ||
const {quotedString} = cssToXpath; | ||
const pseudos = { | ||
@@ -296,2 +298,6 @@ | ||
// Parse as multiple arguments if handler specifies them. | ||
link: (text, href) => `a${href ? `[href="${href}"]` : ''}:text(${quotedString(text)})`, | ||
link2: text => `a:text(${quotedString(text)})`, | ||
// Regexes. | ||
@@ -357,2 +363,14 @@ [/custom-(?<position>\d+)(-(?<type>heading|link))?/](data, matches) { | ||
]], | ||
[':link(Some text, https://link.com)', [ | ||
`a[href="https://link.com"]:text("Some text")`, | ||
`//a[(@href = 'https://link.com') and (translate(normalize-space(), 'SOMETX', 'sometx') = "some text")]`, | ||
]], | ||
[':link("Some, text, with, commas", https://link.com)', [ | ||
`a[href="https://link.com"]:text("Some, text, with, commas")`, | ||
`//a[(@href = 'https://link.com') and (translate(normalize-space(), 'SOMETXWIHCA', 'sometxwihca') = "some, text, with, commas")]`, | ||
]], | ||
[':link2(Some text, but no link)', [ | ||
`a:text("Some text, but no link")`, | ||
`//a[translate(normalize-space(), 'SOMETXBUNLIK', 'sometxbunlik') = "some text, but no link"]`, | ||
]], | ||
[':custom-1-heading', [ | ||
@@ -359,0 +377,0 @@ 'element:nth-of-type(1) > h1', |
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
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
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
39606
924
1