Skip to content

Commit

Permalink
add Post parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 18, 2015
1 parent f6a7c07 commit d83302d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/js/models/marker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export const MARKUP_TYPES = ['b', 'a', 'i', 'em', 'strong'];
const MARKER_TYPE = 'marker';

export default class Marker {
import { detect } from 'content-kit-editor/utils/array-utils';

const Marker = class Marker {
constructor(value='', markups=[]) {
this.value = value;
this.markups = [];
this.type = MARKER_TYPE;

markups.forEach(m => this.addMarkup(m));
}

Expand Down Expand Up @@ -39,11 +44,7 @@ export default class Marker {
}

hasMarkup(type) {
for (let i=0; i<this.markups.length; i++) {
if (this.markups[i].type === type) {
return this.markups[i];
}
}
return detect(this.markups, markup => markup.type === type);
}

getMarkup(type) {
Expand All @@ -67,4 +68,6 @@ export default class Marker {

return [m1, m2];
}
}
};

export default Marker;
15 changes: 15 additions & 0 deletions src/js/parsers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Post from 'content-kit-editor/models/post';
import SectionParser from 'content-kit-editor/parsers/section';
import { forEach } from 'content-kit-editor/utils/array-utils';

export default {
parse(element) {
const post = new Post();

forEach(element.childNodes, child => {
post.appendSection(SectionParser.parse(child));
});

return post;
}
};
5 changes: 5 additions & 0 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { MARKUP_TYPES } from 'content-kit-editor/models/marker';
import { getAttributes } from 'content-kit-editor/utils/dom-utils';
import { forEach } from 'content-kit-editor/utils/array-utils';

/**
* parses an element into a section, ignoring any non-markup
* elements contained within
* @return {Section}
*/
export default {
parse(element) {
if (!this.isSectionElement(element)) {
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/parsers/post-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const {module, test} = QUnit;

import PostParser from 'content-kit-editor/parsers/post';
import Helpers from '../../test-helpers';

module('Unit: Parser: PostParser');

test('#parse can parse a single text node', (assert) => {
let element = Helpers.dom.makeDOM(t =>
t('div', {}, [t.text('some text')])
);

const post = PostParser.parse(element);
assert.ok(post, 'gets post');
assert.equal(post.sections.length, 1, 'has 1 section');

const s1 = post.sections[0];
assert.equal(s1.markers.length, 1, 's1 has 1 marker');
assert.equal(s1.markers[0].value, 'some text', 'has text');
});

test('#parse can parse a section element', (assert) => {
let element = Helpers.dom.makeDOM(t =>
t('div', {}, [
t('p', {}, [
t.text('some text')
])
])
);

const post = PostParser.parse(element);
assert.ok(post, 'gets post');
assert.equal(post.sections.length, 1, 'has 1 section');

const s1 = post.sections[0];
assert.equal(s1.markers.length, 1, 's1 has 1 marker');
assert.equal(s1.markers[0].value, 'some text', 'has text');
});

test('#parse can parse multiple elements', (assert) => {
let element = Helpers.dom.makeDOM(t =>
t('div', {}, [
t('p', {}, [
t.text('some text')
]),
t.text('some other text')
])
);

const post = PostParser.parse(element);
assert.ok(post, 'gets post');
assert.equal(post.sections.length, 2, 'has 2 sections');

const [s1, s2] = post.sections;
assert.equal(s1.markers.length, 1, 's1 has 1 marker');
assert.equal(s1.markers[0].value, 'some text');

assert.equal(s2.markers.length, 1, 's2 has 1 marker');
assert.equal(s2.markers[0].value, 'some other text');
});

0 comments on commit d83302d

Please sign in to comment.