guardian / scribe

DEPRECATED: A rich text editor framework for the web platform
http://guardian.github.io/scribe/
Apache License 2.0
3.51k stars 245 forks source link

HTMLFormatters: Normalize before Sanitize? #444

Closed gpbmike closed 3 years ago

gpbmike commented 8 years ago

I'm trying to use the sanitizer plugin together with the semantic elements plugin. The sanitizer plugin runs during the 'sanitize' phase before the semantic elements plugin runs in the 'normalize' phase.

  HTMLFormatterFactory.prototype.format = function (html) {
    var formatters = this.formatters.sanitize.concat(this.formatters.normalize);

    var formatted = formatters.reduce(function (formattedData, formatter) {
      return formatter(formattedData);
    }, html);

    return formatted;
  };

https://github.com/guardian/scribe/blob/a62186405271aa52c131ab8313a4f5f027096d6d/src/scribe.js#L322-L330

My problem is that if I pass <p><b>hello</b></p> into Scribe, the sanitizer plugin (with strong whitelisted, but not b) will be run first and strip out the italics tag resulting in <p>hello</p>. Ideally I would be able to switch out the <b> tag for a <strong> tag before the sanitize phase. If I whitelist both strong and b, <p><strong>hello</strong><p> is the result but that doesn't feel right. Is that the best way? Any suggestions?

rrees commented 8 years ago

Maybe we should allow configuration to override the ordering.

gpbmike commented 8 years ago

Is there a case to maintain separate 'sanitize' and 'normalize' phases? Could all HTMLFormatters be run in the order they are added?

scribe.use(firstFormatter);
scribe.use(secondFormatter);