Skip to content

Commit

Permalink
chore: Replace deprecated mobiledoc-html-renderer with mobiledoc-dom-…
Browse files Browse the repository at this point in the history
…renderer (#538)

Fallback to text serialization when html not available
  • Loading branch information
bantic authored Mar 9, 2017
1 parent e9d0276 commit 2295cfc
Show file tree
Hide file tree
Showing 6 changed files with 1,009 additions and 745 deletions.
4 changes: 2 additions & 2 deletions Brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var replace = require('broccoli-string-replace');
var demoTree = require('./broccoli/demo');

var vendoredModules = [
{name: 'mobiledoc-html-renderer'},
{name: 'mobiledoc-text-renderer'}
{ name: 'mobiledoc-dom-renderer' },
{ name: 'mobiledoc-text-renderer' }
];

var cssFiles = new Funnel('src/css', {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
],
"license": "MIT",
"dependencies": {
"mobiledoc-html-renderer": "0.3.1",
"mobiledoc-text-renderer": "0.3.1"
"mobiledoc-dom-renderer": "0.6.5",
"mobiledoc-text-renderer": "0.3.2"
},
"devDependencies": {
"broccoli": "^0.16.9",
Expand Down
16 changes: 12 additions & 4 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import RenderTree from 'mobiledoc-kit/models/render-tree';
import mobiledocRenderers from '../renderers/mobiledoc';
import { MOBILEDOC_VERSION } from 'mobiledoc-kit/renderers/mobiledoc';
import { mergeWithOptions } from '../utils/merge';
import { normalizeTagName, clearChildNodes } from '../utils/dom-utils';
import { normalizeTagName, clearChildNodes, serializeHTML } from '../utils/dom-utils';
import { forEach, filter, contains, values, detect } from '../utils/array-utils';
import { setData } from '../utils/element-utils';
import Cursor from '../utils/cursor';
import Range from '../utils/cursor/range';
import Position from '../utils/cursor/position';
import Environment from '../utils/environment';
import PostNodeBuilder from '../models/post-node-builder';
import { DEFAULT_TEXT_INPUT_HANDLERS } from './text-input-handlers';
import {
Expand All @@ -27,7 +28,7 @@ import MutationHandler from 'mobiledoc-kit/editor/mutation-handler';
import EditHistory from 'mobiledoc-kit/editor/edit-history';
import EventManager from 'mobiledoc-kit/editor/event-manager';
import EditState from 'mobiledoc-kit/editor/edit-state';
import HTMLRenderer from 'mobiledoc-html-renderer';
import DOMRenderer from 'mobiledoc-dom-renderer';
import TextRenderer from 'mobiledoc-text-renderer';
import LifecycleCallbacks from 'mobiledoc-kit/models/lifecycle-callbacks';
import LogManager from 'mobiledoc-kit/utils/log-manager';
Expand Down Expand Up @@ -561,8 +562,15 @@ class Editor {

switch (format) {
case 'html':
rendered = new HTMLRenderer(rendererOptions).render(mobiledoc);
return rendered.result;
let result;
if (Environment.hasDOM()) {
rendered = new DOMRenderer(rendererOptions).render(mobiledoc);
result = `<div>${serializeHTML(rendered.result)}</div>`;
} else {
// Fallback to text serialization
result = this.serializePost(post, 'text', options);
}
return result;
case 'text':
rendered = new TextRenderer(rendererOptions).render(mobiledoc);
return rendered.result;
Expand Down
11 changes: 9 additions & 2 deletions src/js/utils/dom-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ function normalizeTagName(tagName) {
}

function parseHTML(html) {
var div = document.createElement('div');
const div = document.createElement('div');
div.innerHTML = html;
return div;
}

function serializeHTML(node) {
const div = document.createElement('div');
div.appendChild(node);
return div.innerHTML;
}

export {
containsNode,
clearChildNodes,
Expand All @@ -109,5 +115,6 @@ export {
isTextNode,
isCommentNode,
isElementNode,
parseHTML
parseHTML,
serializeHTML
};
5 changes: 5 additions & 0 deletions src/js/utils/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
hasDOM() {
return typeof document !== 'undefined';
}
};
Loading

0 comments on commit 2295cfc

Please sign in to comment.