{"name":"posthtml-attrs-parser","version":"1.1.1","description":"PostHTML helper that provides a better API to work with tag attributes.","type":"module","exports":{".":{"import":"./dist/index.mjs","require":"./dist/index.cjs"}},"main":"dist/index.cjs","module":"./dist/index.mjs","author":{"name":"Kirill Maltsev","email":"maltsevkirill@gmail.com"},"license":"MIT","scripts":{"dev":"vitest","release":"npx np","build":"unbuild","prepack":"unbuild","lint":"biome lint ./lib","pretest":"npm run lint","test":"vitest run --coverage"},"keywords":["posthtml","posthtml-plugin","html","post-processor","parser","attributes"],"devDependencies":{"@biomejs/biome":"1.8.3","@vitest/coverage-v8":"^2.0.1","posthtml":"^0.16.6","unbuild":"^2.0.0","vitest":"^2.0.1"},"engines":{"node":">=16.0.0"},"repository":{"type":"git","url":"git+https://github.com/posthtml/posthtml-attrs-parser.git"},"bugs":{"url":"https://github.com/posthtml/posthtml-attrs-parser/issues"},"homepage":"https://github.com/posthtml/posthtml-attrs-parser","_id":"posthtml-attrs-parser@1.1.1","gitHead":"e52c85660b00fce8b5890c1d2e3d1dff0bcc7c57","_nodeVersion":"20.10.0","_npmVersion":"10.2.4","dist":{"integrity":"sha512-yWD1JGaLr5ENrEhUxjqEuSjTkYhX3VVK3ULNeh9Xs+Jqr3c0UGVkerxJg55Uyu036jm5nMsG6rJaBtE5YSf+lw==","shasum":"fc5c3d25e6be4fcf309db6f7ecfd7ec50ebbfbee","tarball":"http://registry-npm.ywwl.com/posthtml-attrs-parser/download/posthtml-attrs-parser-1.1.1.tgz","fileCount":5,"unpackedSize":11275,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD/8Xqgl9xI/Hfoe1MX0+ier9wCND5sjgEASB7QO3RTGgIgfLS5/VIPmLy7i2B8GfzCRegG3L4eJIaTRBXjbuQlQQw="}],"size":3477,"noattachment":false,"key":"/posthtml-attrs-parser/-/posthtml-attrs-parser-1.1.1.tgz"},"_npmUser":{"name":"cossssmin","email":"hellocosmin@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/posthtml-attrs-parser_1.1.1_1721048933307_0.6437221863249336"},"_hasShrinkwrap":false,"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":1721048933475,"maintainers":[{"name":"scrum","email":"scrum@list.ru"},{"name":"maltsev","email":"maltsevkirill@gmail.com"},{"name":"cossssmin","email":"hellocosmin@gmail.com"}],"_cnpm_publish_time":1721048933475,"dist-tags":{"latest":"1.1.2","old-stable":"0.1.2"}}