Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiattran committed Dec 17, 2016
0 parents commit 43ea04a
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.idea
npm-debug.log
coverage
19 changes: 19 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bitwise": true,
"browser": true,
"curly": true,
"eqeqeq": true,
"esnext": true,
"latedef": true,
"noarg": true,
"node": true,
"strict": true,
"undef": true,
"unused": true,
"globals": {
"require": true,
"it": true,
"describe": true,
"before": true
}
}
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sudo: false
language: node_js
node_js:
- 'stable'
- 'iojs'
- v5
- v4

script: 'npm run cover'

after_success:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# html-attribute-parser

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

## Installation

```
npm install --save html-attribute-parser
```

## Usage

```js
var htmlAttributeParser = require('html-attribute-parser');

let text = '<button attr class="btn btn-primary" id="submit" on-click="callFunction()">Hello world</button>';

let tag = htmlAttributeParser(text)
console.log(tag);

/* return
{ tagName: 'button',
attributes:
{ attr: true,
class: 'btn btn-primary',
id: 'submit',
'on-click': 'callFunction()' },
value: 'Hello world' }
*/
```

## Getting To Know Yeoman

Yeoman has a heart of gold. He&#39;s a person with feelings and opinions, but he&#39;s very easy to work with. If you think he&#39;s too opinionated, he can be easily convinced. Feel free to [learn more about him](http://yeoman.io/).

## Created with
[Yeoman](https://npmjs.org/package/yo) and [Generator-simple-package](https://npmjs.org/package/generator-simple-package)

## License
MIT © [Nghia Tran]()

[npm-image]: https://badge.fury.io/js/html-attribute-parser.svg
[npm-url]: https://npmjs.org/package/html-attribute-parser
[travis-image]: https://travis-ci.org/nghiattran/html-attribute-parser.svg?branch=master
[travis-url]: https://travis-ci.org/nghiattran/html-attribute-parser
[daviddm-image]: https://david-dm.org/nghiattran/html-attribute-parser.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/nghiattran/html-attribute-parser
[coveralls-image]: https://coveralls.io/repos/nghiattran/html-attribute-parser/badge.svg
[coveralls-url]: https://coveralls.io/github/nghiattran/html-attribute-parser
15 changes: 15 additions & 0 deletions editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
# Change these settings to your own preference
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
95 changes: 95 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

let Source = require('source-component');

module.exports = function(text) {
return parse(text);
};

function stringParser(source) {
const stringSym = source.currentChar();
let char = source.nextChar();
let string = '';
while (char) {
if (char === stringSym && source.peek(-1) !== '\\') {
source.nextChar();
return string;
} else {
string += char;
}

char = source.nextChar();
}
}

function parseName(source) {
let name = '';

if (source.currentChar() !== '<') {
throw Error('This is not a HTML tag. HTML tag has to start with "<"');
}

let char = source.nextChar();
while (char) {
if (char === ' ') {
source.nextChar();
return name;
} else {
name += char;
}
char = source.nextChar();
}
return name;
}

function parseAttributes(source) {
let field = '';
let char = source.currentChar();
let attributes = {};

while (char && char !== '>') {
if (char === ' ') {
if (!attributes[field]) {attributes[field] = true}
field = '';
} else {
if (char === '=') {
source.nextChar();
attributes[field] = stringParser(source);
char = source.currentChar();
continue;
} else {
field += char;
}
}
char = source.nextChar();
}

return attributes
}

function parseValue(source) {
let char = source.currentChar();

if (char === '>') {
char = source.nextChar();
}

let value = '';
while (char && char !== '<') {
value += char;
char = source.nextChar();
}
return value;
}

function parse(text) {
let field = '';
let source = new Source(text);


return {
tagName: parseName(source),
attributes: parseAttributes(source),
value: parseValue(source)
}
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Nghia Tran <nghiattran3@gmail.com> ()

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "html-attribute-parser",
"version": "0.0.0",
"description": "Attribute parser for html tag",
"main": "index.js",
"scripts": {
"test": "mocha",
"cover": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec"
},
"engines": {
"node": ">=0.10.0"
},
"files": [
"index.js"
],
"repository": "nghiattran/html-attribute-parser",
"author": {
"name": "Nghia Tran",
"email": "nghiattran3@gmail.com",
"url": ""
},
"license": "MIT",
"homepage": "",
"dependencies": {
"source-component": "^1.0.0"
},
"devDependencies": {
"coveralls": "2.11.2",
"istanbul": "0.3.14",
"mocha": "2.2.5",
"mocha-lcov-reporter": "0.0.2"
}
}
47 changes: 47 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var assert = require('assert');
var htmlAttributeParser = require('./');

let text = '<p attr class="animated infinite" ng-class="main.classAnimation">Hello world</p>';
let text2 = '<a href="Lecture/Lec3.ppt">Lecture 3</a>';

describe('test', function(){
it('Assert parsing tag name', function() {
assert(htmlAttributeParser(text)['tagName'] = 'p');
});

it('Assert parsing tag name 2', function() {
assert(htmlAttributeParser(text2)['tagName'] = 'a');
});

it('Assert parsing tag attributes', function() {
const attributes = htmlAttributeParser(text)['attributes'];
assert(attributes['attr']);
assert(attributes['class'] === 'animated infinite');
assert(attributes['ng-class'] === 'main.classAnimation');
});

it('Assert parsing tag attributes 2', function() {
const attributes = htmlAttributeParser(text2)['attributes'];
assert(attributes['href'] === 'Lecture/Lec3.ppt');
});

it('Assert parsing tag attributes: non-existing tag', function() {
const attributes = htmlAttributeParser(text)['attributes'];
assert(attributes['sometag'] === undefined);
});

it('Assert parsing tag attributes 2: non-existing tag', function() {
const attributes = htmlAttributeParser(text2)['attributes'];
assert(attributes['sometag'] === undefined);
});

it('Assert parsing tag value', function() {
assert(htmlAttributeParser(text)['value'] === 'Hello world');
});

it('Assert parsing tag value 2', function() {
assert(htmlAttributeParser(text2)['value'] === 'Lecture 3');
});
});

0 comments on commit 43ea04a

Please sign in to comment.