From cf4584610356cb5989fda608d18270bcd94e9a8c Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Tue, 3 Nov 2015 17:20:59 -0500 Subject: [PATCH] Remove Post parser, its reparse methods are now in DOM parser --- src/js/parsers/post.js | 149 -------------------------------------- src/js/parsers/section.js | 5 -- 2 files changed, 154 deletions(-) delete mode 100644 src/js/parsers/post.js diff --git a/src/js/parsers/post.js b/src/js/parsers/post.js deleted file mode 100644 index 561e83ffa..000000000 --- a/src/js/parsers/post.js +++ /dev/null @@ -1,149 +0,0 @@ -import { - MARKUP_SECTION_TYPE, - LIST_SECTION_TYPE, - LIST_ITEM_TYPE -} from '../models/types'; - -import SectionParser from 'content-kit-editor/parsers/section'; -import { forEach } from 'content-kit-editor/utils/array-utils'; -import { getAttributes, walkTextNodes } from '../utils/dom-utils'; -import Markup from 'content-kit-editor/models/markup'; - -const TAG_REMAPPING = { - 'b': 'strong', - 'i': 'em' -}; - -function normalizeTagName(tagName) { - let normalized = tagName.toLowerCase(); - let remapped = TAG_REMAPPING[normalized]; - return remapped || normalized; -} - -export default class PostParser { - constructor(builder) { - this.builder = builder; - this.sectionParser = new SectionParser(this.builder); - } - - parse(element) { - const post = this.builder.createPost(); - - forEach(element.childNodes, child => { - post.sections.append(this.sectionParser.parse(child)); - }); - - return post; - } - - parseSection(element, otherArg) { - if (!!otherArg) { - element = otherArg; // hack to deal with passed previousSection - } - return this.sectionParser.parse(element); - } - - // walk up from the textNode until the rootNode, converting each - // parentNode into a markup - collectMarkups(textNode, rootNode) { - let markups = []; - let currentNode = textNode.parentNode; - while (currentNode && currentNode !== rootNode) { - let markup = this.markupFromNode(currentNode); - if (markup) { - markups.push(markup); - } - - currentNode = currentNode.parentNode; - } - return markups; - } - - // Turn an element node into a markup - markupFromNode(node) { - if (Markup.isValidElement(node)) { - const tagName = node.tagName; - const attributes = getAttributes(node); - return this.builder.createMarkup(normalizeTagName(tagName), attributes); - } - } - - // FIXME should move to the section parser? - // FIXME the `collectMarkups` logic could simplify the section parser? - reparseSection(section, renderTree) { - switch (section.type) { - case LIST_SECTION_TYPE: - return this.reparseListSection(section, renderTree); - case LIST_ITEM_TYPE: - return this.reparseListItem(section, renderTree); - case MARKUP_SECTION_TYPE: - return this.reparseMarkupSection(section, renderTree); - default: - return; // can only parse the above types - } - } - - reparseMarkupSection(section, renderTree) { - return this._reparseSectionContainingMarkers(section, renderTree); - } - - reparseListItem(listItem, renderTree) { - return this._reparseSectionContainingMarkers(listItem, renderTree); - } - - reparseListSection(listSection, renderTree) { - listSection.items.forEach(li => this.reparseListItem(li, renderTree)); - } - - _reparseSectionContainingMarkers(section, renderTree) { - const element = section.renderNode.element; - let seenRenderNodes = []; - let previousMarker; - - walkTextNodes(element, (textNode) => { - const text = textNode.textContent; - let markups = this.collectMarkups(textNode, element); - - let marker; - - let renderNode = renderTree.getElementRenderNode(textNode); - if (renderNode) { - if (text.length) { - marker = renderNode.postNode; - marker.value = text; - marker.markups = markups; - } else { - renderNode.scheduleForRemoval(); - } - } else { - marker = this.builder.createMarker(text, markups); - - renderNode = renderTree.buildRenderNode(marker); - renderNode.element = textNode; - renderNode.markClean(); - - let previousRenderNode = previousMarker && previousMarker.renderNode; - section.markers.insertAfter(marker, previousMarker); - section.renderNode.childNodes.insertAfter(renderNode, previousRenderNode); - - let parentNodeCount = marker.closedMarkups.length; - let nextMarkerElement = textNode.parentNode; - while (parentNodeCount--) { - nextMarkerElement = nextMarkerElement.parentNode; - } - renderNode.nextMarkerElement = nextMarkerElement; - } - - seenRenderNodes.push(renderNode); - previousMarker = marker; - }); - - let renderNode = section.renderNode.childNodes.head; - while (renderNode) { - if (seenRenderNodes.indexOf(renderNode) === -1) { - renderNode.scheduleForRemoval(); - } - renderNode = renderNode.next; - } - } -} diff --git a/src/js/parsers/section.js b/src/js/parsers/section.js index 72c496d50..747138176 100644 --- a/src/js/parsers/section.js +++ b/src/js/parsers/section.js @@ -172,11 +172,6 @@ export default class SectionParser { state.section = null; } - isSectionElement(element) { - return element.nodeType === ELEMENT_NODE && - VALID_MARKUP_SECTION_TAGNAMES.indexOf(normalizeTagName(element.tagName)) !== -1; - } - _markupsFromElement(element) { let { builder } = this; let markups = [];