forked from ericclemmons/unique-selector
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow selector traits to be conditionally filtered (#8)
- Loading branch information
1 parent
993a2b9
commit 74c9036
Showing
9 changed files
with
188 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
/** | ||
* Returns the {attr} selector of the element | ||
* @param { String } selectorType - The attribute selector to return. | ||
* @param { String } attributes - The attributes of the element. | ||
* @param { Element } el - The element. | ||
* @param { String } attribute - The attribute name. | ||
* @return { String | null } - The {attr} selector of the element. | ||
*/ | ||
|
||
export const getAttribute = ( selectorType, attributes ) => | ||
export const getAttributeSelector = ( el, attribute ) => | ||
{ | ||
for ( let i = 0; i < attributes.length; i++ ) | ||
{ | ||
// extract node name + value | ||
const { nodeName, value } = attributes[ i ]; | ||
const attributeValue = el.getAttribute(attribute) | ||
|
||
// if this matches our selector | ||
if ( nodeName === selectorType ) | ||
{ | ||
if ( value ) | ||
{ | ||
// if we have value that needs quotes | ||
return `[${nodeName}="${value}"]`; | ||
} | ||
if (attributeValue === null) { | ||
return null | ||
} | ||
|
||
return `[${nodeName}]`; | ||
} | ||
if (attributeValue) { | ||
// if we have value that needs quotes | ||
return `[${attribute}="${attributeValue}"]`; | ||
} | ||
|
||
return null; | ||
return `[${attribute}]`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,16 @@ | ||
/** | ||
* Returns the Tag of the element | ||
* @param { Object } element | ||
* @param { Function } filter | ||
* @return { String } | ||
*/ | ||
export function getTag( el ) | ||
export function getTag( el, filter ) | ||
{ | ||
return el.tagName.toLowerCase().replace(/:/g, '\\:'); | ||
const tagName = el.tagName.toLowerCase().replace(/:/g, '\\:') | ||
|
||
if (filter && !filter('tag', 'tag', tagName)) { | ||
return null; | ||
} | ||
|
||
return tagName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.