Skip to content

Commit

Permalink
Ensure we correctly use the markup cache when creating markups
Browse files Browse the repository at this point in the history
fixes #80
  • Loading branch information
bantic committed Aug 27, 2015
1 parent 6f18a98 commit 72cb5c6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/js/models/post-node-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ export default class PostNodeBuilder {
return marker;
}

createMarkup(tagName, attributes) {
// Attributes is an array of [key1, value1, key2, value2, ...]
createMarkup(tagName, attributes=[]) {
tagName = normalizeTagName(tagName);

let markup;

if (attributes) {
if (attributes.length) {
// FIXME: This could also be cached
markup = new Markup(tagName, attributes);
} else {
if (this.markupCache[tagName]) {
markup = this.markupCache[tagName];
} else {
markup = this.markupCache[tagName];

if (!markup) {
markup = new Markup(tagName, attributes);
this.markupCache[tagName] = markup;
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/acceptance/editor-commands-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ test('highlight text, click "bold" button bolds text', (assert) => {
});
});

test('highlight text, click "bold", type more text, re-select text, bold button is active', (assert) => {
let done = assert.async();

setTimeout(() => {
Helpers.toolbar.clickButton(assert, 'bold');
assert.hasElement('#editor strong:contains(IS A)');

let boldTag = $('#editor strong:contains(IS A)')[0];
let textNode = boldTag.childNodes[0];
assert.equal(textNode.textContent, 'IS A', 'precond - correct node');

Helpers.dom.moveCursorTo(textNode, 'IS'.length);
Helpers.dom.insertText('X');

assert.hasElement('strong:contains(ISX A)', 'adds text to bold');

Helpers.dom.selectText('ISX A', editorElement);
Helpers.dom.triggerEvent(document, 'mouseup');

setTimeout(() => {
Helpers.toolbar.assertActiveButton(assert, 'bold');
done();
});
});
});

test('highlight text, click "heading" button turns text into h2 header', (assert) => {
const done = assert.async();

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/models/markup-section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ test('a section can append a marker', (assert) => {
assert.equal(s1.markers.length, 1);
});

test('postNodeBuilder#createMarkup returns a singleton object when it has no attributes', (assert) => {
let markup = builder.createMarkup('b');

let others = [
builder.createMarkup('B'),
builder.createMarkup('B', []),
builder.createMarkup('b', [])
];

others.forEach(other => {
assert.ok(markup === other, 'markup is the same object');
});
});

test('#splitMarker splits the marker at the offset', (assert) => {
const m1 = builder.createMarker('hi ');
const m2 = builder.createMarker('there!');
Expand Down

0 comments on commit 72cb5c6

Please sign in to comment.