snabbdom-virtualize
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -8,7 +8,7 @@ 'use strict'; | ||
exports.default = function (el) { | ||
exports.default = function (el, options) { | ||
if (typeof el === 'string') { | ||
return (0, _strings2.default)(el); | ||
return (0, _strings2.default)(el, options); | ||
} else { | ||
return (0, _nodes2.default)(el); | ||
return (0, _nodes2.default)(el, options); | ||
} | ||
@@ -15,0 +15,0 @@ }; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.default = snabbdomVirtualize; | ||
exports.default = virtualizeNodes; | ||
@@ -21,3 +21,5 @@ var _h = require('snabbdom/h'); | ||
function snabbdomVirtualize(element) { | ||
function virtualizeNodes(element) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
if (!element) { | ||
@@ -27,6 +29,17 @@ return null; | ||
var createdVNodes = []; | ||
var vnode = convertNode(element, createdVNodes); | ||
options.hooks && options.hooks.create && createdVNodes.forEach(function (node) { | ||
options.hooks.create(node); | ||
}); | ||
return vnode; | ||
} | ||
function convertNode(element, createdVNodes) { | ||
// If our node is a text node, then we only want to set the `text` part of | ||
// the VNode. | ||
if (element.nodeType === Node.TEXT_NODE) { | ||
return (0, _utils.createTextVNode)(element.textContent); | ||
var _newNode = (0, _utils.createTextVNode)(element.textContent); | ||
createdVNodes.push(_newNode); | ||
return _newNode; | ||
} | ||
@@ -79,6 +92,8 @@ | ||
for (var i = 0; i < children.length; i++) { | ||
childNodes.push(snabbdomVirtualize(children.item(i))); | ||
childNodes.push(convertNode(children.item(i), createdVNodes)); | ||
} | ||
} | ||
return (0, _h2.default)(element.tagName.toLowerCase(), data, childNodes); | ||
var newNode = (0, _h2.default)(element.tagName.toLowerCase(), data, childNodes); | ||
createdVNodes.push(newNode); | ||
return newNode; | ||
} | ||
@@ -85,0 +100,0 @@ |
@@ -8,2 +8,4 @@ 'use strict'; | ||
exports.default = function (html) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
// If there's nothing here, return null; | ||
@@ -13,5 +15,10 @@ if (!html) { | ||
} | ||
// Maintain a list of created vnodes so we can call the create hook. | ||
var createdVNodes = []; | ||
// Parse the string into the AST and convert to VNodes. | ||
var vnodes = convertNodes((0, _parse2.default)(html)); | ||
var vnodes = convertNodes((0, _parse2.default)(html), createdVNodes); | ||
var res = void 0; | ||
if (!vnodes) { | ||
@@ -21,10 +28,16 @@ // If there are no vnodes but there is string content, then the string | ||
// text (since the AST parser didn't find any well-formed HTML). | ||
return (0, _utils.createTextVNode)(html); | ||
res = toVNode({ type: 'text', content: html }, createdVNodes); | ||
} else if (vnodes.length === 1) { | ||
// If there's only one root node, just return it as opposed to an array. | ||
return vnodes[0]; | ||
res = vnodes[0]; | ||
} else { | ||
// Otherwise we have an array of VNodes, which we should return. | ||
return vnodes; | ||
res = vnodes; | ||
} | ||
// Call the 'create' hook for each created node. | ||
options.hooks && options.hooks.create && createdVNodes.forEach(function (node) { | ||
options.hooks.create(node); | ||
}); | ||
return res; | ||
}; | ||
@@ -46,6 +59,6 @@ | ||
function convertNodes(nodes) { | ||
function convertNodes(nodes, createdVNodes) { | ||
if (nodes instanceof Array && nodes.length > 0) { | ||
return nodes.map(function (node) { | ||
return toVNode(node); | ||
return toVNode(node, createdVNodes); | ||
}); | ||
@@ -57,8 +70,11 @@ } else { | ||
function toVNode(node) { | ||
function toVNode(node, createdVNodes) { | ||
var newNode = void 0; | ||
if (node.type === 'text') { | ||
return (0, _utils.createTextVNode)(node.content); | ||
newNode = (0, _utils.createTextVNode)(node.content); | ||
} else { | ||
return (0, _h2.default)(node.name, buildVNodeData(node), convertNodes(node.children)); | ||
newNode = (0, _h2.default)(node.name, buildVNodeData(node), convertNodes(node.children, createdVNodes)); | ||
} | ||
createdVNodes.push(newNode); | ||
return newNode; | ||
} | ||
@@ -65,0 +81,0 @@ |
{ | ||
"name": "snabbdom-virtualize", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Library for turning strings and DOM nodes into virtual DOM nodes compatible with snabbdom.", | ||
@@ -29,2 +29,3 @@ "author": { | ||
"karma-chai": "^0.1.0", | ||
"karma-chai-sinon": "^0.1.5", | ||
"karma-chrome-launcher": "^0.2.2", | ||
@@ -39,2 +40,3 @@ "karma-firefox-launcher": "^0.1.7", | ||
"sinon": "^1.17.3", | ||
"sinon-chai": "^2.8.0", | ||
"snabbdom": "^0.3.0", | ||
@@ -47,4 +49,4 @@ "webpack": "^1.12.12" | ||
"dependencies": { | ||
"html-parse-stringify": "^1.0.2" | ||
"html-parse-stringify": "https://github.com/rayd/html-parse-stringify.git#7a9d6b4fb5eda23a7f8be0c5d5202da7b858b07e" | ||
} | ||
} |
@@ -68,2 +68,18 @@ # snabbdom-virtualize [![Build Status](https://travis-ci.org/appcues/snabbdom-virtualize.svg?branch=master)](https://travis-ci.org/appcues/snabbdom-virtualize) | ||
#### Hooks | ||
You can register a `create` hook with any of the `virtualize` functions. This will be called once for each vnode that was created. It's called after the virtualization process is completed. The function receives one argument - the `VNode` that was created. | ||
```javascript | ||
// The function passed as the 'create' hook is called 3 times: once for the | ||
// <div>, once for the <span> and once for the text node inside the <span>. | ||
virtualize("<div><span>Hi!</span></div>", { | ||
hooks: { | ||
create: function(vnode) { ... } | ||
} | ||
}); | ||
``` | ||
Hooks allow you to perform some operations on your VNodes after virtualization but before patching with snabbdom. | ||
### Project setup | ||
@@ -70,0 +86,0 @@ |
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
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
14296
256
98
19
1
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbuffer-crc32@0.2.13(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.21.0.3(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedes6-promise@4.2.8(transitive)
- Removedextend@3.0.2(transitive)
- Removedextract-zip@1.7.0(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfd-slicer@1.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedfs-extra@1.0.0(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhasha@2.2.0(transitive)
- Removedhtml-parse-stringify@1.0.3(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisexe@2.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsonfile@2.4.0(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedkew@0.7.0(transitive)
- Removedklaw@1.3.1(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.0.0(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedpend@1.2.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedphantomjs-prebuilt@2.1.16(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedprogress@1.1.8(transitive)
- Removedpsl@1.9.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedrequest@2.88.2(transitive)
- Removedrequest-progress@2.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedthrottleit@1.0.1(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removeduri-js@4.4.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
- Removedvoid-elements@1.0.0(transitive)
- Removedwhich@1.3.1(transitive)
- Removedyauzl@2.10.0(transitive)