├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ 4 | dist/ 5 | npm-debug.log* 6 | .DS_Store 7 | .nyc_output 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "4" 3 | - "5" 4 | - "6" 5 | - "7" 6 | sudo: false 7 | language: node_js 8 | script: "npm run test" 9 | # after_success: "npm i -g codecov && npm run coverage && codecov" 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoshua Wuyts 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # extract-html-tag [![stability][0]][1] 2 | [![npm version][2]][3] [![build status][4]][5] 3 | [![downloads][8]][9] [![js-standard-style][10]][11] 4 | 5 | Extract all tags from html 6 | 7 | ## Usage 8 | ```js 9 | var extract = require('extract-html-tag') 10 | var assert = require('assert') 11 | 12 | var html = ` 13 |
16 | ` 17 | var expected = [ 'foo', 'bar', 'bin', 'baz' ] 18 | assert.deepEqual(extract(html), expected, 'array was same') 19 | ``` 20 | 21 | ## API 22 | ### `classes = extract(html)` 23 | Get an array of classes for the given string. Uses a regex so safe to run on 24 | non-html strings too. Returns an empty array if no matches are found. 25 | 26 | ## License 27 | [MIT](https://tldrlegal.com/license/mit-license) 28 | 29 | [0]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square 30 | [1]: https://nodejs.org/api/documentation.html#documentation_stability_index 31 | [2]: https://img.shields.io/npm/v/extract-html-tag.svg?style=flat-square 32 | [3]: https://npmjs.org/package/extract-html-tag 33 | [4]: https://img.shields.io/travis/yoshuawuyts/extract-html-tag/master.svg?style=flat-square 34 | [5]: https://travis-ci.org/yoshuawuyts/extract-html-tag 35 | [6]: https://img.shields.io/codecov/c/github/yoshuawuyts/extract-html-tag/master.svg?style=flat-square 36 | [7]: https://codecov.io/github/yoshuawuyts/extract-html-tag 37 | [8]: http://img.shields.io/npm/dm/extract-html-tag.svg?style=flat-square 38 | [9]: https://npmjs.org/package/extract-html-tag 39 | [10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 40 | [11]: https://github.com/feross/standard 41 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | 3 | var findHtml = /<([^\s>]+)(\s|>)+/g 4 | module.exports = extract 5 | 6 | function extract (html) { 7 | assert.equal(typeof html, 'string', 'extract-html-tag: html should be type string') 8 | 9 | var arr = [] 10 | var res 11 | 12 | while (true) { 13 | res = findHtml.exec(html) 14 | if (!res) break 15 | arr = arr.concat(res[1].split(' ')).reduce(filter, []) 16 | } 17 | 18 | if (arr.length <= 1) return arr 19 | return arr.filter(function (value, index) { 20 | return arr.indexOf(value) === index 21 | }) 22 | } 23 | 24 | function filter (arr, item) { 25 | if (!/^\//.test(item)) arr.push(item) 26 | return arr 27 | } 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "extract-html-tag", 3 | "description": "Extract all tags from html", 4 | "repository": "yoshuawuyts/extract-html-tag", 5 | "version": "1.0.1", 6 | "scripts": { 7 | "deps": "dependency-check . && dependency-check . --extra --no-dev", 8 | "start": "node .", 9 | "test": "standard && npm run deps && node test" 10 | }, 11 | "dependencies": {}, 12 | "devDependencies": { 13 | "dependency-check": "^2.9.1", 14 | "standard": "^10.0.2", 15 | "tape": "^4.7.0" 16 | }, 17 | "keywords": [ 18 | "html", 19 | "classname", 20 | "extract", 21 | "regex" 22 | ], 23 | "license": "MIT" 24 | } 25 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var extract = require('./') 3 | 4 | tape('extracts classes from html', function (assert) { 5 | var html = ` 6 | 9 | ` 10 | var expected = [ 'div', 'p' ] 11 | assert.deepEqual(extract(html), expected, 'array was same') 12 | assert.end() 13 | }) 14 | 15 | tape('returns empty array if no matches', function (assert) { 16 | var html = `hello world` 17 | var expected = [] 18 | assert.deepEqual(extract(html), expected, 'array was same') 19 | assert.end() 20 | }) 21 | 22 | tape('removes duplicates', function (assert) { 23 | var html = ` 24 | 27 | ` 28 | var expected = [ 'div' ] 29 | assert.deepEqual(extract(html), expected, 'array was same') 30 | assert.end() 31 | }) 32 | 33 | tape('matches fancy tags', function (assert) { 34 | var html = ` 35 | 36 |