diff --git a/src/js/models/post-node-builder.js b/src/js/models/post-node-builder.js index 6568548e9..c53fb0b88 100644 --- a/src/js/models/post-node-builder.js +++ b/src/js/models/post-node-builder.js @@ -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; } } diff --git a/tests/acceptance/editor-commands-test.js b/tests/acceptance/editor-commands-test.js index 44a6f48e9..2c8b8fe72 100644 --- a/tests/acceptance/editor-commands-test.js +++ b/tests/acceptance/editor-commands-test.js @@ -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(); diff --git a/tests/unit/models/markup-section-test.js b/tests/unit/models/markup-section-test.js index 1ec621fae..eb1b56c2a 100644 --- a/tests/unit/models/markup-section-test.js +++ b/tests/unit/models/markup-section-test.js @@ -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!');