@wildpeaks/snapshot-dom
Advanced tools
Comparing version 2.0.0-rc1 to 2.0.0-rc2
@@ -1,48 +0,1 @@ | ||
/* eslint-env browser */ | ||
"use strict"; | ||
(function() { | ||
function snapshotToJson(node, options = {}) { | ||
const serialized = {}; | ||
const isValid = typeof node === "object" && node !== null; | ||
if (isValid) { | ||
if (node.tagName) { | ||
serialized.tagName = node.tagName.toLowerCase(); | ||
} else if (node.nodeName) { | ||
serialized.nodeName = node.nodeName; | ||
} | ||
if (node.nodeValue) { | ||
serialized.nodeValue = node.nodeValue; | ||
} | ||
const attrs = node.attributes; | ||
if (attrs) { | ||
const l = attrs.length; | ||
if (l > 0) { | ||
const aggregated = {}; | ||
for (let i = 0; i < l; i++) { | ||
const attr = attrs[i]; | ||
const skip = options.skipEmptyValue && !attr.nodeValue; | ||
if (!skip) { | ||
aggregated[attr.nodeName] = attr.nodeValue; | ||
} | ||
} | ||
serialized.attributes = aggregated; | ||
} | ||
} | ||
const {childNodes} = node; | ||
if (childNodes) { | ||
const l = childNodes.length; | ||
if (l > 0) { | ||
const aggregated = new Array(l); | ||
for (let i = 0; i < l; i++) { | ||
aggregated[i] = snapshotToJson(childNodes[i], options); | ||
} | ||
serialized.childNodes = aggregated; | ||
} | ||
} | ||
} | ||
return serialized; | ||
} | ||
window.snapshotToJson = snapshotToJson; | ||
})(); | ||
"use strict";window.snapshotToJSON=function e(t){const o={};if("object"==typeof t&&null!==t){t.tagName?o.tagName=t.tagName.toLowerCase():t.nodeName&&(o.nodeName=t.nodeName),t.nodeValue&&(o.nodeValue=t.nodeValue);const n=t.attributes;if(n){const e=n.length;if(e>0){const t={};for(let o=0;o<e;o++){const e=n[o];t[e.nodeName]=e.nodeValue}o.attributes=t}}const{childNodes:a}=t;if(a){const t=a.length;if(t>0){const n=new Array(t);for(let o=0;o<t;o++)n[o]=e(a[o]);o.childNodes=n}}}return o}; |
{ | ||
"name": "@wildpeaks/snapshot-dom", | ||
"version": "2.0.0-rc1", | ||
"version": "2.0.0-rc2", | ||
"description": "Converts a DOM element to a JSON tree", | ||
@@ -12,3 +12,3 @@ "author": "Cecile Muller", | ||
], | ||
"homepage": "https://github.com/wildpeaks/package-snapshot-dom#readme", | ||
"homepage": "https://github.com/wildpeaks/package-snapshot-dom/tree/master/packages/snapshot-dom#readme", | ||
"repository": "https://github.com/wildpeaks/package-snapshot-dom", | ||
@@ -18,35 +18,9 @@ "bugs": { | ||
}, | ||
"eslintConfig": { | ||
"extends": "@wildpeaks/commonjs" | ||
}, | ||
"prettier": { | ||
"printWidth": 120, | ||
"tabWidth": 4, | ||
"useTabs": true, | ||
"bracketSpacing": false | ||
}, | ||
"greenkeeper": { | ||
"commitMessages": { | ||
"dependencyUpdate": "feat: update ${dependency}", | ||
"devDependencyUpdate": "chore: update ${dependency}" | ||
} | ||
}, | ||
"main": "src/snapshot.js", | ||
"main": "lib/index.js", | ||
"browser": "lib/browser.js", | ||
"files": [ | ||
"src", | ||
"lib" | ||
], | ||
"scripts": { | ||
"lint": "eslint ./src/**/*.js", | ||
"test": "mocha test/*.spec.js" | ||
}, | ||
"devDependencies": { | ||
"@wildpeaks/eslint-config-commonjs": "8.1.0", | ||
"eslint": "6.8.0", | ||
"express": "4.17.1", | ||
"jsdom": "16.1.0", | ||
"mocha": "7.0.1", | ||
"prettier": "1.19.1", | ||
"puppeteer": "2.1.1" | ||
} | ||
"lib", | ||
"removeEmptyAttributes", | ||
"sortAttributes" | ||
] | ||
} |
# Snapshot | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/wildpeaks/package-snapshot-dom.svg)](https://greenkeeper.io/) | ||
Converts an HTMLElement to a JSON tree, useful for automated DOM tests. | ||
@@ -48,48 +46,1 @@ | ||
```` | ||
--- | ||
## Skip empty values | ||
It can optionally exclude empty values using the second parameter (default: `false`). | ||
````js | ||
document.body.innerHTML = '<img param1 param2="" param3="hello" />; | ||
// Without skipping | ||
assert.deepStrictEqual( | ||
snapshot.toJSON(document.body, {skipEmptyValue: false}), | ||
{ | ||
tagName: 'body', | ||
childNodes: [ | ||
{ | ||
tagName: 'img', | ||
attributes: { | ||
param1: '', | ||
param2: '', | ||
param3: 'hello' | ||
} | ||
} | ||
] | ||
} | ||
); | ||
// With skipping | ||
assert.deepStrictEqual( | ||
snapshot.toJSON(document.body, {skipEmptyValue: true}), | ||
{ | ||
tagName: 'body', | ||
childNodes: [ | ||
{ | ||
tagName: 'img', | ||
attributes: { | ||
param3: 'hello' | ||
} | ||
} | ||
] | ||
} | ||
); | ||
```` | ||
Note that strings containing only whitespace characters are not empty values. | ||
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
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
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
0
8
3793
0
46
1