@movable/data-sources-parser
Advanced tools
Comparing version 0.3.0 to 0.3.1
module.exports = { | ||
runtimeHelpers: true, | ||
include: ['src/**', 'test/**'], | ||
@@ -8,3 +7,6 @@ presets: [ | ||
{ | ||
modules: false | ||
modules: false, | ||
targets: { | ||
browsers: ['Last 2 versions', 'IE 11'] | ||
} | ||
} | ||
@@ -11,0 +13,0 @@ ] |
@@ -1,5 +0,15 @@ | ||
define(['exports'], function (exports) { 'use strict'; | ||
define(['exports', '@babel/runtime/helpers/toConsumableArray', '@babel/runtime/helpers/classCallCheck', '@babel/runtime/helpers/createClass'], function (exports, _toConsumableArray, _classCallCheck, _createClass) { 'use strict'; | ||
class HTMLNormalizer { | ||
constructor(htmlText, resourceURL = window.location.href) { | ||
_toConsumableArray = _toConsumableArray && _toConsumableArray.hasOwnProperty('default') ? _toConsumableArray['default'] : _toConsumableArray; | ||
_classCallCheck = _classCallCheck && _classCallCheck.hasOwnProperty('default') ? _classCallCheck['default'] : _classCallCheck; | ||
_createClass = _createClass && _createClass.hasOwnProperty('default') ? _createClass['default'] : _createClass; | ||
var HTMLNormalizer = | ||
/*#__PURE__*/ | ||
function () { | ||
function HTMLNormalizer(htmlText) { | ||
var resourceURL = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window.location.href; | ||
_classCallCheck(this, HTMLNormalizer); | ||
this._document = this._createDocument(htmlText); | ||
@@ -9,66 +19,78 @@ this._baseURL = this._generateBaseURL(resourceURL); | ||
get document() { | ||
return this._document; | ||
} | ||
_createClass(HTMLNormalizer, [{ | ||
key: "absolutizePath", | ||
value: function absolutizePath(providedPath) { | ||
if (isAbsoluteURL(providedPath)) { | ||
return ensureProtocol(providedPath, this._baseURL.protocol); | ||
} | ||
get baseURL() { | ||
return this._baseURL.toString(); | ||
} | ||
var url = new URL(this.baseURL); // Copy the URL. | ||
absolutizePath(providedPath) { | ||
if (isAbsoluteURL(providedPath)) { | ||
return ensureProtocol(providedPath, this._baseURL.protocol); | ||
var _extractPathParts = extractPathParts(providedPath), | ||
pathname = _extractPathParts.pathname, | ||
hash = _extractPathParts.hash, | ||
search = _extractPathParts.search; | ||
url.pathname = reconcilePaths(url.pathname, pathname); | ||
url.search = search; | ||
url.hash = hash; | ||
return url.toString(); | ||
} | ||
}, { | ||
key: "_getDocumentBaseURL", | ||
value: function _getDocumentBaseURL() { | ||
var baseTag = this._document.querySelector('base'); | ||
const url = new URL(this.baseURL); // Copy the URL. | ||
const { pathname, hash, search } = extractPathParts(providedPath); | ||
return baseTag && baseTag.getAttribute('href'); | ||
} | ||
}, { | ||
key: "_createDocument", | ||
value: function _createDocument(html) { | ||
return new DOMParser().parseFromString(html, 'text/html'); | ||
} | ||
}, { | ||
key: "_generateBaseURL", | ||
value: function _generateBaseURL(resourceURL) { | ||
var url = new URL(resourceURL); | ||
url.pathname = reconcilePaths(url.pathname, pathname); | ||
url.search = search; | ||
url.hash = hash; | ||
var docBase = this._getDocumentBaseURL(); | ||
return url.toString(); | ||
} | ||
if (isAbsoluteURL(docBase)) { | ||
// if the base is missing a protocol, add it: | ||
docBase = ensureProtocol(docBase, url.protocol); | ||
url = new URL(docBase); | ||
docBase = ''; // If the `base` happens to not end in a trailing slash, then we | ||
// remove the last segment of that path to compute the "base" | ||
_getDocumentBaseURL() { | ||
const baseTag = this._document.querySelector('base'); | ||
return baseTag && baseTag.getAttribute('href'); | ||
} | ||
url.pathname = pathDir(url.pathname); | ||
} // Remove the fragment and search, if present | ||
_createDocument(html) { | ||
return new DOMParser().parseFromString(html, 'text/html'); | ||
} | ||
_generateBaseURL(resourceURL) { | ||
let url = new URL(resourceURL); | ||
let docBase = this._getDocumentBaseURL(); | ||
url.search = ''; | ||
url.hash = ''; // Add the newly resolved path. | ||
if (isAbsoluteURL(docBase)) { | ||
// if the base is missing a protocol, add it: | ||
docBase = ensureProtocol(docBase, url.protocol); | ||
if (docBase) { | ||
url.pathname = pathDir(reconcilePaths(url.pathname, docBase)); | ||
} | ||
url = new URL(docBase); | ||
docBase = ''; | ||
// If the `base` happens to not end in a trailing slash, then we | ||
// remove the last segment of that path to compute the "base" | ||
url.pathname = pathDir(url.pathname); | ||
return url; | ||
} | ||
// Remove the fragment and search, if present | ||
url.search = ''; | ||
url.hash = ''; | ||
// Add the newly resolved path. | ||
if (docBase) { | ||
url.pathname = pathDir(reconcilePaths(url.pathname, docBase)); | ||
}, { | ||
key: "document", | ||
get: function get() { | ||
return this._document; | ||
} | ||
}, { | ||
key: "baseURL", | ||
get: function get() { | ||
return this._baseURL.toString(); | ||
} | ||
}]); | ||
return url; | ||
} | ||
} | ||
return HTMLNormalizer; | ||
}(); | ||
function ensureProtocol(url, protocol) { | ||
if (/^\/\//.test(url)) { | ||
return `${protocol}${url}`; | ||
return "".concat(protocol).concat(url); | ||
} | ||
@@ -80,4 +102,3 @@ | ||
function isAbsoluteURL(url) { | ||
return (/^(https?|\/\/)/.test(url) | ||
); | ||
return /^(https?|\/\/)/.test(url); | ||
} | ||
@@ -90,6 +111,8 @@ | ||
const pathParts = [...pathDir(startPath).split('/'), ...updatePath.split('/')]; | ||
var pathParts = _toConsumableArray(pathDir(startPath).split('/')).concat(_toConsumableArray(updatePath.split('/'))); // Ensure there aren't any "double slashes" in the middle of the path. | ||
// Ensure there aren't any "double slashes" in the middle of the path. | ||
return pathParts.filter((part, index, arr) => part !== '' || index === 0 || index === arr.length - 1).join('/'); | ||
return pathParts.filter(function (part, index, arr) { | ||
return part !== '' || index === 0 || index === arr.length - 1; | ||
}).join('/'); | ||
} | ||
@@ -102,17 +125,25 @@ | ||
function extractPathParts(path) { | ||
const isRootPath = path.charAt(0) === '/'; | ||
const { search, hash, pathname: ogPath } = new URL(`https://www.movableink.com${isRootPath ? '' : '/'}${path}`); | ||
var isRootPath = path.charAt(0) === '/'; | ||
const pathname = isRootPath ? ogPath : ogPath.replace(/^\//, ''); | ||
var _ref = new URL("https://www.movableink.com".concat(isRootPath ? '' : '/').concat(path)), | ||
search = _ref.search, | ||
hash = _ref.hash, | ||
ogPath = _ref.pathname; | ||
return { search, hash, pathname }; | ||
var pathname = isRootPath ? ogPath : ogPath.replace(/^\//, ''); | ||
return { | ||
search: search, | ||
hash: hash, | ||
pathname: pathname | ||
}; | ||
} | ||
/* eslint no-underscore-dangle: 0 */ | ||
/* eslint no-param-reassign: 0 */ | ||
/* eslint no-plusplus: 0 */ | ||
function removeEmptyTextNodes(xml) { | ||
if (xml.childNodes.length) { | ||
for (let child = xml.firstChild; child;) { | ||
for (var child = xml.firstChild; child;) { | ||
if (child.nodeType === Node.ELEMENT_NODE) { | ||
@@ -122,3 +153,3 @@ removeEmptyTextNodes(child); | ||
} else if (child.nodeValue.trim() === '') { | ||
const nextChild = child.nextSibling; | ||
var nextChild = child.nextSibling; | ||
xml.removeChild(child); | ||
@@ -136,7 +167,6 @@ child = nextChild; | ||
if (!node.attributes.length) return; | ||
output._attributes = {}; | ||
for (let i = 0; i < node.attributes.length; i++) { | ||
const attribute = node.attributes[i]; | ||
for (var i = 0; i < node.attributes.length; i++) { | ||
var attribute = node.attributes[i]; | ||
output._attributes[attribute.nodeName] = attribute.nodeValue.trim() || ''; | ||
@@ -150,7 +180,10 @@ } | ||
} | ||
let innerXMLStr = ''; | ||
for (let i = 0; i < node.childNodes.length; i++) { | ||
const child = node.childNodes[i]; | ||
var innerXMLStr = ''; | ||
for (var i = 0; i < node.childNodes.length; i++) { | ||
var child = node.childNodes[i]; | ||
innerXMLStr += new XMLSerializer().serializeToString(child); | ||
} | ||
return innerXMLStr; | ||
@@ -163,2 +196,3 @@ } | ||
} | ||
output[type] = node.textContent.trim() || ''; | ||
@@ -169,12 +203,11 @@ return output; | ||
function parseNode(node) { | ||
let output = {}; | ||
var output = {}; | ||
parseAttributes(node, output); | ||
if (node.childNodes.length) { | ||
let textChild = 0; | ||
let cdataChild = 0; | ||
let hasElementChild = false; | ||
var textChild = 0; | ||
var cdataChild = 0; | ||
var hasElementChild = false; | ||
for (let child = node.firstChild; child; child = child.nextSibling) { | ||
for (var child = node.firstChild; child; child = child.nextSibling) { | ||
if (child.nodeType === Node.ELEMENT_NODE) hasElementChild = true; | ||
@@ -187,22 +220,26 @@ if (child.nodeType === Node.TEXT_NODE) textChild++; | ||
if (textChild < 2 && cdataChild < 2) { | ||
for (let child = node.firstChild; child; child = child.nextSibling) { | ||
switch (child.nodeType) { | ||
for (var _child = node.firstChild; _child; _child = _child.nextSibling) { | ||
switch (_child.nodeType) { | ||
case Node.ELEMENT_NODE: | ||
// handles element arrays | ||
if (output[child.nodeName]) { | ||
if (output[child.nodeName] instanceof Array) { | ||
output[child.nodeName].push(parseNode(child)); | ||
if (output[_child.nodeName]) { | ||
if (output[_child.nodeName] instanceof Array) { | ||
output[_child.nodeName].push(parseNode(_child)); | ||
} else { | ||
output[child.nodeName] = [output[child.nodeName], parseNode(child)]; | ||
output[_child.nodeName] = [output[_child.nodeName], parseNode(_child)]; | ||
} | ||
} else { | ||
output[child.nodeName] = parseNode(child); | ||
output[_child.nodeName] = parseNode(_child); | ||
} | ||
break; | ||
case Node.TEXT_NODE: | ||
output._text = child.textContent.trim(); | ||
output._text = _child.textContent.trim(); | ||
break; | ||
case Node.CDATA_SECTION_NODE: | ||
output._cdata = child.textContent.trim(); | ||
output._cdata = _child.textContent.trim(); | ||
break; | ||
default: | ||
@@ -222,3 +259,3 @@ break; | ||
} else { | ||
throw new Error(`unhandled node type: ${node.nodeType}`); | ||
throw new Error("unhandled node type: ".concat(node.nodeType)); | ||
} | ||
@@ -233,6 +270,4 @@ } else if (!node.attributes.length) { | ||
function xmlConverter(xml) { | ||
const xmlDoc = typeof xml === 'string' ? new DOMParser().parseFromString(xml, 'text/xml') : xml; | ||
var xmlDoc = typeof xml === 'string' ? new DOMParser().parseFromString(xml, 'text/xml') : xml; | ||
removeEmptyTextNodes(xmlDoc); | ||
return parseNode(xmlDoc); | ||
@@ -239,0 +274,0 @@ } |
{ | ||
"name": "@movable/data-sources-parser", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "", | ||
@@ -40,3 +40,3 @@ "main": "src/index.js", | ||
"rollup": "^0.63.5", | ||
"rollup-plugin-babel": "^3.0.7" | ||
"rollup-plugin-babel": "^4.0.0-beta.7" | ||
}, | ||
@@ -43,0 +43,0 @@ "dependencies": { |
@@ -11,2 +11,3 @@ const babel = require('rollup-plugin-babel'); | ||
babel({ | ||
runtimeHelpers: true, | ||
exclude: 'node_modules/**' // only transpile our source code | ||
@@ -13,0 +14,0 @@ }) |
Sorry, the diff of this file is not supported yet
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
274671
844
0