How to use the diffhtml.createTree function in diffhtml

To help you get started, we’ve selected a few diffhtml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tbranyen / diffhtml / packages / diffhtml-render-to-string / index.js View on Github external
tasks.add(function reconcileTrees(transaction) {
  const { domNode, markup, options } = transaction;

  // If we are in a render transaction where no markup was previously parsed
  // then reconcile trees will attempt to create a tree based on the incoming
  // markup (JSX/html/etc).
  if (!transaction.newTree) {
    transaction.newTree = createTree(markup);
  }

  // Associate the old tree with this brand new transaction. Always ensure that
  // diffing happens at the root level. This avoids the unnecessary REPLACE
  // operation that would normally occur under `innerHTML`.
  transaction.oldTree = domNode;

  //console.log(transaction.oldTree);

  // Create a fake, but fast DOM node, replacing the VTree passed in.
  transaction.domNode = makeDOMNode(domNode);
});
github tbranyen / diffhtml / packages / diffhtml-components / lib / tasks / react-like-component.js View on Github external
if (oldInstance.shouldComponentUpdate()) {
      renderTree = oldInstance.render(props, oldInstance.state);
      oldInstance.componentDidUpdate();
    }

    ComponentTreeCache.set(oldInstance, renderTree);
    InstanceCache.set(renderTree, oldInstance);
    oldTree.childNodes.splice(oldTree.childNodes.indexOf(newTree), 1, renderTree);

    return renderTree;
  }
  else if (instance && instance.render) {
    renderTree = createTree(instance.render(props, instance.state));
  }
  else {
    renderTree = createTree(newCtor(props));
  }

  // Nothing was rendered so continue.
  if (!renderTree) {
    return null;
  }

  // Replace the rendered value into the new tree, if rendering a fragment
  // this will inject the contents into the parent.
  if (typeof renderTree.rawNodeName === 'string' && renderTree.nodeType === 11) {
    newTree.childNodes = [...renderTree.childNodes];

    if (instance) {
      ComponentTreeCache.set(instance, oldTree);
      InstanceCache.set(oldTree, instance);
    }
github tbranyen / diffhtml / packages / diffhtml-components / lib / shared / render-component.js View on Github external
if (instance.shouldComponentUpdate()) {
      renderTree = createTree(instance.render(props, instance.state, context));

      if (instance.componentDidUpdate) {
        instance.componentDidUpdate(instance.props, instance.state);
      }
    }
  }
  // New class instance.
  else if (isNewable) {
    instance = new Component(props, context);
    InstanceCache.set(vTree, instance);
    instance[$$vTree] = vTree;

    renderTree = createTree(instance.render(props, instance.state, context));
  }
  else {
    renderTree = createTree(Component(props, context));
  }

  // Associate the children with the parent component that rendered them, this
  // is used to trigger lifecycle events.
  const linkTrees = childNodes => {
    for (let i = 0; i < childNodes.length; i++) {
      const newTree = childNodes[i];

      // If the newTree is not a Fragment, associate the `newTree` with the
      // originating `vTree`.
      if (newTree && newTree.nodeType !== 11) {
        ComponentTreeCache.set(newTree, vTree);
      }
github tbranyen / diffhtml / packages / diffhtml-components / lib / tasks / react-like-component.js View on Github external
oldInstance.props = props;
    InstanceCache.delete(ComponentTreeCache.get(oldInstance));

    if (oldInstance.shouldComponentUpdate()) {
      renderTree = oldInstance.render(props, oldInstance.state);
      oldInstance.componentDidUpdate();
    }

    ComponentTreeCache.set(oldInstance, renderTree);
    InstanceCache.set(renderTree, oldInstance);
    oldTree.childNodes.splice(oldTree.childNodes.indexOf(newTree), 1, renderTree);

    return renderTree;
  }
  else if (instance && instance.render) {
    renderTree = createTree(instance.render(props, instance.state));
  }
  else {
    renderTree = createTree(newCtor(props));
  }

  // Nothing was rendered so continue.
  if (!renderTree) {
    return null;
  }

  // Replace the rendered value into the new tree, if rendering a fragment
  // this will inject the contents into the parent.
  if (typeof renderTree.rawNodeName === 'string' && renderTree.nodeType === 11) {
    newTree.childNodes = [...renderTree.childNodes];

    if (instance) {
github tbranyen / diffhtml / packages / diffhtml-components / lib / shared / render-component.js View on Github external
if (instance.componentDidUpdate) {
        instance.componentDidUpdate(instance.props, instance.state);
      }
    }
  }
  // New class instance.
  else if (isNewable) {
    instance = new Component(props, context);
    InstanceCache.set(vTree, instance);
    instance[$$vTree] = vTree;

    renderTree = createTree(instance.render(props, instance.state, context));
  }
  else {
    renderTree = createTree(Component(props, context));
  }

  // Associate the children with the parent component that rendered them, this
  // is used to trigger lifecycle events.
  const linkTrees = childNodes => {
    for (let i = 0; i < childNodes.length; i++) {
      const newTree = childNodes[i];

      // If the newTree is not a Fragment, associate the `newTree` with the
      // originating `vTree`.
      if (newTree && newTree.nodeType !== 11) {
        ComponentTreeCache.set(newTree, vTree);
      }
      else if (newTree) {
        linkTrees(newTree.childNodes);
      }
github mAAdhaTTah / brookjs / packages / brookjs-silt / src / generate / index.js View on Github external
export default R.curry(function generate (ast: SiltNode, context: ContextSource): vTree {
    // We'll handle null special, since it's valid.
    if (ast == null) {
        return createTree(null);
    }

    if (!Array.isArray(ast)) {
        throw new TypeError(`invalid ast: not an array, got type ${typeof ast}`);
    }

    const [tag, attributes, children] = ast;

    switch (tag) {
        case 'hbs:comment':
            return createTree(null);
        case 'hbs:expression':
            // $FlowFixMe
            const next = handleExpression(attributes, context);

            if (typeof next === 'string') {
github mAAdhaTTah / brookjs / packages / brookjs-silt / src / __tests__ / generate.spec.js View on Github external
it('should generate an if block if the context is true', () => {
        const ast = ['hbs:block', {
            args: undefined,
            context: 'bar',
            block: 'if',
        }, [
            ['#text', [], 'foo!']
        ]];
        const context = { bar: true };
        const expected = createTree('#document-fragment', {}, [
            createTree('#text', {}, 'foo!')
        ]);

        expect(generate(ast, context)).to.deep.equal(expected);
    });
github mAAdhaTTah / brookjs / packages / brookjs-silt / src / generate / block.js View on Github external
export function handleBlock (meta: BlockMeta, children: Array, context: ContextSource): vTree {
    switch (meta.block) {
        case 'if':
            if (getContextValue(meta.context, context)) {
                return generate(['#document-fragment', [], children], context);
            }

            return createTree(null);
        case 'unless':
            if (!getContextValue(meta.context, context)) {
                return generate(['#document-fragment', [], children], context);
            }

            return createTree(null);
        default:
            return createTree(null);
    }
}
github tbranyen / diffhtml / packages / diffhtml-render-to-string / index.js View on Github external
exports.renderToString = function renderToString(markup, options = {}) {
  const parseHTML = options.strict ? html.strict : html;
  const childNodes = typeof markup === 'string' ? parseHTML(markup) : markup;
  const newTree = createTree('#document-fragment', {}, childNodes);
  const oldTree = createTree(newTree.rawNodeName);

  return innerHTML(oldTree, newTree, { tasks: [...tasks], options });
};