Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed May 15, 2019
0 parents commit 31fb139
Show file tree
Hide file tree
Showing 12 changed files with 503 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
node_modules/
coverage/
.DS_Store
package-lock.json
11 changes: 11 additions & 0 deletions .npmignore
@@ -0,0 +1,11 @@
coverage/
img/
node_modules/
test/
_config.yml
.DS_Store
.gitignore
.travis.yml
package-lock.json
rollup.config.js
es.config.js
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: node_js
node_js:
- stable
git:
depth: 1
branches:
only:
- master
- /^greenkeeper/.*$/
after_success:
- "npm run coveralls"
15 changes: 15 additions & 0 deletions LICENSE
@@ -0,0 +1,15 @@
ISC License

Copyright (c) 2019, Andrea Giammarchi, @WebReflection

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
@@ -0,0 +1,34 @@
# safer-dom

<sup>**Social Media Photo by [freestocks.org](https://unsplash.com/@freestocks) on [Unsplash](https://unsplash.com/)**</sup>

[![Build Status](https://travis-ci.com/WebReflection/safer-dom.svg?branch=master)](https://travis-ci.com/WebReflection/safer-dom) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/safer-dom/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/safer-promise?branch=master) ![WebReflection status](https://offline.report/status/webreflection.svg)

Most common DOM operations made safe by [safer-function](https://github.com/WebReflection/safer-function#safer-function) and [safer-class](https://github.com/WebReflection/safer-class#safer-class).

### Example

```js
// list of exports
import {
CustomEvent,
Event,
HTMLElement,
window,
document,
childNodes,
children,
createElement,
addEventListener,
removeEventListener,
dispatchEvent,
appendChild,
removeChild,
insertBefore,
querySelector,
querySelectorAll
} from 'safer-dom';

const body = querySelector(document, 'body');
const nodes = childNodes(body);
```
87 changes: 87 additions & 0 deletions cjs/index.js
@@ -0,0 +1,87 @@
'use strict';
const {call} = require('safer-function');
const saferClass = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('safer-class'));

const {getOwnPropertyDescriptor} = Object;

const {prototype} = Element;
const {
addEventListener: add, removeEventListener: remove,
dispatchEvent: dispatch,
appendChild: app, removeChild: rem,
insertBefore: ins,
querySelector: query, querySelectorAll: queryAll
} = prototype;

const getChildNodes = getOwnPropertyDescriptor(Node.prototype, 'childNodes').get;
const getChildren = getOwnPropertyDescriptor(prototype, 'children').get;

const doc = document;
const {defaultView, querySelector: $, querySelectorAll: $$} = doc;

const CE = saferClass(
class SaferCustomEvent extends CustomEvent {}
);

const E = saferClass(
class SaferEvent extends Event {}
);

const HE = saferClass(
class SaferHTMLElement extends HTMLElement {}
);

exports.document = doc;
exports.window = defaultView;
exports.CustomEvent = CE;
exports.Event = E;
exports.HTMLElement = HE;

const childNodes = where => call(getChildNodes, where);
exports.childNodes = childNodes;

const children = where => call(getChildren, where);
exports.children = children;

const createElement = doc.createElement.bind(doc);
exports.createElement = createElement;

const addEventListener = (where, type, listener, options) => call(
add, where, type, listener, options == null ? false : options
);
exports.addEventListener = addEventListener;

const removeEventListener = (where, type, listener, options) => call(
remove, where, type, listener, options == null ? false : options
);
exports.removeEventListener = removeEventListener;

const dispatchEvent = (where, event) => call(
dispatch, where, event
);
exports.dispatchEvent = dispatchEvent;

const appendChild = (where, child) => call(
app, where, child
);
exports.appendChild = appendChild;

const removeChild = (where, child) => call(
rem, where, child
);
exports.removeChild = removeChild;

const insertBefore = (where, node, before) => call(
ins, where, node, before
);
exports.insertBefore = insertBefore;

const querySelector = (where, selector) => call(
where === doc ? $ : query, where, selector
);
exports.querySelector = querySelector;

const querySelectorAll = (where, selector) => call(
where === doc ? $$ : queryAll, where, selector
);
exports.querySelectorAll = querySelectorAll;
77 changes: 77 additions & 0 deletions esm/index.js
@@ -0,0 +1,77 @@
import {call} from 'safer-function';
import saferClass from 'safer-class';

const {getOwnPropertyDescriptor} = Object;

const {prototype} = Element;
const {
addEventListener: add, removeEventListener: remove,
dispatchEvent: dispatch,
appendChild: app, removeChild: rem,
insertBefore: ins,
querySelector: query, querySelectorAll: queryAll
} = prototype;

const getChildNodes = getOwnPropertyDescriptor(Node.prototype, 'childNodes').get;
const getChildren = getOwnPropertyDescriptor(prototype, 'children').get;

const doc = document;
const {defaultView, querySelector: $, querySelectorAll: $$} = doc;

const CE = saferClass(
class SaferCustomEvent extends CustomEvent {}
);

const E = saferClass(
class SaferEvent extends Event {}
);

const HE = saferClass(
class SaferHTMLElement extends HTMLElement {}
);

export {
doc as document,
defaultView as window,
CE as CustomEvent,
E as Event,
HE as HTMLElement,
};

export const childNodes = where => call(getChildNodes, where);

export const children = where => call(getChildren, where);

export const createElement = doc.createElement.bind(doc);

export const addEventListener = (where, type, listener, options) => call(
add, where, type, listener, options == null ? false : options
);

export const removeEventListener = (where, type, listener, options) => call(
remove, where, type, listener, options == null ? false : options
);

export const dispatchEvent = (where, event) => call(
dispatch, where, event
);

export const appendChild = (where, child) => call(
app, where, child
);

export const removeChild = (where, child) => call(
rem, where, child
);

export const insertBefore = (where, node, before) => call(
ins, where, node, before
);

export const querySelector = (where, selector) => call(
where === doc ? $ : query, where, selector
);

export const querySelectorAll = (where, selector) => call(
where === doc ? $$ : queryAll, where, selector
);
146 changes: 146 additions & 0 deletions index.js
@@ -0,0 +1,146 @@
var saferDOM = (function (exports) {
'use strict';

/*! (c) Andrea Giammarchi - ISC */
var call = Function.call;
var bind = call.bind(call.bind);
var apply = bind(call, call.apply);
call = bind(call, call);

/*! (c) Andrea Giammarchi - ISC */

const {
defineProperty,
getPrototypeOf,
getOwnPropertyDescriptor,
getOwnPropertyNames,
hasOwnProperty
} = Object;

const falsify = (descriptor, name) => {
defineProperty(descriptor, name, {
enumerable: true,
value: false
});
};

const updated = descriptor => {
falsify(descriptor, 'configurable');
if (call(hasOwnProperty, descriptor, 'writable'))
falsify(descriptor, 'writable');
return descriptor;
};

var saferObject = object => {
const self = object;
const names = [];
const descriptors = [];
do {
getOwnPropertyNames(object).forEach(name => {
if (!names.includes(name)) {
names.push(name);
descriptors.push(getOwnPropertyDescriptor(object, name));
}
});
}
while (object = getPrototypeOf(object));
names.forEach((name, i) => {
defineProperty(self, name, updated(descriptors[i]));
});
return self;
};

/*! (c) Andrea Giammarchi - ISC */

var saferClass = Class => (
saferObject(Class.prototype),
saferObject(Class)
);

const {getOwnPropertyDescriptor: getOwnPropertyDescriptor$1} = Object;

const {prototype} = Element;
const {
addEventListener: add, removeEventListener: remove,
dispatchEvent: dispatch,
appendChild: app, removeChild: rem,
insertBefore: ins,
querySelector: query, querySelectorAll: queryAll
} = prototype;

const getChildNodes = getOwnPropertyDescriptor$1(Node.prototype, 'childNodes').get;
const getChildren = getOwnPropertyDescriptor$1(prototype, 'children').get;

const doc = document;
const {defaultView, querySelector: $, querySelectorAll: $$} = doc;

const CE = saferClass(
class SaferCustomEvent extends CustomEvent {}
);

const E = saferClass(
class SaferEvent extends Event {}
);

const HE = saferClass(
class SaferHTMLElement extends HTMLElement {}
);

const childNodes = where => call(getChildNodes, where);

const children = where => call(getChildren, where);

const createElement = doc.createElement.bind(doc);

const addEventListener = (where, type, listener, options) => call(
add, where, type, listener, options == null ? false : options
);

const removeEventListener = (where, type, listener, options) => call(
remove, where, type, listener, options == null ? false : options
);

const dispatchEvent = (where, event) => call(
dispatch, where, event
);

const appendChild = (where, child) => call(
app, where, child
);

const removeChild = (where, child) => call(
rem, where, child
);

const insertBefore = (where, node, before) => call(
ins, where, node, before
);

const querySelector = (where, selector) => call(
where === doc ? $ : query, where, selector
);

const querySelectorAll = (where, selector) => call(
where === doc ? $$ : queryAll, where, selector
);


















return index;

}({}));
2 changes: 2 additions & 0 deletions min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31fb139

Please sign in to comment.