Skip to content

Commit

Permalink
Ensure rendered editor dom closes markup tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 28, 2015
1 parent f95aa2b commit 12c20af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ var sampleMobiledocs = {
]],
[1, "P", [
[[], 0, "see it "],
[[0], 1, "on github."]
[[0], 1, "on github"],
[[], 0, "."]
]]
]
],
Expand Down
3 changes: 3 additions & 0 deletions src/js/models/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export const VALID_MARKUP_TAGNAMES = [
];

export default class Markup {
/*
* @param {attributes} array flat array of key1,value1,key2,value2,...
*/
constructor(tagName, attributes=[]) {
this.tagName = tagName.toLowerCase();
this.attributes = attributes;
Expand Down
22 changes: 11 additions & 11 deletions src/js/renderers/editor-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { POST_TYPE } from "../models/post";
import { MARKUP_SECTION_TYPE } from "../models/markup-section";
import { IMAGE_SECTION_TYPE } from "../models/image";

function createElementFromMarkerType(doc, markerType) {
var element = doc.createElement(markerType.tagName);
if (markerType.attributes) {
for (var i=0, l=markerType.attributes.length;i<l;i=i+2) {
element.setAttribute(markerType.attributes[i], markerType.attributes[i+1]);
function createElementFromMarkup(doc, markup) {
var element = doc.createElement(markup.tagName);
if (markup.attributes) {
for (var i=0, l=markup.attributes.length;i<l;i=i+2) {
element.setAttribute(markup.attributes[i], markup.attributes[i+1]);
}
}
return element;
Expand All @@ -20,25 +20,25 @@ function renderMarkupSection(doc, section, markers) {
var elements = [element];
var currentElement = element;
var i, l, j, m, marker, openTypes, closeTypes, text;
var markerType;
var markup;
var openedElement;
for (i=0, l=markers.length;i<l;i++) {
marker = markers[i];
openTypes = marker.markups;
closeTypes = marker.close;
openTypes = marker.openedMarkups;
closeTypes = marker.closedMarkups;
text = marker.value;

for (j=0, m=openTypes.length;j<m;j++) {
markerType = openTypes[j];
openedElement = createElementFromMarkerType(doc, markerType);
markup = openTypes[j];
openedElement = createElementFromMarkup(doc, markup);
currentElement.appendChild(openedElement);
elements.push(openedElement);
currentElement = openedElement;
}

currentElement.appendChild(doc.createTextNode(text));

for (j=0, m=closeTypes;j<m;j++) {
for (j=0, m=closeTypes.length;j<m;j++) {
elements.pop();
currentElement = elements[elements.length-1];
}
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/renderers/editor-dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,38 @@ test('renders a post with marker', (assert) => {
assert.equal(node.element.innerHTML, '<p><strong>Hi</strong></p>');
});

test('renders a post with multiple markers', (assert) => {
let post = builder.generatePost();
let section = builder.generateMarkupSection('P');
post.appendSection(section);

let bMarkup = builder.generateMarkup('B');
let iMarkup = builder.generateMarkup('I');

section.appendMarker(builder.generateMarker([], 'hello '));
section.appendMarker(
builder.generateMarker([
bMarkup
], 'bold, ')
);
section.appendMarker(
builder.generateMarker([
bMarkup,
iMarkup
], 'italic,')
);
section.appendMarker(
builder.generateMarker([], ' world.')
);

let node = new RenderNode(post);
let renderTree = new RenderTree(node);
node.renderTree = renderTree;
render(renderTree);
assert.equal(node.element.innerHTML, '<p>hello <b>bold, <i>italic,</i></b> world.</p>');
});


test('renders a post with image', (assert) => {
let url = DATA_URL;
let post = builder.generatePost();
Expand Down

0 comments on commit 12c20af

Please sign in to comment.