Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure we correctly use the markup cache when creating markups #99

Merged
merged 1 commit into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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