Socket
Socket
Sign inDemoInstall

snabbdom-virtualize

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snabbdom-virtualize - npm Package Compare versions

Comparing version 0.4.0 to 0.5.0

6

lib/index.js

@@ -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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc