html-react-parser
Advanced tools
Comparing version 0.0.2 to 0.0.3
81
index.js
@@ -1,24 +0,63 @@ | ||
'use strict'; | ||
/* eslint-disable strict */ | ||
// UMD template: https://github.com/ForbesLindesay/umd/blob/master/template.js | ||
;(function(factory) { // eslint-disable-line no-extra-semi | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var htmlToDOM = require('./lib/html-to-dom'); | ||
var domToReact = require('./lib/dom-to-react'); | ||
// CommonJS | ||
if (typeof exports === 'object' && typeof module !== 'undefined') { | ||
module.exports = factory(); | ||
/** | ||
* Convert HTML to React. | ||
* | ||
* @param {String} html - The HTML. | ||
* @param {Object} [options] - The additional options. | ||
* @param {Function} [options.replace] - The replace method. | ||
* @return {ReactElement|Array} | ||
*/ | ||
function htmlToReact(html, options) { | ||
return domToReact(htmlToDOM(html), options); | ||
} | ||
// RequireJS (AMD) | ||
} else if (typeof define === 'function' && define.amd) { // eslint-disable-line no-undef | ||
define([], factory()); // eslint-disable-line no-undef | ||
/** | ||
* Export HTML to React parser. | ||
*/ | ||
module.exports = htmlToReact; | ||
// Browser (script tag) | ||
} else { | ||
var root; | ||
if (typeof window !== 'undefined') { | ||
root = window; | ||
} else if (typeof global !== 'undefined') { | ||
root = global; | ||
} else if (typeof self !== 'undefined') { | ||
root = self; | ||
} else { | ||
// works provided we're not in strict mode | ||
root = this; | ||
} | ||
// define namespace | ||
root.HTMLReactParser = factory(); | ||
} | ||
})(function() { | ||
var domToReact = require('./lib/dom-to-react'); | ||
var htmlToDOM; | ||
// client (browser) | ||
if (typeof window !== 'undefined' && this === window) { | ||
htmlToDOM = require('./lib/html-to-dom-client'); | ||
// server (node) | ||
} else { | ||
htmlToDOM = require('./lib/html-to-dom-server'); | ||
} | ||
/** | ||
* Convert HTML string to React elements. | ||
* | ||
* @param {String} html - The HTML. | ||
* @param {Object} [options] - The additional options. | ||
* @param {Function} [options.replace] - The replace method. | ||
* @return {ReactElement|Array} | ||
*/ | ||
function HTMLReactParser(html, options) { | ||
if (typeof html !== 'string') { | ||
throw new Error('`HTMLReactParser`: The first argument must be a string.'); | ||
} | ||
return domToReact(htmlToDOM(html), options); | ||
} | ||
// source | ||
return HTMLReactParser; | ||
}); |
@@ -98,4 +98,53 @@ 'use strict'; | ||
/** | ||
* Export HTML to DOM client helper. | ||
* Parse HTML string to DOM nodes. | ||
* This uses the browser DOM API. | ||
* | ||
* @param {String} html - The HTML. | ||
* @return {Object} - The DOM nodes. | ||
*/ | ||
module.exports = formatDOM; | ||
function htmlToDOMClient(html) { | ||
var match = typeof html === 'string' ? html.match(/<(.+?)>/) : null; | ||
var tagName; | ||
var parentNode; | ||
var nodes; | ||
if (match && typeof match[1] === 'string') { | ||
tagName = match[1].toLowerCase(); | ||
} | ||
// `DOMParser` can parse full HTML | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMParser | ||
if (tagName && window.DOMParser) { | ||
var parser = new window.DOMParser(); | ||
var doc = parser.parseFromString(html, 'text/html'); | ||
// <head> and <body> are siblings | ||
if (tagName === 'head' || tagName === 'body') { | ||
nodes = doc.getElementsByTagName(tagName); | ||
// document's child nodes | ||
} else if (tagName === 'html') { | ||
nodes = doc.childNodes; | ||
// get the element's parent's child nodes | ||
// do this in case of adjacent elements | ||
} else { | ||
parentNode = doc.getElementsByTagName(tagName)[0].parentNode; | ||
nodes = parentNode.childNodes; | ||
} | ||
// otherwise, use `innerHTML` | ||
// but this will strip out tags like <html> and <body> | ||
} else { | ||
parentNode = document.createElement('div'); | ||
parentNode.innerHTML = html; | ||
nodes = parentNode.childNodes; | ||
} | ||
return formatDOM(nodes); | ||
} | ||
/** | ||
* Export HTML to DOM parser (client). | ||
*/ | ||
module.exports = htmlToDOMClient; |
{ | ||
"name": "html-react-parser", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "An HTML to React parser.", | ||
@@ -40,2 +40,3 @@ "author": "Mark <mark@remarkablemark.org>", | ||
"browser": { | ||
"./lib/html-to-dom-server.js": false, | ||
"htmlparser2/lib/Parser": false, | ||
@@ -42,0 +43,0 @@ "domhandler": false |
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
20191
471
10