{"name":"posthtml-attrs-parser","version":"0.1.1","keywords":["posthtml","posthtml-plugin","html","postproccessor","parser"],"author":{"name":"Kirill Maltsev","email":"maltsevkirill@gmail.com"},"license":"MIT","_id":"posthtml-attrs-parser@0.1.1","homepage":"https://github.com/maltsev/posthtml-attrs-parser","bugs":{"url":"https://github.com/maltsev/posthtml-attrs-parser/issues"},"dist":{"shasum":"cc33e00155fb99ba96f67e25e330461f05742ac8","tarball":"http://registry-npm.ywwl.com/posthtml-attrs-parser/download/posthtml-attrs-parser-0.1.1.tgz","integrity":"sha512-ZFZfTIb74uHFdn80+7DyP+qrryDX/Bb7Q2rUaByh6iUJ1HlKGSxkChKORpwIUHjChJDzcKEtnyXxzXzcjpH9CQ==","signatures":[{"sig":"MEUCIB08Qay26aCGCciO8GUVHQIdB1BiKopF5RXXrp7NOuOlAiEAuj5Ewq6JaR1z6EB8sv5zFJBMfWFn/RLpfHchGtczZWM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"size":3878,"noattachment":false,"key":"/posthtml-attrs-parser/-/posthtml-attrs-parser-0.1.1.tgz"},"main":"index.js","_from":".","babel":{"presets":["es2015"]},"_shasum":"cc33e00155fb99ba96f67e25e330461f05742ac8","gitHead":"085bfc2ea5d1d004cc8b4f091420f072425fb506","scripts":{"lint":"eslint *.js lib/*.es6 test/","test":"_mocha --compilers js:babel-core/register","compile":"rm -f lib/*.js && node_modules/.bin/babel -d lib/ lib/","pretest":"npm run lint && npm run compile","prepublish":"npm run compile"},"_npmUser":{"name":"maltsev","email":"maltsevkirill@gmail.com"},"repository":{"url":"git://github.com/maltsev/posthtml-attrs-parser.git","type":"git"},"_npmVersion":"2.7.6","description":"PostHTML helper that gives a better API to work with tag's attrs","directories":{},"_nodeVersion":"1.7.1","dependencies":{"object-assign":"^4.0.1"},"devDependencies":{"mocha":"^2.3.4","eslint":"^1.10.3","expect":"^1.13.0","posthtml":"^0.8.0","babel-cli":"^6.3.15","babel-core":"^6.3.15","babel-eslint":"^4.1.6","babel-preset-es2015":"^6.3.13"},"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":1451946300605,"maintainers":[{"name":"scrum","email":"scrum@list.ru"},{"name":"maltsev","email":"maltsevkirill@gmail.com"},{"name":"cossssmin","email":"hellocosmin@gmail.com"}],"_cnpm_publish_time":1451946300605,"dist-tags":{"latest":"1.1.2","old-stable":"0.1.2"}}