-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add special [attribute-name] param value
- Loading branch information
1 parent
8e47996
commit 88c365f
Showing
3 changed files
with
78 additions
and
22 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,28 +1,53 @@ | ||
import { hit } from './hit'; | ||
import { logMessage } from './log-message'; | ||
import { hit } from './hit'; | ||
|
||
/** | ||
* Sets attribute with given value to given element. | ||
* | ||
* @param elem | ||
* @param attribute | ||
* @param value | ||
* @returns | ||
*/ | ||
const defaultAttributeSetter = ( | ||
elem: Element, | ||
attribute: string, | ||
value: string, | ||
): void => elem.setAttribute(attribute, value); | ||
|
||
/** | ||
* Sets attribute with given value to all elements matching given selector | ||
* | ||
* @param source source object | ||
* @param source source | ||
* @param selector CSS selector | ||
* @param attr attribute name to set | ||
* @param attribute attribute name to set | ||
* @param value attribute value to set | ||
* @param attributeSetter function to apply to each element, | ||
* defaults to native .setAttribute | ||
*/ | ||
export const setAttributeToSelection = ( | ||
export const setAttributeBySelector = ( | ||
source: Source, | ||
selector: string, | ||
attr: string, | ||
attribute: string, | ||
value: string, | ||
attributeSetter = defaultAttributeSetter, | ||
): void => { | ||
let elements; | ||
try { | ||
elements = document.querySelectorAll(selector); | ||
} catch { | ||
logMessage(source, `Failed to find elements matching selector "${selector}"`); | ||
return; | ||
} | ||
|
||
if (!elements || elements.length === 0) { | ||
return; | ||
} | ||
|
||
try { | ||
const nodes = document.querySelectorAll(selector); | ||
if (nodes.length === 0) { | ||
return; | ||
} | ||
nodes.forEach((node) => node.setAttribute(attr, value)); | ||
elements.forEach((elem) => attributeSetter(elem, attribute, value)); | ||
hit(source); | ||
} catch { | ||
logMessage(source, `Failed to set attribute "${attr}" to "${value}" on "${selector}"`); | ||
logMessage(source, `Failed to set [${attribute}="${value}"] to each of selected elements.`); | ||
} | ||
}; |
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