summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/rich_content_editor/services/renderers/render_html_block.js
blob: 18bd17d43d9346e52fefb3b078277c646d738b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { buildUneditableHtmlAsTextTokens } from './build_uneditable_token';
import { ALLOWED_VIDEO_ORIGINS } from '../../constants';
import { getURLOrigin } from '~/lib/utils/url_utility';

const isVideoFrame = html => {
  const parser = new DOMParser();
  const doc = parser.parseFromString(html, 'text/html');
  const {
    children: { length },
  } = doc;
  const iframe = doc.querySelector('iframe');
  const origin = iframe && getURLOrigin(iframe.getAttribute('src'));

  return length === 1 && ALLOWED_VIDEO_ORIGINS.includes(origin);
};

const canRender = ({ type, literal }) => {
  return type === 'htmlBlock' && !isVideoFrame(literal);
};

const render = node => buildUneditableHtmlAsTextTokens(node);

export default { canRender, render };