{"name":"posthtml-attrs-parser","version":"1.0.1","keywords":["posthtml","posthtml-plugin","html","post-processor","parser","attributes"],"author":{"name":"Kirill Maltsev","email":"maltsevkirill@gmail.com"},"license":"MIT","_id":"posthtml-attrs-parser@1.0.1","homepage":"https://github.com/posthtml/posthtml-attrs-parser","bugs":{"url":"https://github.com/posthtml/posthtml-attrs-parser/issues"},"dist":{"shasum":"d2435b0cff4c65dfe1dd55055b66a195c21d886b","tarball":"http://registry-npm.ywwl.com/posthtml-attrs-parser/download/posthtml-attrs-parser-1.0.1.tgz","fileCount":6,"integrity":"sha512-GEjJu/PApFm56HEQ8U2M6v5YQglS05FhXKftuuAhP8loNM6Peu1Njt1ykXWJdZ0Nq770YLql9zhvwbTW/szpWw==","signatures":[{"sig":"MEYCIQCzrIrw4omr9A0sEOIZgM9lG+5qzPlpK0WBm7eALiQ77AIhAMBb6Bp/NfUP1d5yfiBFDXbOWQKadsfy7/6rZRk6nSlo","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":8750,"size":3452,"noattachment":false,"key":"/posthtml-attrs-parser/-/posthtml-attrs-parser-1.0.1.tgz"},"main":"lib/index.js","type":"module","engines":{"node":">=16.0.0"},"gitHead":"a0e4efcb0d21aee0af6eba42fecd5d0bffef5362","scripts":{"dev":"vitest","lint":"eslint lib/*.js","test":"vitest run --coverage","pretest":"npm run lint","release":"np"},"_npmUser":{"name":"cossssmin","email":"hellocosmin@gmail.com"},"repository":{"url":"git+https://github.com/posthtml/posthtml-attrs-parser.git","type":"git"},"_npmVersion":"10.2.4","description":"PostHTML helper that provides a better API to work with tag attributes.","directories":{},"_nodeVersion":"20.10.0","_hasShrinkwrap":false,"devDependencies":{"np":"^9.2.0","eslint":"^8.56.0","vitest":"^1.2.1","posthtml":"^0.16.6","@vitest/coverage-v8":"^1.2.1"},"_npmOperationalInternal":{"tmp":"tmp/posthtml-attrs-parser_1.0.1_1706025868101_0.3288748821658569","host":"s3://npm-registry-packages"},"readme":"<div align=\"center\">\n  <img width=\"150\" height=\"150\" alt=\"PostHTML\" src=\"https://posthtml.github.io/posthtml/logo.svg\">\n  <h1>posthtml-attrs-parser</h1>\n  <p>PostHTML plugin for parsing HTML attributes</p>\n\n  [![Version][npm-version-shield]][npm]\n  [![Build][github-ci-shield]][github-ci]\n  [![License][license-shield]][license]\n  [![Downloads][npm-stats-shield]][npm-stats]\n</div>\n\n[npm]: https://www.npmjs.com/package/posthtml-attrs-parser\n[npm-version-shield]: https://img.shields.io/npm/v/posthtml-attrs-parser\n[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-attrs-parser\n[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-attrs-parser.svg\n[github-ci]: https://github.com/posthtml/posthtml-attrs-parser/actions/workflows/nodejs.yml\n[github-ci-shield]: https://github.com/posthtml/posthtml-attrs-parser/actions/workflows/nodejs.yml/badge.svg\n[license]: ./license\n[license-shield]: https://img.shields.io/npm/l/posthtml-attrs-parser.svg\n\nA [PostHTML](https://github.com/posthtml/posthtml) helper plugin that provides a better API for working with tag attributes.\n\n## Usage\n```js\nimport posthtml from 'posthtml';\nimport parseAttrs from 'posthtml-attrs-parser';\n\nposthtml()\n  .use(function (tree) {\n    const div = tree[0];\n    const attrs = parseAttrs(div.attrs);\n\n    attrs.style['font-size'] = '15px';\n    attrs.class.push('title-sub');\n\n    // Compose attributes back to PostHTML-compatible format\n    div.attrs = attrs.compose();\n  })\n  .process('<div class=\"title\" style=\"font-size: 14px\">Hello!</div>')\n  .then(function (result) {\n    console.log(result.html);\n  });\n\n// <div class=\"title title-sub\" style=\"font-size: 15px\">Hello!</div>\n```\n\nBoth ESM and CJS exports are provided, you can use the plugin in CJS too:\n\n```js\nconst posthtml = require('posthtml');\nconst parseAttrs = require('posthtml-attrs-parser');\n\n// ...\n```\n\n## Attributes\n\nOnly `style` and `class` attributes are parsed by default (as object and array, respectively). For other attributes, the parsing rules should be specified (see [Custom parsing rule](#custom-parsing-rule) below).\n\n### Default attributes\n\n#### `style`\n\n```html\n<div style=\"color: red; font-size: 14px; color: blue\"></div>\n```\n```js\nconst attrs = parseAttrs(div.attrs);\n\nconsole.log(attrs.style);\n/*\n{\n  // If there are several properties with the same name,\n  // the values are packed in array\n  'color': ['red', 'blue'],\n  'font-size': '14px'\n}\n*/\n```\n\n#### `class`\n\n```html\n<div class=\"title title-sub\"></div>\n```\n```js\nconst attrs = parseAttrs(div.attrs);\n\nconsole.log(attrs.class);\n// ['title', 'title-sub']\n```\n\n### Custom parsing rule\n\nYou may also define the parsing rule for other attributes.\n\n#### Array-like attribute\n\n```html\n<div data-ids=\"1  2 4 5   6\"></div>\n```\n```js\nconst attrs = parseAttrs(div.attrs, {\n  rules: {\n  'data-ids': {\n    delimiter: /\\s+/,\n    // Optional parameter for stringifying attribute's values\n    // If not set, glue = delimiter\n    glue: ' '\n  }\n}\n});\n\nconsole.log(attrs['data-ids']);\n// ['1', '2', '4', '5', '6']\n\nconsole.log(attrs.compose()['data-ids']);\n// 1 2 3 4 5 6\n```\n\n#### Object-like attribute\n\n```html\n<div data-config=\"TEST=1;ENV=debug;PATH=.\"></div>\n```\n```js\nconst attrs = parseAttrs(div.attrs, {\n  rules: {\n    'data-config': {\n      // Delimiter for key-value pairs\n      delimiter: ';',\n      // Delimiter for a key-value\n      keyDelimiter: '=',\n      // Optional parameter for stringifying key-value pairs\n      // If not set, keyGlue = keyDelimiter\n      glue: '; ',\n      // Optional parameter for stringifying a key-value\n      // If not set, glue = delimiter\n      keyGlue: ' = '\n    }\n  }\n});\n\nconsole.log(attrs['data-config']);\n// {TEST: '1', ENV: 'debug', PATH: '.'}\n\nconsole.log(attrs.compose()['data-config']);\n// TEST = 1; ENV = debug; PATH = .\n```\n","publish_time":1706025868245,"maintainers":[{"name":"scrum","email":"scrum@list.ru"},{"name":"maltsev","email":"maltsevkirill@gmail.com"},{"name":"cossssmin","email":"hellocosmin@gmail.com"}],"_cnpm_publish_time":1706025868245,"dist-tags":{"latest":"1.1.2","old-stable":"0.1.2"}}