create-element-basic
Advanced tools
Comparing version 0.1.0 to 0.2.0
46
index.js
/*! | ||
* create-element-basic <https://github.com/akileez/create-element-basic> | ||
* | ||
* Copyright (c) 2014 Keith Williams, contributors. | ||
* Copyright (c) 2014-2015 Keith Williams, contributors. | ||
* Licensed under the MIT license. | ||
@@ -13,22 +13,46 @@ * | ||
function openTag (type, closing, attr) { | ||
var html = ['<' + type]; | ||
var emptyTags = [ | ||
'link', | ||
'track', | ||
'param', | ||
'area', | ||
'command', | ||
'col', | ||
'base', | ||
'meta', | ||
'hr', | ||
'source', | ||
'img', | ||
'keygen', | ||
'br', | ||
'wbr', | ||
'input' | ||
] | ||
function openTag (type, closing, attr) { | ||
var html = ['<' + type] | ||
for (var prop in attr) { | ||
// A falsy value is used to remove the attribute. | ||
// EG: attr[false] to remove, attr['false'] to add | ||
if (attr[prop]) { | ||
html.push(prop + '="' + attr[prop] + '"'); | ||
} | ||
if (attr[prop]) html.push(prop + '="' + attr[prop] + '"') | ||
} | ||
return html.join(' ') + (!closing ? ' /' : '') + '>'; | ||
return html.join(' ') + (!closing ? ' /' : '') + '>' | ||
} | ||
function closeTag (type) { | ||
return '</' + type + '>'; | ||
return '</' + type + '>' | ||
} | ||
function createElement(type, closing, attr, contents) { | ||
return openTag(type, closing, attr) + (closing ? (contents || '') + closeTag(type) : ''); | ||
function createElement(type, attr, contents) { | ||
var closing = emptyTags.filter(isClosingTag) | ||
function isClosingTag (val, idx, arr) { | ||
if (val === type) return false | ||
return true | ||
} | ||
return openTag(type, closing, attr) + (closing ? (contents || '') + closeTag(type) : '') | ||
} | ||
module.exports = createElement; | ||
module.exports = createElement |
{ | ||
"name": "create-element-basic", | ||
"description": "HTML element string creation", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/akileez/create-element-basic", | ||
@@ -17,6 +17,3 @@ "author": { | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/akileez/create-element-basic/blob/master/LICENSE-MIT" | ||
}, | ||
"license": "MIT", | ||
"main": "index.js", | ||
@@ -33,3 +30,4 @@ "engines": { | ||
}, | ||
"keywords": [] | ||
"keywords": ["hmtl", "tags", "tag", "create", "element"], | ||
"files": ["index.js"] | ||
} |
@@ -12,8 +12,2 @@ # create-element-basic [![NPM version](https://badge.fury.io/js/create-element-basic.svg)](http://badge.fury.io/js/create-element-basic) | ||
## Run tests | ||
```bash | ||
npm test | ||
``` | ||
## Usage | ||
@@ -24,5 +18,9 @@ | ||
createElement(htmlTag[string], closingTag[Boolean], attributes[object], content[string || function]) | ||
createElement(htmlTag[string], attributes[object], content[string || function]) | ||
``` | ||
**Breaking Change !** | ||
createElement will now determine if the htmlTag entered is empty or not. No longer necessary | ||
to enter a Boolean (true/false) as the second argument. | ||
### Create an HTML tag either closing or opening type. | ||
@@ -33,4 +31,9 @@ | ||
```js | ||
// Before: | ||
createElement('a', true, {href: 'http://www.apple.com'}, "Apple"); | ||
createElement('img', false, {src: "foo.png"}); | ||
// Now: | ||
createElement('a', {href: 'http://www.apple.com'}, "Apple"); | ||
createElement('img', {src: "foo.png"}); | ||
``` | ||
@@ -45,5 +48,33 @@ | ||
## API | ||
You can nest functions to create complex components. | ||
```js | ||
var closeAria = createElement('span', {'aria-hidden': true}, '×') | ||
+ createElement('span', {class: 'sr-only'}, 'Close') | ||
var close = createElement('button', { | ||
class: 'close', | ||
type: 'button', | ||
'data-dismiss': 'alert' | ||
}, closeAria) | ||
var element = createElement('div', extend({ | ||
class: modifier | ||
}, hash), close + 'One more time for the Gipper.') | ||
return element | ||
``` | ||
yields: | ||
```html | ||
<div class="alert alert-info alert-raised fade in"> | ||
<button class="close" type="button" data-dismiss="alert"> | ||
<span aria-hidden="true">×</span> | ||
<span class="sr-only">Close</span> | ||
</button> | ||
One more time for the Gipper. | ||
</div> | ||
``` | ||
## Contributing | ||
@@ -59,7 +90,4 @@ Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/akileez/create-element-basic/issues) | ||
## License | ||
Copyright (c) 2014 Keith Williams | ||
Copyright (c) 2014-2015 Keith Williams | ||
Released under the MIT license | ||
*** | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 24, 2014._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4092
48
89
3