react-from-dom
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -489,2 +489,37 @@ export declare const noTextChildNodes: string[]; | ||
zoomandpan: string; | ||
onblur: string; | ||
onchange: string; | ||
onclick: string; | ||
oncontextmenu: string; | ||
ondoubleclick: string; | ||
ondrag: string; | ||
ondragend: string; | ||
ondragenter: string; | ||
ondragexit: string; | ||
ondragleave: string; | ||
ondragover: string; | ||
ondragstart: string; | ||
ondrop: string; | ||
onerror: string; | ||
onfocus: string; | ||
oninput: string; | ||
oninvalid: string; | ||
onkeydown: string; | ||
onkeypress: string; | ||
onkeyup: string; | ||
onload: string; | ||
onmousedown: string; | ||
onmouseenter: string; | ||
onmouseleave: string; | ||
onmousemove: string; | ||
onmouseout: string; | ||
onmouseover: string; | ||
onmouseup: string; | ||
onscroll: string; | ||
onsubmit: string; | ||
ontouchcancel: string; | ||
ontouchend: string; | ||
ontouchmove: string; | ||
ontouchstart: string; | ||
onwheel: string; | ||
}; |
@@ -517,2 +517,38 @@ "use strict"; | ||
zoomandpan: 'zoomAndPan', | ||
// event handlers | ||
onblur: 'onBlur', | ||
onchange: 'onChange', | ||
onclick: 'onClick', | ||
oncontextmenu: 'onContextMenu', | ||
ondoubleclick: 'onDoubleClick', | ||
ondrag: 'onDrag', | ||
ondragend: 'onDragEnd', | ||
ondragenter: 'onDragEnter', | ||
ondragexit: 'onDragExit', | ||
ondragleave: 'onDragLeave', | ||
ondragover: 'onDragOver', | ||
ondragstart: 'onDragStart', | ||
ondrop: 'onDrop', | ||
onerror: 'onError', | ||
onfocus: 'onFocus', | ||
oninput: 'onInput', | ||
oninvalid: 'onInvalid', | ||
onkeydown: 'onKeyDown', | ||
onkeypress: 'onKeyPress', | ||
onkeyup: 'onKeyUp', | ||
onload: 'onLoad', | ||
onmousedown: 'onMouseDown', | ||
onmouseenter: 'onMouseEnter', | ||
onmouseleave: 'onMouseLeave', | ||
onmousemove: 'onMouseMove', | ||
onmouseout: 'onMouseOut', | ||
onmouseover: 'onMouseOver', | ||
onmouseup: 'onMouseUp', | ||
onscroll: 'onScroll', | ||
onsubmit: 'onSubmit', | ||
ontouchcancel: 'onTouchCancel', | ||
ontouchend: 'onTouchEnd', | ||
ontouchmove: 'onTouchMove', | ||
ontouchstart: 'onTouchStart', | ||
onwheel: 'onWheel', | ||
}; |
@@ -6,2 +6,3 @@ import React from 'react'; | ||
level?: number; | ||
nodeOnly?: boolean; | ||
selector?: string; | ||
@@ -15,5 +16,5 @@ type?: string; | ||
} | ||
export declare function convertFromNode(node: Node, options?: IOptions): any; | ||
export declare function convertFromString(text: string, options?: IOptions): any; | ||
export declare function convertFromNode(input: Node, options?: IOptions): any; | ||
export declare function convertFromString(input: string, options?: IOptions): any; | ||
export default function convert(input: Node | string, options?: IOptions): any; | ||
export {}; |
@@ -88,19 +88,20 @@ "use strict"; | ||
} | ||
function convertFromNode(node, options) { | ||
function convertFromNode(input, options) { | ||
if (options === void 0) { options = {}; } | ||
if (!node) { | ||
if (!input || !(input instanceof Node)) { | ||
return null; | ||
} | ||
var _a = options.actions, actions = _a === void 0 ? [] : _a, _b = options.index, index = _b === void 0 ? 0 : _b, _c = options.level, level = _c === void 0 ? 0 : _c; | ||
var nextNode = node; | ||
var node = input; | ||
var key = level + "-" + index; | ||
var result = []; | ||
/* istanbul ignore else */ | ||
if (Array.isArray(actions)) { | ||
actions.forEach(function (action) { | ||
if (action.condition(nextNode, key, level)) { | ||
if (action.condition(node, key, level)) { | ||
if (typeof action.pre === 'function') { | ||
nextNode = action.pre(nextNode, key, level); | ||
/* istanbul ignore next */ | ||
if (!(nextNode instanceof Node)) { | ||
nextNode = node; | ||
node = action.pre(node, key, level); | ||
if (!(node instanceof Node)) { | ||
node = input; | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -113,3 +114,3 @@ // tslint:disable-next-line:no-console | ||
if (typeof action.post === 'function') { | ||
result.push(action.post(nextNode, key, level)); | ||
result.push(action.post(node, key, level)); | ||
} | ||
@@ -122,7 +123,7 @@ } | ||
} | ||
switch (nextNode.nodeType) { | ||
switch (node.nodeType) { | ||
case 1: // regular dom-node | ||
return react_1.default.createElement(parseName(nextNode.nodeName), parseAttributes(nextNode, key), parseChildren(nextNode.childNodes, level, options)); | ||
return react_1.default.createElement(parseName(node.nodeName), parseAttributes(node, key), parseChildren(node.childNodes, level, options)); | ||
case 3: // textnode | ||
var nodeText = nextNode.nodeValue.toString(); | ||
var nodeText = node.nodeValue.toString(); | ||
/* istanbul ignore else */ | ||
@@ -133,11 +134,11 @@ if (!/[a-zA-Z0-9_]+/.test(nodeText)) { | ||
/* istanbul ignore next */ | ||
if (!nextNode.parentNode) { | ||
if (!node.parentNode) { | ||
return nodeText; | ||
} | ||
var parentNodeName = nextNode.parentNode.nodeName.toLowerCase(); | ||
/* istanbul ignore next */ | ||
var parentNodeName = node.parentNode.nodeName.toLowerCase(); | ||
if (helpers_1.noTextChildNodes.indexOf(parentNodeName) !== -1) { | ||
/* istanbul ignore else */ | ||
if (/\S/.test(nodeText)) { | ||
// tslint:disable-next-line:no-console | ||
console.warn("a textnode is not allowed inside '" + parentNodeName + "'. your text \"" + nodeText + "\" will be ignored"); | ||
console.warn("A textNode is not allowed inside '" + parentNodeName + "'. Your text \"" + nodeText + "\" will be ignored"); | ||
} | ||
@@ -149,2 +150,3 @@ return null; | ||
return null; | ||
/* istanbul ignore next */ | ||
default: | ||
@@ -155,18 +157,22 @@ return null; | ||
exports.convertFromNode = convertFromNode; | ||
function convertFromString(text, options) { | ||
function convertFromString(input, options) { | ||
if (options === void 0) { options = {}; } | ||
if (!text || typeof text !== 'string') { | ||
if (!input || typeof input !== 'string') { | ||
return null; | ||
} | ||
var _a = options.type, type = _a === void 0 ? 'text/html' : _a, _b = options.selector, selector = _b === void 0 ? 'body > *' : _b; | ||
var _a = options.nodeOnly, nodeOnly = _a === void 0 ? false : _a, _b = options.selector, selector = _b === void 0 ? 'body > *' : _b, _c = options.type, type = _c === void 0 ? 'text/html' : _c; | ||
try { | ||
var parser = new DOMParser(); | ||
var doc = parser.parseFromString(text, type); | ||
var doc = parser.parseFromString(input, type); | ||
var node = doc.querySelector(selector); | ||
if (!(node instanceof Node)) { | ||
throw new Error('Error parsing text'); | ||
throw new Error('Error parsing input'); | ||
} | ||
if (nodeOnly) { | ||
return node; | ||
} | ||
return convertFromNode(node, options); | ||
} | ||
catch (error) { | ||
/* istanbul ignore else */ | ||
if (process.env.NODE_ENV !== 'production') { | ||
@@ -173,0 +179,0 @@ // tslint:disable-next-line:no-console |
{ | ||
"name": "react-from-dom", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Convert HTML/XML source code or DOM nodes to React elements", | ||
@@ -5,0 +5,0 @@ "author": "Gil Barbara <gilbarbara@gmail.com>", |
# react-from-dom | ||
[![NPM version](https://badge.fury.io/js/react-from-dom.svg)](https://www.npmjs.com/package/react-from-dom) [![build status](https://travis-ci.org/gilbarbara/react-from-dom.svg)](https://travis-ci.org/gilbarbara/react-from-dom) [![Maintainability](https://api.codeclimate.com/v1/badges/8b7357d2d51cd2ee7f8e/maintainability)](https://codeclimate.com/github/gilbarbara/react-from-dom/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/8b7357d2d51cd2ee7f8e/test_coverage)](https://codeclimate.com/github/gilbarbara/react-from-dom/test_coverage) | ||
Convert HTML/XML source code or a DOM node to a React element. | ||
@@ -4,0 +6,0 @@ The perfect replacement for React's `dangerouslySetInnerHTML` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
44553
1285
147
0