bustle/mobiledoc-kit

addSection in parserPlugins obliterate previous section content in certain cases

Closed this issue · 2 comments

For a demo see https://jsfiddle.net/1rmndk8w/4/

The way I discovered this was by copying & pasting a github comment with an image. I found that the text immediately preceding the image was gone after pasting.

I believe this is because state.text is not properly tracking everything but I'm not exactly intimately familiar with the code so I'm not certain.

      addSection: (section) => {
        // avoid creating empty paragraphs due to wrapper elements around
        // parser-plugin-handled elements
        if (this.state.section.isMarkerable && !this.state.text) {
          this.state.section = null;
        } else {
          this._closeCurrentSection();
        }
        this.sections.push(section);
      },

One thing that is interesting is that in this case, this.state.section.text has the text but this.state.text is an empty string. Perhaps this check could be changed to include this.state.section.text?

/cc @kevinansfield

@sdhull you were spot on with the required change to the conditional, PR opened with the change and a test here #685

The reason this.state.text was blank yet this.state.section.text wasn't is because the <a> element parsing calls createMarker() which migrates the temporary this.state.text into a marker in the section.

Thank you @kevinansfield! 🙏