Last change
on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | exports.name = 'cleanupAttrs';
|
---|
| 4 | exports.type = 'visitor';
|
---|
| 5 | exports.active = true;
|
---|
| 6 | exports.description =
|
---|
| 7 | 'cleanups attributes from newlines, trailing and repeating spaces';
|
---|
| 8 |
|
---|
| 9 | const regNewlinesNeedSpace = /(\S)\r?\n(\S)/g;
|
---|
| 10 | const regNewlines = /\r?\n/g;
|
---|
| 11 | const regSpaces = /\s{2,}/g;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Cleanup attributes values from newlines, trailing and repeating spaces.
|
---|
| 15 | *
|
---|
| 16 | * @author Kir Belevich
|
---|
| 17 | *
|
---|
| 18 | * @type {import('../lib/types').Plugin<{
|
---|
| 19 | * newlines?: boolean,
|
---|
| 20 | * trim?: boolean,
|
---|
| 21 | * spaces?: boolean
|
---|
| 22 | * }>}
|
---|
| 23 | */
|
---|
| 24 | exports.fn = (root, params) => {
|
---|
| 25 | const { newlines = true, trim = true, spaces = true } = params;
|
---|
| 26 | return {
|
---|
| 27 | element: {
|
---|
| 28 | enter: (node) => {
|
---|
| 29 | for (const name of Object.keys(node.attributes)) {
|
---|
| 30 | if (newlines) {
|
---|
| 31 | // new line which requires a space instead of themselve
|
---|
| 32 | node.attributes[name] = node.attributes[name].replace(
|
---|
| 33 | regNewlinesNeedSpace,
|
---|
| 34 | (match, p1, p2) => p1 + ' ' + p2
|
---|
| 35 | );
|
---|
| 36 | // simple new line
|
---|
| 37 | node.attributes[name] = node.attributes[name].replace(
|
---|
| 38 | regNewlines,
|
---|
| 39 | ''
|
---|
| 40 | );
|
---|
| 41 | }
|
---|
| 42 | if (trim) {
|
---|
| 43 | node.attributes[name] = node.attributes[name].trim();
|
---|
| 44 | }
|
---|
| 45 | if (spaces) {
|
---|
| 46 | node.attributes[name] = node.attributes[name].replace(
|
---|
| 47 | regSpaces,
|
---|
| 48 | ' '
|
---|
| 49 | );
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | },
|
---|
| 53 | },
|
---|
| 54 | };
|
---|
| 55 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.