├── .github └── workflows │ └── tests.yml ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test.js /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v1 16 | with: 17 | node-version: "12.x" 18 | - run: npm ci 19 | - run: npm test 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jonathan Svenheden 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XPath to CSS 2 | 3 | ![Tests][tests-badge] 4 | [![NPM version][npm-image]][npm-url] 5 | 6 | Utility function for converting XPath expressions to CSS selectors. 7 | 8 | Originally written in Python by [santiycr](https://github.com/santiycr) for [cssify](https://github.com/santiycr/cssify) and ported to JavaScript by [Dither](https://github.com/Dither) who published it in [this gist](https://gist.github.com/Dither/1909679). Since I needed it in a project and can't depend on a gist in my `package.json` I have converted it to ES2015 and CommonJS, cleaned it up a bit and [published it to npm][npm-url]. 9 | 10 | ## Install 11 | 12 | ``` 13 | $ npm install --save xpath-to-css 14 | ``` 15 | 16 | ## Usage 17 | 18 | ```js 19 | import xPathToCss from "xpath-to-css"; 20 | 21 | const xPath = 22 | '//div[@id="foo"][2]/span[@class="bar"]//a[contains(@class, "baz")]//img[1]'; 23 | const css = xPathToCss(xPath); 24 | console.log(css); // => 'div#foo:nth-of-type(2) > span.bar a[class*=baz] img:first-of-type' 25 | ``` 26 | 27 | ## License 28 | 29 | MIT © [Jonathan Svenheden](https://github.com/svenheden) 30 | 31 | [npm-url]: https://npmjs.org/package/xpath-to-css 32 | [npm-image]: https://badge.fury.io/js/xpath-to-css.svg 33 | [tests-badge]: https://github.com/svenheden/xpath-to-css/workflows/Tests/badge.svg 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * XPath to CSS 3 | * 4 | * Utility function for converting XPath expressions to CSS selectors 5 | * 6 | * Originally written in Python by [santiycr](https://github.com/santiycr) for 7 | * [cssify](https://github.com/santiycr/cssify) and ported to JavaScript by 8 | * [Dither](https://github.com/Dither). Converted to ES2015 and packaged as an npm module by 9 | * [svenheden](https://github.com/svenheden) 10 | */ 11 | 12 | 'use strict'; 13 | 14 | const isValidXPath = expr => ( 15 | typeof expr != 'undefined' && 16 | expr.replace(/[\s-_=]/g,'') !== '' && 17 | expr.length === expr.replace(/[-_\w:.]+\(\)\s*=|=\s*[-_\w:.]+\(\)|\sor\s|\sand\s|\[(?:[^\/\]]+[\/\[]\/?.+)+\]|starts-with\(|\[.*last\(\)\s*[-\+<>=].+\]|number\(\)|not\(|count\(|text\(|first\(|normalize-space|[^\/]following-sibling|concat\(|descendant::|parent::|self::|child::|/gi,'').length 18 | ); 19 | 20 | const getValidationRegex = () => { 21 | let regex = 22 | "(?P"+ 23 | "("+ 24 | "^id\\([\"\\']?(?P%(value)s)[\"\\']?\\)"+// special case! `id(idValue)` 25 | "|"+ 26 | "(?P