Skip to content

Commit

Permalink
section has type='section' and tagName property
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 17, 2015
1 parent b757a3b commit e6509e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
15 changes: 9 additions & 6 deletions src/js/models/section.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
export const DEFAULT_TYPE = 'p';
export const SECTION_TYPES = [
export const DEFAULT_TAG_NAME = 'p';
export const SECTION_TAG_NAMES = [
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div'
];
const SECTION_TYPE = 'section';

const Section = class Section {
constructor(type=DEFAULT_TYPE, markers=[]) {
constructor(tagName=DEFAULT_TAG_NAME, markers=[]) {
this.markers = [];
this.type = type;
this.tagName = tagName;
this.type = SECTION_TYPE;

markers.forEach(m => this.appendMarker(m));
}

Expand Down Expand Up @@ -36,8 +39,8 @@ const Section = class Section {
right.push(rightMiddle);

return [
new Section(this.type, left),
new Section(this.type, right)
new Section(this.tagName, left),
new Section(this.tagName, right)
];
}

Expand Down
20 changes: 10 additions & 10 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const ELEMENT_NODE = 1;

import Section from 'content-kit-editor/models/section';
import {
DEFAULT_TYPE,
SECTION_TYPES
DEFAULT_TAG_NAME,
SECTION_TAG_NAMES
} from 'content-kit-editor/models/section';

import Marker from 'content-kit-editor/models/marker';
Expand All @@ -16,8 +16,8 @@ export default {
element = this.wrapInSectionElement(element);
}

const type = this.typeFromTagName(element.tagName);
const section = new Section(type);
const tagName = this.tagNameFromElement(element);
const section = new Section(tagName);
const state = {section, markups:[], text:''};

this.toArray(element.childNodes).forEach(el => {
Expand All @@ -34,7 +34,7 @@ export default {
},

wrapInSectionElement(element) {
const parent = document.createElement(DEFAULT_TYPE);
const parent = document.createElement(DEFAULT_TAG_NAME);
parent.appendChild(element);
return parent;
},
Expand Down Expand Up @@ -82,7 +82,7 @@ export default {
},

isSectionElement(element) {
return SECTION_TYPES.indexOf(element.tagName) !== -1;
return SECTION_TAG_NAMES.indexOf(element.tagName) !== -1;
},

markupFromElement(element) {
Expand Down Expand Up @@ -117,9 +117,9 @@ export default {
return arr;
},

typeFromTagName(tagName) {
let type = tagName.toLowerCase();
if (SECTION_TYPES.indexOf(tagName) === -1) { type = DEFAULT_TYPE; }
return type;
tagNameFromElement(element) {
let tagName = element.tagName.toLowerCase();
if (SECTION_TAG_NAMES.indexOf(tagName) === -1) { tagName = DEFAULT_TAG_NAME; }
return tagName;
}
};
8 changes: 4 additions & 4 deletions tests/unit/parsers/section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('#parse parses simple dom', (assert) => {
);

const section = SectionParser.parse(element);
assert.equal(section.type, 'p');
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 2, 'has 2 markers');
const [m1, m2] = section.markers;

Expand Down Expand Up @@ -61,7 +61,7 @@ test('#parse ignores non-markup elements like spans', (assert) => {
);

const section = SectionParser.parse(element);
assert.equal(section.type, 'p');
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 markers');
const [m1] = section.markers;

Expand Down Expand Up @@ -97,7 +97,7 @@ test('#parse joins contiguous text nodes separated by non-markup elements', (ass
);

const section = SectionParser.parse(element);
assert.equal(section.type, 'p');
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 markers');
const [m1] = section.markers;

Expand All @@ -109,7 +109,7 @@ test('#parse parses a single text node', (assert) => {
'text', {value: 'raw text'}
);
const section = SectionParser.parse(element);
assert.equal(section.type, 'p');
assert.equal(section.tagName, 'p');
assert.equal(section.markers.length, 1, 'has 1 marker');
assert.equal(section.markers[0].value, 'raw text');
});
Expand Down

0 comments on commit e6509e4

Please sign in to comment.