Skip to content

Commit

Permalink
use markup model
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 19, 2015
1 parent 875d31c commit 63b8fbf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
9 changes: 1 addition & 8 deletions src/js/models/marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@ const Marker = class Marker {
}

addMarkup(markup) {
// simple markup, no attributes
if (typeof markup === 'string') {
markup = {tagName: markup};
}
let {tagName, attributes} = markup;
tagName = tagName.toLowerCase();
const {tagName} = markup;

if (MARKUP_TAG_NAMES.indexOf(tagName) === -1) {
throw new Error(`Cannot add markup of tagName ${tagName}`);
}

markup = {tagName, attributes};

if (!this.hasMarkup(tagName)) {
this.markups.push(markup);
}
Expand Down
9 changes: 9 additions & 0 deletions src/js/models/markup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const MARKUP_TYPE = 'markup';

export default class Markup {
constructor(tagName, attributes={}) {
this.tagName = tagName.toLowerCase();
this.attributes = attributes;
this.type = MARKUP_TYPE;
}
}
6 changes: 2 additions & 4 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'content-kit-editor/models/section';

import Marker from 'content-kit-editor/models/marker';
import Markup from 'content-kit-editor/models/markup';
import { MARKUP_TAG_NAMES } from 'content-kit-editor/models/marker';
import { getAttributes } from 'content-kit-editor/utils/dom-utils';
import { forEach } from 'content-kit-editor/utils/array-utils';
Expand Down Expand Up @@ -99,10 +100,7 @@ export default {
const tagName = element.tagName.toLowerCase();
if (MARKUP_TAG_NAMES.indexOf(tagName) === -1) { return null; }

return {
tagName: tagName,
attributes: getAttributes(element)
};
return new Markup(tagName, getAttributes(element));
},

sectionTagNameFromElement(element) {
Expand Down
19 changes: 10 additions & 9 deletions tests/unit/models/marker-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {module, test} = QUnit;

import Marker from 'content-kit-editor/models/marker';
import Markup from 'content-kit-editor/models/markup';

module('Unit: Marker');

Expand Down Expand Up @@ -28,22 +29,22 @@ test('a marker can truncated to an offset', (assert) => {

test('a marker can have a markup applied to it', (assert) => {
const m1 = new Marker('hi there!');
m1.addMarkup('b');
m1.addMarkup(new Markup('b'));

assert.ok(m1.hasMarkup('b'));
});

test('a marker cannot have the same markup tagName applied twice', (assert) => {
const m1 = new Marker('hi there!');
m1.addMarkup('b');
m1.addMarkup('b');
m1.addMarkup(new Markup('b'));
m1.addMarkup(new Markup('b'));

assert.equal(m1.markups.length, 1, 'markup only applied once');
});

test('a marker can have a complex markup applied to it', (assert) => {
const m1 = new Marker('hi there!');
const markup = {tagName: 'a', attributes:{href:'blah'}};
const markup = new Markup('a', {href:'blah'});
m1.addMarkup(markup);

assert.ok(m1.hasMarkup('a'));
Expand All @@ -52,8 +53,8 @@ test('a marker can have a complex markup applied to it', (assert) => {

test('a marker cannot have the same complex markup tagName applied twice, even with different attributes', (assert) => {
const m1 = new Marker('hi there!');
const markup1 = {tagName: 'a', attributes:{href:'blah'}};
const markup2 = {tagName: 'a', attributes:{href:'blah2'}};
const markup1 = new Markup('a', {href:'blah'});
const markup2 = new Markup('a', {href:'blah2'});
m1.addMarkup(markup1);
m1.addMarkup(markup2);

Expand All @@ -64,9 +65,9 @@ test('a marker cannot have the same complex markup tagName applied twice, even w

test('a marker can be joined to another', (assert) => {
const m1 = new Marker('hi');
m1.addMarkup('b');
m1.addMarkup(new Markup('b'));
const m2 = new Marker(' there!');
m2.addMarkup('i');
m2.addMarkup(new Markup('i'));

const m3 = m1.join(m2);
assert.equal(m3.value, 'hi there!');
Expand All @@ -76,7 +77,7 @@ test('a marker can be joined to another', (assert) => {

test('a marker can be split into two', (assert) => {
const m1 = new Marker('hi there!');
m1.addMarkup('b');
m1.addMarkup(new Markup('b'));

const [_m1, m2] = m1.split(5);
assert.ok(_m1.hasMarkup('b') && m2.hasMarkup('b'),
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/models/section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {module, test} = QUnit;

import Section from 'content-kit-editor/models/section';
import Marker from 'content-kit-editor/models/marker';
import Markup from 'content-kit-editor/models/markup';

module('Unit: Section');

Expand Down Expand Up @@ -78,7 +79,7 @@ test('#markerContaining finds the marker at the given offset when multiple marke
});

test('a section can be split, splitting its markers', (assert) => {
const m = new Marker('hi there!', ['b']);
const m = new Marker('hi there!', [new Markup('b')]);
const s = new Section('p', [m]);

const [s1, s2] = s.split(5);
Expand Down

0 comments on commit 63b8fbf

Please sign in to comment.