test('#fromNode when node is card section element or next to it', (assert) => {
  let editorOptions = { cards: [{
    name: 'some-card',
    type: 'dom',
    render() {
      return $('<div id="the-card">this is the card</div>')[0];
    }
  }]};
  editor = Helpers.mobiledoc.renderInto(editorElement,
    ({post, cardSection}) => {
    return post([cardSection('some-card')]);
  }, editorOptions);

  let nodes = {
    wrapper:     editorElement.firstChild,
    leftCursor:  editorElement.firstChild.firstChild,
    rightCursor: editorElement.firstChild.lastChild,
    cardDiv:     editorElement.firstChild.childNodes[1]
  };

  assert.ok(nodes.wrapper && nodes.leftCursor && nodes.rightCursor &&
            nodes.cardDiv,
            'precond - nodes');

  assert.equal(nodes.wrapper.tagName.toLowerCase(), 'div', 'precond - wrapper');
  assert.equal(nodes.leftCursor.textContent, ZWNJ, 'precond - left cursor');
  assert.equal(nodes.rightCursor.textContent, ZWNJ, 'precond - right cursor');
  assert.ok(nodes.cardDiv.className.indexOf(CARD_ELEMENT_CLASS_NAME) !== -1,
            'precond -card div');

  let renderTree = editor._renderTree;
  let cardSection = editor.post.sections.head;

  let leftPos  = cardSection.headPosition();
  let rightPos = cardSection.tailPosition();

  assert.positionIsEqual(Position.fromNode(renderTree, nodes.wrapper, 0),
                         leftPos, 'wrapper offset 0');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.wrapper, 1),
                         leftPos, 'wrapper offset 1');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.wrapper, 2),
                         rightPos, 'wrapper offset 2');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.leftCursor, 0),
                         leftPos, 'left cursor offset 0');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.leftCursor, 1),
                         leftPos, 'left cursor offset 1');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.rightCursor, 0),
                         rightPos, 'right cursor offset 0');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.rightCursor, 1),
                         rightPos, 'right cursor offset 1');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.cardDiv, 0),
                         leftPos, 'card div offset 0');
  assert.positionIsEqual(Position.fromNode(renderTree, nodes.cardDiv, 1),
                         leftPos, 'card div offset 1');
});
test('#fromNode when node is root element and offset is > 0', (assert) => {
  editor = Helpers.mobiledoc.renderInto(editorElement,
    ({post, markupSection, marker}) => {
    return post([markupSection('p', [marker('abc'), marker('123')])]);
  });

  let renderTree = editor._renderTree;
  let position = Position.fromNode(renderTree, editorElement, 1);

  assert.positionIsEqual(position, editor.post.tailPosition());
});
test('#fromNode when offset refers to one past the number of child nodes of the node', function(assert) {
  editor = Helpers.mobiledoc.renderInto(editorElement,
    ({post, markupSection, marker}) => {
    return post([markupSection('p', [marker('abc')])]);
  });

  let renderTree = editor._renderTree;
  let elementNode = editorElement.firstChild;
  let position = Position.fromNode(renderTree, elementNode, 1);

  assert.positionIsEqual(position, editor.post.tailPosition());
});
test('#fromNode when node is section node with offset', (assert) => {
  editor = Helpers.mobiledoc.renderInto(editorElement,
    ({post, markupSection, marker}) => {
    return post([markupSection('p', [marker('abc'), marker('123')])]);
  });

  let pNode = editorElement.firstChild;
  assert.equal(pNode.tagName.toLowerCase(), 'p', 'precond - correct node');

  let renderTree = editor._renderTree;
  let position = Position.fromNode(renderTree, pNode, 0);

  assert.positionIsEqual(position, editor.post.sections.head.headPosition());
});
test('#fromNode when node is marker text node', (assert) => {
  editor = Helpers.mobiledoc.renderInto(editorElement,
    ({post, markupSection, marker}) => {
    return post([markupSection('p', [marker('abc'), marker('123')])]);
  });

  let textNode = editorElement.firstChild  // p
                              .lastChild; // textNode

  assert.equal(textNode.textContent, '123', 'precond - correct text node');

  let renderTree = editor._renderTree;
  let position = Position.fromNode(renderTree, textNode, 2);

  let section = editor.post.sections.head;
  assert.positionIsEqual(position, new Position(section, 'abc'.length + 2));
});