All files / src Parser.js

99.28% Statements 137/138
97.58% Branches 121/124
100% Functions 15/15
99.28% Lines 137/138
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453                                                                    8x 8x 8x                               188x 154x 34x 5x     183x 183x 183x 183x 183x               67x 51x                         2476x 2476x 2476x 2476x   2476x 2722x 2722x     2722x         16x       2706x 30x       2676x 3197x     3197x     3197x   3197x               2476x 302x       2174x 2174x   2174x 3197x   3197x     3197x 2618x       3197x     3197x       3197x       2174x 1450x     2174x             2774x 6x       2768x         14x       2754x         3x       2751x 13x       2738x 2x       2736x 7x     2729x                 191x   191x 25x       166x     166x     166x   166x                 188x   188x 4x       184x     184x               113x 113x 113x   113x 1x     112x 80x 80x 80x     80x 2x         78x 77x         13x         65x     65x 21x     44x 5x       39x     65x 65x     112x 52x     60x               2797x 2789x           8x             80x         80x 13x       13x 2x       11x   11x               67x                   38x                     130x 130x 130x   130x   224x 75x 75x     75x 2x       73x 50x 50x           73x 57x     57x 57x         57x 17x     57x 13x     57x                   16x       149x 147x       147x 14x   133x         130x 79x     130x      
/**
 * @copyright   2016, Miles Johnson
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */
 
/* eslint-disable no-cond-assign, no-undef */
 
import React from 'react';
import Matcher from './Matcher';
import Filter from './Filter';
import ElementComponent from './components/Element';
import {
  FILTER_DENY,
  FILTER_CAST_NUMBER,
  FILTER_CAST_BOOL,
  TAGS,
  TAGS_BLACKLIST,
  ATTRIBUTES,
  ATTRIBUTES_TO_PROPS,
  TYPE_INLINE,
  TYPE_BLOCK,
  CONFIG_BLOCK,
} from './constants';
 
import type {
  Attributes,
  PrimitiveType,
  ParsedNodes,
  NodeConfig,
  NodeInterface,
  ElementProps,
} from './types';
 
const ELEMENT_NODE: number = 1;
const TEXT_NODE: number = 3;
const INVALID_ROOTS: string[] = ['!DOC', 'HTML', 'HEAD', 'BODY'];
 
export default class Parser {
  doc: Document;
  content: ParsedNodes;
  props: Object;
  matchers: Matcher<*>[];
  filters: Filter[];
  keyIndex: number;
 
  constructor(
    markup: string,
    props: Object = {},
    matchers: Matcher<*>[] = [],
    filters: Filter[] = [],
  ) {
    if (!markup) {
      markup = '';
    } else if (typeof markup !== 'string') {
      throw new TypeError('Interweave parser requires a valid string.');
    }
 
    this.props = props;
    this.matchers = matchers;
    this.filters = filters;
    this.keyIndex = -1;
    this.doc = this.createDocument(markup);
  }
 
  /**
   * Loop through and apply all registered attribute filters to the
   * provided value.
   */
  applyFilters(attribute: string, value: string): string {
    return this.filters.reduce((newValue, filter) => (
      (filter.attribute === attribute) ? filter.filter(newValue) : newValue
    ), value);
  }
 
  /**
   * Loop through and apply all registered matchers to the string.
   * If a match is found, create a React element, and build a new array.
   * This array allows React to interpolate and render accordingly.
   */
  applyMatchers(
    string: string,
    parentConfig: NodeConfig,
  ): string | Array<string | React.Element<*>> {
    const elements = [];
    const props = this.props;
    let matchedString = string;
    let parts = {};
 
    this.matchers.forEach((matcher: Matcher<*>) => {
      const tagName = matcher.asTag().toLowerCase();
      const config = this.getTagConfig(tagName);
 
      // Skip matchers that have been disabled from props or are not supported
      if (
        props[matcher.inverseName] ||
        TAGS_BLACKLIST[tagName] ||
        (!props.disableWhitelist && !TAGS[tagName])
      ) {
        return;
      }
 
      // Skip matchers in which the child cannot be rendered
      if (!this.canRenderChild(parentConfig, config)) {
        return;
      }
 
      // Continuously trigger the matcher until no matches are found
      while (parts = matcher.match(matchedString)) {
        const { match, ...partProps } = parts;
 
        // Replace the matched portion with a placeholder
        matchedString = matchedString.replace(match, `#{{${elements.length}}}#`);
 
        // Create an element through the matchers factory
        this.keyIndex += 1;
 
        elements.push(matcher.createElement(match, {
          ...props,
          ...(partProps || {}),
          key: this.keyIndex,
        }));
      }
    });
 
    if (!elements.length) {
      return matchedString;
    }
 
    // Deconstruct the string into an array so that React can render it
    const matchedArray = [];
    let lastIndex = 0;
 
    while (parts = matchedString.match(/#\{\{(\d+)\}\}#/)) {
      const no = parts[1];
      // $FlowIssue https://github.com/facebook/flow/issues/2450
      const index = parts.index;
 
      // Extract the previous string
      if (lastIndex !== index) {
        matchedArray.push(matchedString.substring(lastIndex, index));
      }
 
      // Inject the element
      matchedArray.push(elements[parseInt(no, 10)]);
 
      // Set the next index
      lastIndex = index + parts[0].length;
 
      // Replace the token so it won't be matched again
      // And so that the string length doesn't change
      matchedString = matchedString.replace(`#{{${no}}}#`, `%{{${no}}}%`);
    }
 
    // Extra the remaining string
    if (lastIndex < matchedString.length) {
      matchedArray.push(matchedString.substring(lastIndex));
    }
 
    return matchedArray;
  }
 
  /**
   * Determine whether the child can be rendered within the parent.
   */
  canRenderChild(parentConfig: NodeConfig, childConfig: NodeConfig): boolean {
    if (!parentConfig.tagName || !childConfig.tagName) {
      return false;
    }
 
    // Valid children
    if (
      parentConfig.children &&
      parentConfig.children.length &&
      parentConfig.children.indexOf(childConfig.tagName) === -1
    ) {
      return false;
    }
 
    // Valid parent
    if (
      childConfig.parent &&
      childConfig.parent.length &&
      childConfig.parent.indexOf(parentConfig.tagName) === -1
    ) {
      return false;
    }
 
    // Self nesting
    if (!parentConfig.self && parentConfig.tagName === childConfig.tagName) {
      return false;
    }
 
    // Block
    if (!parentConfig.block && childConfig.type === TYPE_BLOCK) {
      return false;
    }
 
    // Inline
    if (!parentConfig.inline && childConfig.type === TYPE_INLINE) {
      return false;
    }
 
    return true;
  }
 
  /**
   * Convert line breaks in a string to HTML `<br/>` tags.
   * If the string contains HTML, we should not convert anything,
   * as line breaks should be handled by `<br/>`s in the markup itself.
   */
  convertLineBreaks(markup: string): string {
    const { noHtml, disableLineBreaks } = this.props;
 
    if (noHtml || disableLineBreaks || markup.match(/<((?:\/[a-z ]+)|(?:[a-z ]+\/))>/ig)) {
      return markup;
    }
 
    // Replace carriage returns
    markup = markup.replace(/\r\n/g, '\n');
 
    // Replace long line feeds
    markup = markup.replace(/\n{3,}/g, '\n\n\n');
 
    // Replace line feeds with `<br/>`s
    markup = markup.replace(/\n/g, '<br/>');
 
    return markup;
  }
 
  /**
   * Create a detached HTML document that allows for easy HTML
   * parsing while not triggering scripts or loading external
   * resources.
   */
  createDocument(markup: string): Document {
    const doc = document.implementation.createHTMLDocument('Interweave');
 
    if (INVALID_ROOTS.indexOf(markup.substr(1, 4).toUpperCase()) >= 0) {
      throw new Error('HTML documents as Interweave content are not supported.');
 
    } else {
      // $FlowIssue Isn't null
      doc.body.innerHTML = this.convertLineBreaks(markup);
    }
 
    return doc;
  }
 
  /**
   * Convert an elements attribute map to an object map.
   * Returns null if no attributes are defined.
   */
  extractAttributes(node: NodeInterface): ?Attributes {
    const { disableWhitelist } = this.props;
    const attributes = {};
    let count = 0;
 
    if (node.nodeType !== ELEMENT_NODE || !node.attributes) {
      return null;
    }
 
    Array.from(node.attributes).forEach((attr: { name: string, value: string }) => {
      const name: string = attr.name.toLowerCase();
      const value: string = attr.value;
      const filter: number = ATTRIBUTES[name];
 
      // Verify the node is safe from attacks
      if (!this.isSafe(node)) {
        return;
      }
 
      // Do not allow blacklisted attributes excluding ARIA attributes
      // Do not allow events or XSS injections
      if (name.substr(0, 5) !== 'aria-') {
        if (
          (!disableWhitelist && (!filter || filter === FILTER_DENY)) ||
          name.match(/^on/) ||
          value.replace(/(\s|\0|&#x0(9|A|D);)/, '').match(/(javascript|vbscript|livescript|xss):/i)
        ) {
          return;
        }
      }
 
      // Apply filters
      let newValue: PrimitiveType = this.applyFilters(name, value);
 
      // Cast to boolean
      if (filter === FILTER_CAST_BOOL) {
        newValue = (newValue === 'true' || newValue === name);
 
      // Cast to number
      } else if (filter === FILTER_CAST_NUMBER) {
        newValue = parseFloat(newValue);
 
      // Cast to string
      } else {
        newValue = String(newValue);
      }
 
      attributes[ATTRIBUTES_TO_PROPS[name] || name] = newValue;
      count += 1;
    });
 
    if (count === 0) {
      return null;
    }
 
    return attributes;
  }
 
  /**
   * Return configuration for a specific tag.
   * If no tag config exists, return a plain object.
   */
  getTagConfig(tagName: string): NodeConfig {
    if (TAGS[tagName]) {
      return {
        ...TAGS[tagName],
        tagName,
      };
    }
 
    return {};
  }
 
  /**
   * Verify that a node is safe from XSS and injection attacks.
   */
  isSafe(node: NodeInterface): boolean {
    Iif (!(node instanceof HTMLElement)) {
      return true;
    }
 
    // URLs should only support HTTP and email
    if ('href' in node) {
      const href = node.getAttribute('href');
 
      // Fragment protocols start with about:
      // So let's just allow them
      if (href && href.charAt(0) === '#') {
        return true;
      }
 
      // $FlowIssue Protocol only exists for anchors
      const protocol = (node.protocol || '').toLowerCase();
 
      return (
        protocol === ':' ||
        protocol === 'http:' ||
        protocol === 'https:' ||
        protocol === 'mailto:'
      );
    }
 
    return true;
  }
 
  /**
   * Parse the markup by injecting it into a detached document,
   * while looping over all child nodes and generating an
   * array to interpolate into JSX.
   */
  parse(): ParsedNodes {
    // $FlowIssue Body is not null!
    return this.parseNode(this.doc.body, {
      ...CONFIG_BLOCK,
      tagName: 'body',
    });
  }
 
  /**
   * Loop over the nodes children and generate a
   * list of text nodes and React elements.
   */
  parseNode(parentNode: NodeInterface, parentConfig: NodeConfig): ParsedNodes {
    const { noHtml, disableWhitelist } = this.props;
    let content = [];
    let mergedText = '';
 
    Array.from(parentNode.childNodes).forEach((node: NodeInterface) => {
      // Create React elements from HTML elements
      if (node.nodeType === ELEMENT_NODE) {
        const tagName = node.nodeName.toLowerCase();
        const config = this.getTagConfig(tagName);
 
        // Never allow these tags
        if (TAGS_BLACKLIST[tagName]) {
          return;
        }
 
        // Persist any previous text
        if (mergedText) {
          content.push(mergedText);
          mergedText = '';
        }
 
        // Only render when the following criteria is met:
        //  - HTML has not been disabled
        //  - Whitelist is disabled OR the child is valid within the parent
        if (!noHtml && (disableWhitelist || this.canRenderChild(parentConfig, config))) {
          this.keyIndex += 1;
 
          // Build the props as it makes it easier to test
          const attributes = this.extractAttributes(node);
          const elementProps: ElementProps = {
            key: this.keyIndex,
            tagName,
          };
 
          if (attributes) {
            elementProps.attributes = attributes;
          }
 
          if (config.void) {
            elementProps.selfClose = config.void;
          }
 
          content.push((
            <ElementComponent {...elementProps}>
              {this.parseNode(node, config)}
            </ElementComponent>
          ));
 
        // Render the children of the current element only.
        // Important: If the current element is not whitelisted,
        // use the parent element for the next scope.
        } else {
          content = content.concat(this.parseNode(node, config.tagName ? config : parentConfig));
        }
 
      // Apply matchers if a text node
      } else if (node.nodeType === TEXT_NODE) {
        const text = noHtml
          ? node.textContent
          : this.applyMatchers(node.textContent, parentConfig);
 
        if (Array.isArray(text)) {
          content = content.concat(text);
        } else {
          mergedText += text;
        }
      }
    });
 
    if (mergedText) {
      content.push(mergedText);
    }
 
    return content;
  }
}