Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

crel

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

crel - npm Package Compare versions

Comparing version 3.1.0 to 4.0.1

233

crel.js

@@ -1,144 +0,70 @@

//Copyright (C) 2012 Kory Nunn
/* Copyright (C) 2012 Kory Nunn
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.
//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:
NOTE:
This code is formatted for run-speed and to assist compilers.
This might make it harder to read at times, but the code's intention should be transparent. */
//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.
/*
This code is not formatted for readability, but rather run-speed and to assist compilers.
However, the code's intention should be transparent.
*** IE SUPPORT ***
If you require this library to work in IE7, add the following after declaring crel.
var testDiv = document.createElement('div'),
testLabel = document.createElement('label');
testDiv.setAttribute('class', 'a');
testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
testDiv.setAttribute('name','a');
testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
element.id = value;
}:undefined;
testLabel.setAttribute('for', 'a');
testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;
*/
(function (root, factory) {
if (typeof exports === 'object') {
module.exports = factory();
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.crel = factory();
}
}(this, function () {
var fn = 'function',
obj = 'object',
nodeType = 'nodeType',
textContent = 'textContent',
setAttribute = 'setAttribute',
attrMapString = 'attrMap',
// IIFE our function
((exporter) => {
// Define our function and its properties
// These strings are used multiple times, so this makes things smaller once compiled
const func = 'function',
isNodeString = 'isNode',
isElementString = 'isElement',
d = typeof document === obj ? document : {},
isType = function(a, type){
return typeof a === type;
},
isNode = typeof Node === fn ? function (object) {
return object instanceof Node;
} :
// in IE <= 8 Node is an object, obviously..
function(object){
return object &&
isType(object, obj) &&
(nodeType in object) &&
isType(object.ownerDocument,obj);
},
isElement = function (object) {
return crel[isNodeString](object) && object[nodeType] === 1;
},
isArray = function(a){
return a instanceof Array;
},
appendChild = function(element, child) {
if (isArray(child)) {
child.map(function(subChild){
appendChild(element, subChild);
});
return;
d = document,
// Helper functions used throughout the script
isType = (object, type) => typeof object === type,
isNode = (node) => node instanceof Node,
isElement = (object) => object instanceof Element,
// Recursively appends children to given element. As a text node if not already an element
appendChild = (element, child) => {
if (child !== null) {
if (Array.isArray(child)) { // Support (deeply) nested child elements
child.map((subChild) => appendChild(element, subChild));
} else {
if (!crel[isNodeString](child)) {
child = d.createTextNode(child);
}
element.appendChild(child);
}
}
if(!crel[isNodeString](child)){
child = d.createTextNode(child);
}
element.appendChild(child);
};
function crel(){
var args = arguments, //Note: assigned to a variable to assist compilers. Saves about 40 bytes in closure compiler. Has negligable effect on performance.
element = args[0],
child,
settings = args[1],
childIndex = 2,
argumentsLength = args.length,
attributeMap = crel[attrMapString];
element = crel[isElementString](element) ? element : d.createElement(element);
// shortcut
if(argumentsLength === 1){
return element;
}
if(!isType(settings,obj) || crel[isNodeString](settings) || isArray(settings)) {
--childIndex;
settings = null;
}
// shortcut if there is only one child that is a string
if((argumentsLength - childIndex) === 1 && isType(args[childIndex], 'string') && element[textContent] !== undefined){
element[textContent] = args[childIndex];
}else{
for(; childIndex < argumentsLength; ++childIndex){
child = args[childIndex];
if(child == null){
continue;
}
if (isArray(child)) {
for (var i=0; i < child.length; ++i) {
appendChild(element, child[i]);
}
//
function crel (element, settings) {
// Define all used variables / shortcuts here, to make things smaller once compiled
let args = arguments, // Note: assigned to a variable to assist compilers.
index = 1,
key,
attribute;
// If first argument is an element, use it as is, otherwise treat it as a tagname
element = crel.isElement(element) ? element : d.createElement(element);
// Check if second argument is a settings object. Skip it if it's:
// - not an object (this includes `undefined`)
// - a Node
// - an array
if (!(!isType(settings, 'object') || crel[isNodeString](settings) || Array.isArray(settings))) {
// Don't treat settings as a child
index++;
// Go through settings / attributes object, if it exists
for (key in settings) {
// Store the attribute into a variable, before we potentially modify the key
attribute = settings[key];
// Get mapped key / function, if one exists
key = crel.attrMap[key] || key;
// Note: We want to prioritise mapping over properties
if (isType(key, func)) {
key(element, attribute);
} else if (isType(attribute, func)) { // ex. onClick property
element[key] = attribute;
} else {
appendChild(element, child);
// Set the element attribute
element.setAttribute(key, attribute);
}
}
}
for(var key in settings){
if(!attributeMap[key]){
if(isType(settings[key],fn)){
element[key] = settings[key];
}else{
element[setAttribute](key, settings[key]);
}
}else{
var attr = attributeMap[key];
if(typeof attr === fn){
attr(element, settings[key]);
}else{
element[setAttribute](attr, settings[key]);
}
}
// Loop through all arguments, if any, and append them to our element if they're not `null`
for (; index < args.length; index++) {
appendChild(element, args[index]);
}

@@ -149,19 +75,26 @@

// Used for mapping one kind of attribute to the supported version of that in bad browsers.
crel[attrMapString] = {};
crel[isElementString] = isElement;
// Used for mapping attribute keys to supported versions in bad browsers, or to custom functionality
crel.attrMap = {};
crel.isElement = isElement;
crel[isNodeString] = isNode;
if(typeof Proxy !== 'undefined'){
crel.proxy = new Proxy(crel, {
get: function(target, key){
!(key in crel) && (crel[key] = crel.bind(null, key));
return crel[key];
}
});
// Expose proxy interface
crel.proxy = new Proxy(crel, {
get: (target, key) => {
!(key in crel) && (crel[key] = crel.bind(null, key));
return crel[key];
}
});
// Export crel
exporter(crel, func);
})((product, func) => {
if (typeof exports === 'object') {
// Export for Browserify / CommonJS format
module.exports = product;
} else if (typeof define === func && define.amd) {
// Export for RequireJS / AMD format
define(product);
} else {
// Export as a 'global' function
this.crel = product;
}
return crel;
}));
});

@@ -1,1 +0,1 @@

!function(e,n){"object"==typeof exports?module.exports=n():"function"==typeof define&&define.amd?define(n):e.crel=n()}(this,function(){function e(){var o,a=arguments,p=a[0],m=a[1],x=2,v=a.length,b=e[f];if(p=e[c](p)?p:d.createElement(p),1===v)return p;if((!l(m,t)||e[u](m)||s(m))&&(--x,m=null),v-x===1&&l(a[x],"string")&&void 0!==p[r])p[r]=a[x];else for(;v>x;++x)if(o=a[x],null!=o)if(s(o))for(var g=0;g<o.length;++g)y(p,o[g]);else y(p,o);for(var h in m)if(b[h]){var N=b[h];typeof N===n?N(p,m[h]):p[i](N,m[h])}else p[i](h,m[h]);return p}var n="function",t="object",o="nodeType",r="textContent",i="setAttribute",f="attrMap",u="isNode",c="isElement",d=typeof document===t?document:{},l=function(e,n){return typeof e===n},a=typeof Node===n?function(e){return e instanceof Node}:function(e){return e&&l(e,t)&&o in e&&l(e.ownerDocument,t)},p=function(n){return e[u](n)&&1===n[o]},s=function(e){return e instanceof Array},y=function(n,t){e[u](t)||(t=d.createTextNode(t)),n.appendChild(t)};return e[f]={},e[c]=p,e[u]=a,"undefined"!=typeof Proxy&&(e.proxy=new Proxy(e,{get:function(n,t){return!(t in e)&&(e[t]=e.bind(null,t)),e[t]}})),e});
(e=>{const t="function",n="isNode",r=document,o=(e,t)=>typeof e===t,i=(e,t)=>{null!==t&&(Array.isArray(t)?t.map(t=>i(e,t)):(a[n](t)||(t=r.createTextNode(t)),e.appendChild(t)))};function a(e,f){let l,d,s=arguments,c=1;if(e=a.isElement(e)?e:r.createElement(e),o(f,"object")&&!a[n](f)&&!Array.isArray(f))for(l in c++,f)d=f[l],l=a.attrMap[l]||l,o(l,t)?l(e,d):o(d,t)?e[l]=d:e.setAttribute(l,d);for(;c<s.length;c++)i(e,s[c]);return e}a.attrMap={},a.isElement=(e=>e instanceof Element),a[n]=(e=>e instanceof Node),a.proxy=new Proxy(a,{get:(e,t)=>(!(t in a)&&(a[t]=a.bind(null,t)),a[t])}),e(a,t)})((e,t)=>{"object"==typeof exports?module.exports=e:typeof define===t&&define.amd?define(e):this.crel=e});

@@ -0,0 +0,0 @@ The MIT License (MIT)

@@ -8,3 +8,3 @@ {

],
"version": "3.1.0",
"version": "4.0.1",
"main": "crel.js",

@@ -15,9 +15,8 @@ "dependencies": {},

"tape": "^3.0.3",
"tape-run": "^3.0.0",
"uglify-js": "^2.4.16"
"uglify-es": "^3.3.9"
},
"scripts": {
"test": "browserify ./test/index.js | tape-run",
"test": "npm run testBuild && google-chrome ./test/test.html",
"testBuild": "browserify ./test/index.js > ./test/index.browser.js",
"build": "uglifyjs crel.js -m -c > crel.min.js"
"build": "uglifyjs crel.js -m -c --warn -o crel.min.js"
},

@@ -24,0 +23,0 @@ "repository": {

@@ -6,34 +6,22 @@ ![crel](logo.png)

# What #
# What
a small, simple, and fast DOM creation utility
A small, simple, and fast DOM creation utility
# Why? #
# Why
Writing HTML is stupid. It's slow, messy, and should not be done in JavaScript.
The best way to make DOM elements is via document.createElement, but making lots of DOM with it is tedious.
The best way to make DOM elements is via `document.createElement`, but making lots of them with it is tedious.
crel.js makes the process easier.
Crel makes this process easier.
Inspiration was taken from https://github.com/joestelmach/laconic, but crel wont screw with your bad in-DOM event listeners, and is smaller, faster, etc...
Inspiration was taken from [laconic](https://github.com/joestelmach/laconic), but Crel wont screw with your bad in-DOM event listeners, and it's smaller,
faster, etc...
# Usage #
# Installing
Signature:
```javascript
crel(tagName/dom element [, attributes, child1, child2, childN...])
```
where `childN` may be
* a DOM element,
* a string, which will be inserted as a `textNode`,
* `null`, which will be ignored, or
* an `Array` containing any of the above
For browserify:
```
```bash
npm i crel

@@ -46,17 +34,40 @@ ```

For AMD:
```javascript
require.config({paths: { crel: 'https://cdnjs.cloudflare.com/ajax/libs/crel/3.1.0/crel.min' }});
require(['crel'], function(crel) {
// Your code
});
```
For standard script tag style:
```html
<script src="crel.min.js"></script>
<script src="crel.min.js"></script>
```
To make some DOM:
# Usage
Example:
Syntax:
```javascript
// Returns a DOM element
crel(tagName / domElement, attributes, child1, child2, childN);
```
where `childN` may be:
- a DOM element,
- a string, which will be inserted as a `textNode`,
- `null`, which will be ignored, or
- an `Array` containing any of the above
## Examples
```javascript
var element = crel('div',
crel('h1', 'Crello World!'),
crel('p', 'This is crel'),
crel('input', {type: 'number'})
crel('input', { type: 'number' })
);

@@ -67,38 +78,51 @@

You can create attributes with dashes, or reserved keywords, but using strings for the objects keys:
You can add attributes that have dashes or reserved keywords in the name, by using strings for the objects keys:
```javascript
crel('div', {'class':'thing', 'data-attribute':'majigger'});
crel('div', { 'class': 'thing', 'data-attribute': 'majigger' });
```
You can pass an already available element to crel, and it will be the target of the attributes/child elements
You can define custom functionality for certain keys seen in the attributes
object:
```javascript
crel(document.body,
crel('h1', 'Page title')
)
crel.attrMap['on'] = function(element, value) {
for (var eventName in value) {
element.addEventListener(eventName, value[eventName]);
}
};
// Attaches an onClick event to the img element
crel('img', { on: {
'click': function() {
console.log('Clicked');
}
}});
```
You can pass already available elements to Crel to modify their attributes / add child elements to them
```javascript
crel(document.body, crel('h1', 'Page title'));
```
You can assign child elements to variables during creation:
```javascript
var button,
wrapper = crel('div',
button = crel('button')
);
var button;
var wrapper = crel('div',
button = crel('button')
);
```
You could probably use crel to rearrange existing dom..
You could probably use Crel to rearrange existing DOM elements..
```javascript
crel(someDiv,
crel(someOtherDiv, anotherOne)
)
crel(someDiv, crel(someOtherDiv, anotherOne));
```
But don't.
_But don't._
# Proxy support
If you are using crel in an environment that supports Proxies, you can also use the new API:
If you are using Crel in an environment that supports Proxies, you can also use the new API:

@@ -111,3 +135,3 @@ ```javascript

crel.p('This is crel'),
crel.input({type: 'number'})
crel.input({ type: 'number' })
);

@@ -118,41 +142,19 @@ ```

Crel works in everything (as far as I know), but of course...
Crel works in all evergreen browsers.
## IE SUPPORT
# Goals
If you require this library to work in IE7, add the following after declaring crel.
### Easy to use & Tiny
```javascript
var testDiv = document.createElement('div'),
testLabel = document.createElement('label');
Less than 1K minified, about 500 bytes gzipped === **Smal**
testDiv.setAttribute('class', 'a');
testDiv['className'] !== 'a' ? crel.attrMap['class'] = 'className':undefined;
testDiv.setAttribute('name','a');
testDiv['name'] !== 'a' ? crel.attrMap['name'] = function(element, value){
element.id = value;
}:undefined;
### Fast
Crel is fast.
Depending on what browser you use, it is up there with straight `document.createElement` calls: http://jsperf.com/dom-creation-libs/10
testLabel.setAttribute('for', 'a');
testLabel['htmlFor'] !== 'a' ? crel.attrMap['for'] = 'htmlFor':undefined;
```
# License
# Goals #
**MIT**
## Easy to use ##
## Tiny ##
less than 1K minified
about 500 bytes gzipped
## Fast ##
crel is fast. Depending on what browser you use, it is up there with straight document.createElement calls.
http://jsperf.com/dom-creation-libs/10
# License #
MIT
[npm-image]: https://img.shields.io/npm/v/crel.svg?style=flat-square

@@ -159,0 +161,0 @@ [npm-url]: https://npmjs.org/package/crel

@@ -0,0 +0,0 @@ var crel = require('../'),

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc