From 09d30ceed755d53cfc04fd408d0fb246376ed3f2 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 15:59:58 -0500 Subject: [PATCH 01/11] optimize unique query --- src/index.js | 46 +++++++++++++++++++++++++++++++++++----------- src/isUnique.js | 8 ++++++-- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 12cd80c..5779870 100644 --- a/src/index.js +++ b/src/index.js @@ -48,8 +48,12 @@ function getAllSelectors( el, selectors, attributesToIgnore ) function testUniqueness( element, selector ) { const { parentNode } = element; - const elements = parentNode.querySelectorAll( selector ); - return elements.length === 1 && elements[ 0 ] === element; + try { + const elements = parentNode.querySelectorAll( selector ); + return elements.length === 1 && elements[0] === element; + } catch (e) { + return false + } } /** @@ -182,16 +186,36 @@ export default function unique( el, options={} ) } } - const selectors = []; - for( let it of allSelectors ) - { - selectors.unshift( it ); - const selector = selectors.join( ' > ' ); - if( isUnique( el, selector ) ) - { - return selector; + let currentElement = el + while (currentElement) { + let selector = selectorCache ? selectorCache.get(currentElement) : undefined + + if (!selector) { + selector = getUniqueSelector( + currentElement, + selectorTypes, + attributesToIgnore + ) + if (selectorCache) { + selectorCache.set(currentElement, selector) + } + } + + allSelectors.unshift(selector) + const maybeUniqueSelector = allSelectors.join(' > ') + let isUniqueSelector = isUniqueCache ? isUniqueCache.get(maybeUniqueSelector) : undefined + if (isUniqueSelector === undefined) { + isUniqueSelector = _isUnique.isUnique(el, maybeUniqueSelector) + if (isUniqueCache) { + isUniqueCache.set(maybeUniqueSelector, isUniqueSelector) + } } - } + + if (isUniqueSelector) { + return maybeUniqueSelector + } + currentElement = currentElement.parentNode + } return null; } diff --git a/src/isUnique.js b/src/isUnique.js index bcd1e53..3d7819c 100644 --- a/src/isUnique.js +++ b/src/isUnique.js @@ -7,6 +7,10 @@ export function isUnique( el, selector ) { if( !Boolean( selector ) ) return false; - const elems = el.ownerDocument.querySelectorAll( selector ); - return elems.length === 1 && elems[ 0 ] === el; + try { + var elems = el.ownerDocument.querySelectorAll(selector); + return elems.length === 1 && elems[0] === el; + } catch (e) { + return false + } } From 3c8a97726ea4192bf6ff6eabe7a5c5c9ff5f46f1 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 16:07:02 -0500 Subject: [PATCH 02/11] missed some variable definitions --- src/index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 5779870..77916fd 100644 --- a/src/index.js +++ b/src/index.js @@ -173,7 +173,12 @@ function getUniqueSelector( element, selectorTypes, attributesToIgnore ) export default function unique( el, options={} ) { - const { selectorTypes=['id', 'class', 'tag', 'nth-child'], attributesToIgnore= ['id', 'class', 'length'] } = options; + const { + selectorTypes=['id', 'class', 'tag', 'nth-child'], + attributesToIgnore= ['id', 'class', 'length'], + selectorCache, + isUniqueCache + } = options; const allSelectors = []; const parents = getParents( el ); From 09f0380bf77676de0d97e4a6c773f42a342fafda Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 16:11:24 -0500 Subject: [PATCH 03/11] fix isUnique --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 77916fd..a0f54f2 100644 --- a/src/index.js +++ b/src/index.js @@ -210,7 +210,7 @@ export default function unique( el, options={} ) const maybeUniqueSelector = allSelectors.join(' > ') let isUniqueSelector = isUniqueCache ? isUniqueCache.get(maybeUniqueSelector) : undefined if (isUniqueSelector === undefined) { - isUniqueSelector = _isUnique.isUnique(el, maybeUniqueSelector) + isUniqueSelector = isUnique(el, maybeUniqueSelector) if (isUniqueCache) { isUniqueCache.set(maybeUniqueSelector, isUniqueSelector) } From 998ec3bb1c7fd2afbe718240d77b648d547410e1 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 16:14:19 -0500 Subject: [PATCH 04/11] removing extraneous code that was accidentally left --- src/index.js | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index a0f54f2..29cdd7c 100644 --- a/src/index.js +++ b/src/index.js @@ -171,8 +171,7 @@ function getUniqueSelector( element, selectorTypes, attributesToIgnore ) * @api private */ -export default function unique( el, options={} ) -{ +export default function unique( el, options={} ) { const { selectorTypes=['id', 'class', 'tag', 'nth-child'], attributesToIgnore= ['id', 'class', 'length'], @@ -180,16 +179,6 @@ export default function unique( el, options={} ) isUniqueCache } = options; const allSelectors = []; - const parents = getParents( el ); - - for( let elem of parents ) - { - const selector = getUniqueSelector( elem, selectorTypes, attributesToIgnore ); - if( Boolean( selector ) ) - { - allSelectors.push( selector ); - } - } let currentElement = el while (currentElement) { From 728fedfe324ab1892ee2eebd478ef6ed575c25a7 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 16:29:14 -0500 Subject: [PATCH 05/11] github workflow --- .github/workflows/main.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..540ad3d --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,10 @@ +steps: +- uses: actions/checkout@v4 +- name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: '20.x' +- name: Install dependencies + run: npm ci +- name: Run tests + run: npm run test \ No newline at end of file From cf184b158cc4c99373d284c5571d74240096ceb5 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Tue, 19 Dec 2023 16:31:15 -0500 Subject: [PATCH 06/11] on push --- .github/workflows/main.yml | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 540ad3d..d9b91f8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,10 +1,18 @@ -steps: -- uses: actions/checkout@v4 -- name: Use Node.js - uses: actions/setup-node@v3 - with: - node-version: '20.x' -- name: Install dependencies - run: npm ci -- name: Run tests - run: npm run test \ No newline at end of file +name: Test + +on: [push] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Use Node.js + uses: actions/setup-node@v3 + with: + node-version: '20.x' + - name: Install dependencies + run: npm ci + - name: Run tests + run: npm run test \ No newline at end of file From d0e52c87390ea24b2f56da5d401270634809ccf0 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Wed, 20 Dec 2023 11:24:07 -0500 Subject: [PATCH 07/11] fix: semantic release --- .github/workflows/main.yml | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d9b91f8..2e10424 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -4,15 +4,46 @@ on: [push] jobs: test: + name: Test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Use Node.js + - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '20.x' - name: Install dependencies run: npm ci - name: Run tests - run: npm run test \ No newline at end of file + run: npm run test + + release: + name: Release + runs-on: ubuntu-latest + + permissions: + contents: write # to be able to publish a GitHub release + issues: write # to be able to comment on released issues + pull-requests: write # to be able to comment on released pull requests + id-token: write # to enable use of OIDC for npm provenance + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '20.x' + - name: Install dependencies + run: npm ci + - uses: actions/checkout@v4 + - name: Release + if: branch = master + needs: test + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: npx semantic-release \ No newline at end of file From 7f926cdf2f19a17e0bb89a30f5e79c4c80a67f0b Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Wed, 20 Dec 2023 11:27:11 -0500 Subject: [PATCH 08/11] remove extra 'uses' --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2e10424..e8111e2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -39,7 +39,6 @@ jobs: node-version: '20.x' - name: Install dependencies run: npm ci - - uses: actions/checkout@v4 - name: Release if: branch = master needs: test From 86cf1d344ae7a3866d35511f5f72fb2316e88754 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Wed, 20 Dec 2023 12:00:07 -0500 Subject: [PATCH 09/11] fix indent --- .github/workflows/main.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e8111e2..164ae50 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -42,7 +42,7 @@ jobs: - name: Release if: branch = master needs: test - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - run: npx semantic-release \ No newline at end of file + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + run: npx semantic-release \ No newline at end of file From 75ae2b0c95484bdd0a2b01bb9445e6a042b56db4 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Wed, 20 Dec 2023 12:02:26 -0500 Subject: [PATCH 10/11] move needs to job --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 164ae50..0f9b31a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,6 +20,7 @@ jobs: release: name: Release + needs: test runs-on: ubuntu-latest permissions: @@ -41,7 +42,6 @@ jobs: run: npm ci - name: Release if: branch = master - needs: test env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} From b8f2ae16bc243681afa8bbd26f33dcde7546ecb1 Mon Sep 17 00:00:00 2001 From: Jennifer Shehane Date: Wed, 20 Dec 2023 12:08:26 -0500 Subject: [PATCH 11/11] github ref for master --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0f9b31a..5308a80 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,7 +41,7 @@ jobs: - name: Install dependencies run: npm ci - name: Release - if: branch = master + if: github.ref == 'refs/heads/master' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }}