<commit_before>import Ember from 'ember';
import Renderer from 'ember-mobiledoc-dom-renderer';
import { RENDER_TYPE } from 'ember-mobiledoc-dom-renderer';
import layout from '../templates/components/render-mobiledoc';
import { getDOM } from '../utils/dom';

const {
  assert,
  computed,
  run: { join },
  uuid
} = Ember;

const ADD_CARD_HOOK             = 'addComponentCard';
const REMOVE_CARD_HOOK          = 'removeComponentCard';
const ADD_ATOM_HOOK             = 'addComponentAtom';
const REMOVE_ATOM_HOOK          = 'removeComponentAtom';
const CARD_TAG_NAME             = 'div';
const ATOM_TAG_NAME             = 'span';
const UUID_PREFIX               = '__rendered-mobiledoc-entity-';
export const CARD_ELEMENT_CLASS = '__rendered-mobiledoc-card';
export const ATOM_ELEMENT_CLASS = '__rendered-mobiledoc-atom';

const CARD_HOOKS = {
  ADD:    ADD_CARD_HOOK,
  REMOVE: REMOVE_CARD_HOOK
};

const ATOM_HOOKS = {
  ADD:    ADD_ATOM_HOOK,
  REMOVE: REMOVE_ATOM_HOOK
};

function rendererFor(type) {
  let hookNames;

  if (type === 'card') {
    hookNames = CARD_HOOKS;
  } else if (type === 'atom') {
    hookNames = ATOM_HOOKS;
  }

  return function({env, options}) {
    let { onTeardown } = env;
    let addHook    = options[hookNames.ADD];
    let removeHook = options[hookNames.REMOVE];

    let { entity, element } = addHook(...arguments);
    onTeardown(() => removeHook(entity));

    return element;
  };
}

function createComponentCard(name) {
  return {
    name,
    type: RENDER_TYPE,
    render: rendererFor('card')
  };
}

function createComponentAtom(name) {
  return {
    name,
    type: RENDER_TYPE,
    render: rendererFor('atom')
  };
}

export default Ember.Component.extend({
  layout: layout,

  didReceiveAttrs() {
    let mobiledoc = this.get('mobiledoc');
    assert(`Must pass mobiledoc to render-mobiledoc component`, !!mobiledoc);

    if (this._teardownRender) {
      this._teardownRender();
      this._teardownRender = null;
    }
    this._renderMobiledoc();
  },

  // pass in an array of card names that the mobiledoc may have. These
  // map to component names using `cardNameToComponentName`
  cardNames: [],

  // pass in an array of atom names that the mobiledoc may have. These
  // map to component names using `atomNameToComponentName`
  atomNames: [],

  _mdcCards: computed('cardNames', function() {
    return this.get('cardNames').map(name => createComponentCard(name));
  }),

  _mdcAtoms: computed('atomNames', function() {
    return this.get('atomNames').map(name => createComponentAtom(name));
  }),

  _renderMobiledoc() {
    let domHelper = getDOM(this);
    let dom = domHelper.document;

    let mobiledoc = this.get('mobiledoc');

    let options = {
      cards: this.get('_mdcCards'),
      atoms: this.get('_mdcAtoms'),
      cardOptions: this.get('_cardOptions')
    };
    [
      'mobiledoc', 'sectionElementRenderer', 'markupElementRenderer',
       'unknownCardHandler', 'unknownAtomHandler'
    ].forEach(option => {
      let value = this.get(option);
      if (value) {
        options[option] = value;
      }
    });

    let renderer = new Renderer(options);
    let { result, teardown } = renderer.render(mobiledoc);

    // result is a document fragment, and glimmer2 errors when cleaning it up.
    // We must append the document fragment to a static wrapper.
    // Related: https://github.com/tildeio/glimmer/pull/331 and
    //          https://github.com/yapplabs/ember-wormhole/issues/66#issuecomment-246207622
    let wrapper = this._createElement(dom, 'div');<commit_msg>Pass dom to the renderer<commit_after>import Ember from 'ember';
import Renderer from 'ember-mobiledoc-dom-renderer';
import { RENDER_TYPE } from 'ember-mobiledoc-dom-renderer';
import layout from '../templates/components/render-mobiledoc';
import { getDOM } from '../utils/dom';

const {
  assert,
  computed,
  run: { join },
  uuid
} = Ember;

const ADD_CARD_HOOK             = 'addComponentCard';
const REMOVE_CARD_HOOK          = 'removeComponentCard';
const ADD_ATOM_HOOK             = 'addComponentAtom';
const REMOVE_ATOM_HOOK          = 'removeComponentAtom';
const CARD_TAG_NAME             = 'div';
const ATOM_TAG_NAME             = 'span';
const UUID_PREFIX               = '__rendered-mobiledoc-entity-';
export const CARD_ELEMENT_CLASS = '__rendered-mobiledoc-card';
export const ATOM_ELEMENT_CLASS = '__rendered-mobiledoc-atom';

const CARD_HOOKS = {
  ADD:    ADD_CARD_HOOK,
  REMOVE: REMOVE_CARD_HOOK
};

const ATOM_HOOKS = {
  ADD:    ADD_ATOM_HOOK,
  REMOVE: REMOVE_ATOM_HOOK
};

function rendererFor(type) {
  let hookNames;

  if (type === 'card') {
    hookNames = CARD_HOOKS;
  } else if (type === 'atom') {
    hookNames = ATOM_HOOKS;
  }

  return function({env, options}) {
    let { onTeardown } = env;
    let addHook    = options[hookNames.ADD];
    let removeHook = options[hookNames.REMOVE];

    let { entity, element } = addHook(...arguments);
    onTeardown(() => removeHook(entity));

    return element;
  };
}

function createComponentCard(name) {
  return {
    name,
    type: RENDER_TYPE,
    render: rendererFor('card')
  };
}

function createComponentAtom(name) {
  return {
    name,
    type: RENDER_TYPE,
    render: rendererFor('atom')
  };
}

export default Ember.Component.extend({
  layout: layout,

  didReceiveAttrs() {
    let mobiledoc = this.get('mobiledoc');
    assert(`Must pass mobiledoc to render-mobiledoc component`, !!mobiledoc);

    if (this._teardownRender) {
      this._teardownRender();
      this._teardownRender = null;
    }
    this._renderMobiledoc();
  },

  // pass in an array of card names that the mobiledoc may have. These
  // map to component names using `cardNameToComponentName`
  cardNames: [],

  // pass in an array of atom names that the mobiledoc may have. These
  // map to component names using `atomNameToComponentName`
  atomNames: [],

  _mdcCards: computed('cardNames', function() {
    return this.get('cardNames').map(name => createComponentCard(name));
  }),

  _mdcAtoms: computed('atomNames', function() {
    return this.get('atomNames').map(name => createComponentAtom(name));
  }),

  _renderMobiledoc() {
    let domHelper = getDOM(this);
    let dom = domHelper.document;

    let mobiledoc = this.get('mobiledoc');

    let options = {
      dom,
      cards: this.get('_mdcCards'),
      atoms: this.get('_mdcAtoms'),
      cardOptions: this.get('_cardOptions')
    };
    [
      'mobiledoc', 'sectionElementRenderer', 'markupElementRenderer',
       'unknownCardHandler', 'unknownAtomHandler'
    ].forEach(option => {
      let value = this.get(option);
      if (value) {
        options[option] = value;
      }
    });

    let renderer = new Renderer(options);
    let { result, teardown } = renderer.render(mobiledoc);

    // result is a document fragment, and glimmer2 errors when cleaning it up.
    // We must append the document fragment to a static wrapper.
    // Related: https://github.com/tildeio/glimmer/pull/331 and
    //          https://github.com/yapplabs/ember-wormhole/issues/66#issuecomment-246207622
    let wrapper = this._createElement(dom, 'div');
{'fraction_non_alphanumeric': 0.12496501539322698, 'fraction_numerical': 0.0041981528127623844, 'mean_word_length': 3.2289940828402366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5508256', 'n_tokens_mistral': 2370, 'n_tokens_neox': 2323, 'n_words': 528}