debut des details de la page. Vu que c'est le troisieme (euh quatrieme?) composant, c'etait un peu plus rapide, mais heureusement que claude est la pour repasser derriere mes erreurs prcq en solo je n'y arriverais pas du tout!
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Gianluca Guarini
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
# bianco.attr
|
||||
|
||||
[![Build Status][ci-image]][ci-url]
|
||||
[![NPM version][npm-version-image]][npm-url]
|
||||
[![NPM downloads][npm-downloads-image]][npm-url]
|
||||
[![MIT License][license-image]][license-url]
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { set, get, has, remove } from 'bianco.attr'
|
||||
|
||||
// set an attribute on a node
|
||||
const img = document.createElement('img')
|
||||
|
||||
set(img, 'width', 100)
|
||||
get(img, 'width') // => '100'
|
||||
has(img, 'width') // => true
|
||||
|
||||
remove(img, 'width')
|
||||
get(img, 'width') // => null
|
||||
```
|
||||
|
||||
[ci-image]:https://img.shields.io/github/workflow/status/biancojs/attr/test?style=flat-square
|
||||
[ci-url]:https://github.com/biancojs/attr/actions
|
||||
|
||||
[license-image]: http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
|
||||
|
||||
[license-url]: LICENSE.txt
|
||||
|
||||
[npm-version-image]: http://img.shields.io/npm/v/bianco.attr.svg?style=flat-square
|
||||
|
||||
[npm-downloads-image]: http://img.shields.io/npm/dm/bianco.attr.svg?style=flat-square
|
||||
|
||||
[npm-url]: https://npmjs.org/package/bianco.attr
|
||||
|
||||
## API
|
||||
|
||||
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
||||
|
||||
#### Table of Contents
|
||||
|
||||
- [set](#set)
|
||||
- [Parameters](#parameters)
|
||||
- [Examples](#examples)
|
||||
- [get](#get)
|
||||
- [Parameters](#parameters-1)
|
||||
- [Examples](#examples-1)
|
||||
- [remove](#remove)
|
||||
- [Parameters](#parameters-2)
|
||||
- [Examples](#examples-2)
|
||||
- [has](#has)
|
||||
- [Parameters](#parameters-3)
|
||||
- [Examples](#examples-3)
|
||||
|
||||
### set
|
||||
|
||||
Set any attribute on a single or a list of DOM nodes
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
|
||||
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object))** either the name of the attribute to set
|
||||
or a list of properties as object key - value
|
||||
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the new value of the attribute (optional)
|
||||
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
import { set } from 'bianco.attr'
|
||||
|
||||
const img = document.createElement('img')
|
||||
|
||||
set(img, 'width', 100)
|
||||
|
||||
// or also
|
||||
set(img, {
|
||||
width: 300,
|
||||
height: 300
|
||||
})
|
||||
```
|
||||
|
||||
Returns **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** the original array of elements passed to this function
|
||||
|
||||
### get
|
||||
|
||||
Get any attribute from a single or a list of DOM nodes
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
|
||||
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to get
|
||||
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
import { get } from 'bianco.attr'
|
||||
|
||||
const img = document.createElement('img')
|
||||
|
||||
get(img, 'width') // => '200'
|
||||
|
||||
// or also
|
||||
get(img, ['width', 'height']) // => ['200', '300']
|
||||
|
||||
// or also
|
||||
get([img1, img2], ['width', 'height']) // => [['200', '300'], ['500', '200']]
|
||||
```
|
||||
|
||||
Returns **([Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String))** list of the attributes found
|
||||
|
||||
### remove
|
||||
|
||||
Remove any attribute from a single or a list of DOM nodes
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
|
||||
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to remove
|
||||
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
import { remove } from 'bianco.attr'
|
||||
|
||||
remove(img, 'width') // remove the width attribute
|
||||
|
||||
// or also
|
||||
remove(img, ['width', 'height']) // remove the width and the height attribute
|
||||
|
||||
// or also
|
||||
remove([img1, img2], ['width', 'height']) // remove the width and the height attribute from both images
|
||||
```
|
||||
|
||||
Returns **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** the original array of elements passed to this function
|
||||
|
||||
### has
|
||||
|
||||
Set any attribute on a single or a list of DOM nodes
|
||||
|
||||
#### Parameters
|
||||
|
||||
- `els` **([HTMLElement](https://developer.mozilla.org/docs/Web/HTML/Element) \| [NodeList](https://developer.mozilla.org/docs/Web/API/NodeList) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** DOM node/s to parse
|
||||
- `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** name or list of attributes to detect
|
||||
|
||||
#### Examples
|
||||
|
||||
```javascript
|
||||
import { has } from 'bianco.attr'
|
||||
|
||||
has(img, 'width') // false
|
||||
|
||||
// or also
|
||||
has(img, ['width', 'height']) // => [false, false]
|
||||
|
||||
// or also
|
||||
has([img1, img2], ['width', 'height']) // => [[false, false], [false, false]]
|
||||
```
|
||||
|
||||
Returns **([boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean) \| [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array))** true or false or an array of boolean values
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
export declare function get<T extends Element>(el: T, attr: string): string | null
|
||||
export declare function get<T extends Element>(el: T, attrs: string[]): string[] | null
|
||||
export declare function get<T extends Element>(els: T[], attr: string): string[] | null
|
||||
export declare function get<T extends Element>(els: T[], attrs: string[]): string[][] | null
|
||||
|
||||
export declare function set<T extends Element>(el: T, attr: string, val: unknown): T
|
||||
export declare function set<T extends Element>(el: T, attrs: Record<string, unknown>): T
|
||||
export declare function set<T extends Element>(els: T[], attr: string, val: unknown): T[]
|
||||
export declare function set<T extends Element>(els: T[], attrs: Record<string, unknown>): T[]
|
||||
|
||||
export declare function remove<T extends Element>(el: T, attr: string | string[]): void
|
||||
export declare function remove<T extends Element>(els: T[], attr: string | string[]): void
|
||||
|
||||
export declare function has<T extends Element>(el: T, attr: string): boolean | null
|
||||
export declare function has<T extends Element>(el: T, attrs: string[]): boolean[] | null
|
||||
export declare function has<T extends Element>(els: T[], attr: string): boolean[] | null
|
||||
export declare function has<T extends Element>(els: T[], attrs: string[]): boolean[][] | null
|
||||
|
||||
export default {
|
||||
get,
|
||||
set,
|
||||
remove,
|
||||
has
|
||||
}
|
||||
|
||||
|
||||
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
var domToArray = require('bianco.dom-to-array');
|
||||
|
||||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
||||
|
||||
var domToArray__default = /*#__PURE__*/_interopDefaultLegacy(domToArray);
|
||||
|
||||
/**
|
||||
* Normalize the return values, in case of a single value we avoid to return an array
|
||||
* @param { Array } values - list of values we want to return
|
||||
* @returns { Array|string|boolean } either the whole list of values or the single one found
|
||||
* @private
|
||||
*/
|
||||
const normalize = values => values.length === 1 ? values[0] : values;
|
||||
|
||||
/**
|
||||
* Parse all the nodes received to get/remove/check their attributes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes
|
||||
* @param { string } method - method that will be used to parse the attributes
|
||||
* @returns { Array|string } result of the parsing in a list or a single value
|
||||
* @private
|
||||
*/
|
||||
function parseNodes(els, name, method) {
|
||||
const names = typeof name === 'string' ? [name] : name;
|
||||
return normalize(domToArray__default["default"](els).map(el => {
|
||||
return normalize(names.map(n => el[method](n)))
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Set any attribute on a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Object } name - either the name of the attribute to set
|
||||
* or a list of properties as object key - value
|
||||
* @param { string } value - the new value of the attribute (optional)
|
||||
* @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { set } from 'bianco.attr'
|
||||
*
|
||||
* const img = document.createElement('img')
|
||||
*
|
||||
* set(img, 'width', 100)
|
||||
*
|
||||
* // or also
|
||||
* set(img, {
|
||||
* width: 300,
|
||||
* height: 300
|
||||
* })
|
||||
*
|
||||
*/
|
||||
function set(els, name, value) {
|
||||
const attrs = typeof name === 'object' ? name : { [name]: value };
|
||||
const props = Object.keys(attrs);
|
||||
|
||||
domToArray__default["default"](els).forEach(el => {
|
||||
props.forEach(prop => el.setAttribute(prop, attrs[prop]));
|
||||
});
|
||||
return els
|
||||
}
|
||||
|
||||
/**
|
||||
* Get any attribute from a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to get
|
||||
* @returns { Array|string } list of the attributes found
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { get } from 'bianco.attr'
|
||||
*
|
||||
* const img = document.createElement('img')
|
||||
*
|
||||
* get(img, 'width') // => '200'
|
||||
*
|
||||
* // or also
|
||||
* get(img, ['width', 'height']) // => ['200', '300']
|
||||
*
|
||||
* // or also
|
||||
* get([img1, img2], ['width', 'height']) // => [['200', '300'], ['500', '200']]
|
||||
*/
|
||||
function get(els, name) {
|
||||
return parseNodes(els, name, 'getAttribute')
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any attribute from a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to remove
|
||||
* @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { remove } from 'bianco.attr'
|
||||
*
|
||||
* remove(img, 'width') // remove the width attribute
|
||||
*
|
||||
* // or also
|
||||
* remove(img, ['width', 'height']) // remove the width and the height attribute
|
||||
*
|
||||
* // or also
|
||||
* remove([img1, img2], ['width', 'height']) // remove the width and the height attribute from both images
|
||||
*/
|
||||
function remove(els, name) {
|
||||
return parseNodes(els, name, 'removeAttribute')
|
||||
}
|
||||
|
||||
/**
|
||||
* Set any attribute on a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to detect
|
||||
* @returns { boolean|Array } true or false or an array of boolean values
|
||||
* @example
|
||||
*
|
||||
* import { has } from 'bianco.attr'
|
||||
*
|
||||
* has(img, 'width') // false
|
||||
*
|
||||
* // or also
|
||||
* has(img, ['width', 'height']) // => [false, false]
|
||||
*
|
||||
* // or also
|
||||
* has([img1, img2], ['width', 'height']) // => [[false, false], [false, false]]
|
||||
*/
|
||||
function has(els, name) {
|
||||
return parseNodes(els, name, 'hasAttribute')
|
||||
}
|
||||
|
||||
var index_next = {
|
||||
get,
|
||||
set,
|
||||
remove,
|
||||
has
|
||||
};
|
||||
|
||||
exports["default"] = index_next;
|
||||
exports.get = get;
|
||||
exports.has = has;
|
||||
exports.remove = remove;
|
||||
exports.set = set;
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
import domToArray from 'bianco.dom-to-array'
|
||||
|
||||
/**
|
||||
* Normalize the return values, in case of a single value we avoid to return an array
|
||||
* @param { Array } values - list of values we want to return
|
||||
* @returns { Array|string|boolean } either the whole list of values or the single one found
|
||||
* @private
|
||||
*/
|
||||
const normalize = values => values.length === 1 ? values[0] : values
|
||||
|
||||
/**
|
||||
* Parse all the nodes received to get/remove/check their attributes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes
|
||||
* @param { string } method - method that will be used to parse the attributes
|
||||
* @returns { Array|string } result of the parsing in a list or a single value
|
||||
* @private
|
||||
*/
|
||||
function parseNodes(els, name, method) {
|
||||
const names = typeof name === 'string' ? [name] : name
|
||||
return normalize(domToArray(els).map(el => {
|
||||
return normalize(names.map(n => el[method](n)))
|
||||
}))
|
||||
}
|
||||
|
||||
/**
|
||||
* Set any attribute on a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Object } name - either the name of the attribute to set
|
||||
* or a list of properties as object key - value
|
||||
* @param { string } value - the new value of the attribute (optional)
|
||||
* @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { set } from 'bianco.attr'
|
||||
*
|
||||
* const img = document.createElement('img')
|
||||
*
|
||||
* set(img, 'width', 100)
|
||||
*
|
||||
* // or also
|
||||
* set(img, {
|
||||
* width: 300,
|
||||
* height: 300
|
||||
* })
|
||||
*
|
||||
*/
|
||||
export function set(els, name, value) {
|
||||
const attrs = typeof name === 'object' ? name : { [name]: value }
|
||||
const props = Object.keys(attrs)
|
||||
|
||||
domToArray(els).forEach(el => {
|
||||
props.forEach(prop => el.setAttribute(prop, attrs[prop]))
|
||||
})
|
||||
return els
|
||||
}
|
||||
|
||||
/**
|
||||
* Get any attribute from a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to get
|
||||
* @returns { Array|string } list of the attributes found
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { get } from 'bianco.attr'
|
||||
*
|
||||
* const img = document.createElement('img')
|
||||
*
|
||||
* get(img, 'width') // => '200'
|
||||
*
|
||||
* // or also
|
||||
* get(img, ['width', 'height']) // => ['200', '300']
|
||||
*
|
||||
* // or also
|
||||
* get([img1, img2], ['width', 'height']) // => [['200', '300'], ['500', '200']]
|
||||
*/
|
||||
export function get(els, name) {
|
||||
return parseNodes(els, name, 'getAttribute')
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any attribute from a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to remove
|
||||
* @returns { HTMLElement|NodeList|Array } the original array of elements passed to this function
|
||||
*
|
||||
* @example
|
||||
*
|
||||
* import { remove } from 'bianco.attr'
|
||||
*
|
||||
* remove(img, 'width') // remove the width attribute
|
||||
*
|
||||
* // or also
|
||||
* remove(img, ['width', 'height']) // remove the width and the height attribute
|
||||
*
|
||||
* // or also
|
||||
* remove([img1, img2], ['width', 'height']) // remove the width and the height attribute from both images
|
||||
*/
|
||||
export function remove(els, name) {
|
||||
return parseNodes(els, name, 'removeAttribute')
|
||||
}
|
||||
|
||||
/**
|
||||
* Set any attribute on a single or a list of DOM nodes
|
||||
* @param { HTMLElement|NodeList|Array } els - DOM node/s to parse
|
||||
* @param { string|Array } name - name or list of attributes to detect
|
||||
* @returns { boolean|Array } true or false or an array of boolean values
|
||||
* @example
|
||||
*
|
||||
* import { has } from 'bianco.attr'
|
||||
*
|
||||
* has(img, 'width') // false
|
||||
*
|
||||
* // or also
|
||||
* has(img, ['width', 'height']) // => [false, false]
|
||||
*
|
||||
* // or also
|
||||
* has([img1, img2], ['width', 'height']) // => [[false, false], [false, false]]
|
||||
*/
|
||||
export function has(els, name) {
|
||||
return parseNodes(els, name, 'hasAttribute')
|
||||
}
|
||||
|
||||
export default {
|
||||
get,
|
||||
set,
|
||||
remove,
|
||||
has
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "bianco.attr",
|
||||
"version": "1.1.1",
|
||||
"description": "Helper to set/get/remove DOM attributes on a list of nodes",
|
||||
"main": "index.js",
|
||||
"jsnext:main": "index.next.js",
|
||||
"module": "index.next.js",
|
||||
"types": "index.d.ts",
|
||||
"scripts": {
|
||||
"prepare": "npm run build && npm test",
|
||||
"lint": "eslint index.next.js test.js rollup.config.js",
|
||||
"build": "rollup -c",
|
||||
"doc": "documentation readme index.next.js -s API",
|
||||
"test": "npm run lint && mocha test.js"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.next.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/biancojs/@.git"
|
||||
},
|
||||
"keywords": [
|
||||
"es6",
|
||||
"es2015"
|
||||
],
|
||||
"author": "Gianluca Guarini <gianluca.guarini@gmail.com> (http://gianlucaguarini.com)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/biancojs/@/issues"
|
||||
},
|
||||
"homepage": "https://github.com/biancojs/@#readme",
|
||||
"devDependencies": {
|
||||
"@gianlucaguarini/eslint-config": "^2.0.0",
|
||||
"eslint": "^8.10.0",
|
||||
"jsdom": "19.0.0",
|
||||
"jsdom-global": "3.0.2",
|
||||
"mocha": "^9.2.1",
|
||||
"rollup": "^2.70.0",
|
||||
"rollup-plugin-node-resolve": "^3.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"bianco.dom-to-array": "^1.1.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user