Skip to content

Commit 0ed69d3

Browse files
committed
⚡ Energize
0 parents  commit 0ed69d3

30 files changed

+9263
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_size = 2
6+
indent_style = space
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.nyc_output
3+
*.log

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- stable
4+
- 12
5+
- 10

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Cosmin Popovici
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
13+
all 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
21+
THE SOFTWARE.

README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<div align="center">
2+
<img width="150" height="150" title="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
3+
<h1>Extra Attributes</h1>
4+
<p>Add new attributes to elements in your HTML</p>
5+
6+
[![Version][npm-version-shield]][npm]
7+
[![License][license-shield]][license]
8+
[![Build][travis-ci-shield]][travis-ci]
9+
[![Downloads][npm-stats-shield]][npm-stats]
10+
</div>
11+
12+
## Introduction
13+
14+
This PostHTML plugin can add extra attributes to elements in your HTML:
15+
16+
- does not overwrite existing attributes (configurable)
17+
- appends class names to existing ones
18+
- supports a variety of CSS-like selectors
19+
20+
## Installation
21+
22+
```
23+
$ npm i posthtml posthtml-extra-attributes
24+
```
25+
26+
## Usage
27+
28+
```js
29+
const posthtml = require('posthtml')
30+
const addAttributes = require('posthtml-extra-attributes')
31+
32+
posthtml([
33+
addAttributes({
34+
attributes: {
35+
div: {
36+
class: 'new',
37+
id: 'new'
38+
}
39+
}
40+
})
41+
])
42+
.process('<div class="test">Test</div>')
43+
.then(result => console.log(result.html))
44+
45+
// <div class="test new" id="new">Test</div>
46+
```
47+
48+
## Options
49+
50+
### `attributes`
51+
52+
Type: `object`\
53+
Default: `{}`
54+
55+
An object defining which elements should get what attributes.
56+
57+
Elements can be any [posthtml-match-helper](https://github.com/phloe/posthtml-match-helper) selector.
58+
59+
#### Select by tag
60+
61+
Add `id="new"` to all `<div>` tags:
62+
63+
```js
64+
const attributes = {
65+
div: {
66+
id: 'new'
67+
},
68+
}
69+
```
70+
71+
#### Select by class
72+
73+
Add `editable="true"` to all elements with a `test` class:
74+
75+
```js
76+
const attributes = {
77+
'.test': {
78+
'editable': true
79+
},
80+
}
81+
```
82+
83+
#### Select by id
84+
85+
Add `class="new"` to any element with `id="test"`:
86+
87+
```js
88+
const attributes = {
89+
'#test': {
90+
class: 'new'
91+
},
92+
}
93+
```
94+
95+
If the element already has a `class` attribute, the value will be appended:
96+
97+
```html
98+
<div id="test" class="test">Test</div>
99+
```
100+
101+
... will result in:
102+
103+
```html
104+
<div id="test" class="test new">Test</div>
105+
```
106+
107+
#### Select by attribute
108+
109+
Adds `aria-roledescription="slide"` to all elements with a `role` attribute:
110+
111+
```js
112+
const attributes = {
113+
'[role]': {
114+
'aria-roledescription': 'slide'
115+
},
116+
}
117+
```
118+
119+
#### Select multiple tags
120+
121+
Add multiple attributes to multiple elements in one go:
122+
123+
```js
124+
const attributes = {
125+
'div, p': {
126+
class: 'test',
127+
},
128+
'div[role=alert], section.alert': {
129+
class: 'alert'
130+
},
131+
}
132+
```
133+
134+
### `overwrite`
135+
136+
Type: `boolean`\
137+
Default: `false`
138+
139+
By default, the plugin will not overwrite existing attribute values.
140+
141+
Set this option to `true` to enable attribute value overwriting:
142+
143+
```js
144+
posthtml([
145+
addAttributes({
146+
attributes: {
147+
div: {
148+
id: 'new'
149+
}
150+
},
151+
overwrite: true
152+
})
153+
])
154+
.process('<div id="test">Test</div>')
155+
.then(result => console.log(result.html))
156+
157+
// <div id="new">Test</div>
158+
```
159+
160+
[npm]: https://www.npmjs.com/package/posthtml-extra-attributes
161+
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-extra-attributes.svg
162+
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-extra-attributes
163+
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-extra-attributes.svg
164+
[travis-ci]: https://travis-ci.org/posthtml/posthtml-extra-attributes/
165+
[travis-ci-shield]: https://img.shields.io/travis/posthtml/posthtml-extra-attributes/master.svg
166+
[license]: ./LICENSE
167+
[license-shield]: https://img.shields.io/npm/l/posthtml-extra-attributes.svg

lib/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const omit = require('lodash.omit')
2+
const matchHelper = require('posthtml-match-helper')
3+
4+
module.exports = (options = {}) => tree => {
5+
options.attributes = options.attributes || {}
6+
options.overwrite = options.overwrite || false
7+
8+
const process = node => {
9+
const attributes = Object.keys(options.attributes)
10+
const matchers = attributes.map(attribute => matchHelper(attribute))
11+
12+
attributes.forEach((key, i) => {
13+
tree.match(matchers[i], node => {
14+
// For each attribute that we want to add...
15+
Object.entries(options.attributes[key]).forEach(([k, v]) => {
16+
if (k === 'class' && node.attrs && node.attrs.class) {
17+
node.attrs.class = [...(new Set([...node.attrs.class.split(' '), ...v.split(' ')]))].join(' ')
18+
} else {
19+
const attributes = options.overwrite ? options.attributes[key] : omit(options.attributes[key], Object.keys(node.attrs || {}))
20+
node.attrs = {...node.attrs, ...attributes}
21+
}
22+
})
23+
24+
return node
25+
})
26+
})
27+
28+
return node
29+
}
30+
31+
return tree.walk(process)
32+
}

0 commit comments

Comments
 (0)