all files / src/ write-stream.js

95.74% Statements 225/235
87.97% Branches 117/133
97.3% Functions 36/37
94.62% Lines 176/186
8 statements, 8 branches Ignored     
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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610                                                         1519×   1519×     1519×                   318× 318×   318× 84×   234×                                                                   84× 84× 84× 84× 84× 84×     84×       84×     84×   84× 84×   84×               477× 477×         477× 406×   406× 84×   322×                       84× 84× 84× 84×                   454×   454× 454× 454× 454×     454×     548×   548× 548×       454× 378×     454× 132×     454×                       378×   378×         378× 378× 378×   378×       378×   378×       378×                     378×     378×     378×     378×   378× 378× 548× 548×   548×   548×   234× 234×     234×     234×   102×                   314×     314×       378×                           378× 378×       378× 1051× 1051×     1051× 481×   234× 234×       481×   481×   395×         1051×                 132× 132×   132×   59×     132×   132× 132×       130×         122×       130× 130×                                                                                                                     132×     132×                                       130×     130× 130×                     214× 214× 214×                     214×         214×     214×             214×                       130× 130× 130×   130×   52× 52× 51× 51×       130× 130× 130× 79×                             130×     130× 104×       130× 78×     130×                     52×     52×   50× 50×           52× 50×                                     130× 130×          
import HtmlParser from 'prescribe';
import * as utils from './utils';
 
/**
 * Turn on to debug how each chunk affected the DOM.
 * @type {boolean}
 */
const DEBUG_CHUNK = false;
 
/**
 * Prefix for data attributes on DOM elements.
 * @type {string}
 */
const BASEATTR = 'data-ps-';
 
/**
 * ID for the style proxy
 * @type {string}
 */
const PROXY_STYLE = 'ps-style';
 
/**
 * ID for the script proxy
 * @type {string}
 */
const PROXY_SCRIPT = 'ps-script';
 
/**
 * Get data attributes
 *
 * @param {Object} el The DOM element.
 * @param {String} name The attribute name.
 * @returns {String}
 */
function getData(el, name) {
  const attr = BASEATTR + name;
 
  const val = el.getAttribute(attr);
 
  // IE 8 returns a number if it's a number
  return !utils.existy(val) ? val : String(val);
}
 
/**
 * Set data attributes
 *
 * @param {Object} el The DOM element.
 * @param {String} name The attribute name.
 * @param {null|*} value The attribute value.
 */
function setData(el, name, value = null) {
  const attr = BASEATTR + name;
 
  if (utils.existy(value) && value !== '') {
    el.setAttribute(attr, value);
  } else {
    el.removeAttribute(attr);
  }
}
 
/**
 * Stream static html to an element, where "static html" denotes "html
 * without scripts".
 *
 * This class maintains a *history of writes devoid of any attributes* or
 * "proxy history".
 *
 * Injecting the proxy history into a temporary div has no side-effects,
 * other than to create proxy elements for previously written elements.
 *
 * Given the `staticHtml` of a new write, a `tempDiv`'s innerHTML is set to
 * `proxy_history + staticHtml`.
 * The *structure* of `tempDiv`'s contents, (i.e., the placement of new nodes
 * beside or inside of proxy elements), reflects the DOM structure that would
 * have resulted if all writes had been squashed into a single write.
 *
 * For each descendent `node` of `tempDiv` whose parentNode is a *proxy*,
 * `node` is appended to the corresponding *real* element within the DOM.
 *
 * Proxy elements are mapped to *actual* elements in the DOM by injecting a
 * `data-id` attribute into each start tag in `staticHtml`.
 *
 */
export default class WriteStream {
  /**
   * Constructor.
   *
   * @param {Object} root The root element
   * @param {?Object} options The options
   */
  constructor(root, options = {}) {
    this.root = root;
    this.options = options;
    this.doc = root.ownerDocument;
    this.win = this.doc.defaultView || this.doc.parentWindow;
    this.parser = new HtmlParser('', {autoFix: options.autoFix});
 
    // Actual elements by id.
    this.actuals = [root];
 
    // Embodies the "structure" of what's been written so far,
    // devoid of attributes.
    this.proxyHistory = '';
 
    // Create a proxy of the root element.
    this.proxyRoot = this.doc.createElement(root.nodeName);
 
    this.scriptStack = [];
    this.writeQueue = [];
 
    setData(this.proxyRoot, 'proxyof', 0);
  }
 
  /**
   * Writes the given strings.
   *
   * @param {...String} str The strings to write
   */
  write(...str) {
    this.writeQueue.push(...str);
 
    // Process writes
    // When new script gets pushed or pending this will stop
    // because new writeQueue gets pushed
    while (!this.deferredRemote && this.writeQueue.length) {
      const arg = this.writeQueue.shift();
 
      if (utils.isFunction(arg)) {
        this._callFunction(arg);
      } else {
        this._writeImpl(arg);
      }
    }
  }
 
  /**
   * Calls the given function.
   *
   * @param {Function} fn The function to call
   * @private
   */
  _callFunction(fn) {
    const tok = {type: 'function', value: fn.name || fn.toString()};
    this._onScriptStart(tok);
    fn.call(this.win, this.doc);
    this._onScriptDone(tok);
  }
 
  /**
   * The write implementation
   *
   * @param {String} html The HTML to write.
   * @private
   */
  _writeImpl(html) {
    this.parser.append(html);
 
    let tok;
    let script;
    let style;
    const tokens = [];
 
    // stop if we see a script token
    while ((tok = this.parser.readToken()) &&
      !(script = utils.isScript(tok)) &&
      !(style = utils.isStyle(tok))) {
      tok = this.options.beforeWriteToken(tok);
 
      Eif (tok) {
        tokens.push(tok);
      }
    }
 
    if (tokens.length > 0) {
      this._writeStaticTokens(tokens);
    }
 
    if (script) {
      this._handleScriptToken(tok);
    }
 
    if (style) {
      this._handleStyleToken(tok);
    }
  }
 
  /**
   * Write a contiguous non-script tokens (a chunk)
   *
   * @param {Array<Object>} tokens The tokens
   * @returns {{tokens, raw, actual, proxy}|null}
   * @private
   */
  _writeStaticTokens(tokens) {
    const chunk = this._buildChunk(tokens);
 
    Iif (!chunk.actual) {
      // e.g., no tokens, or a noscript that got ignored
      return null;
    }
 
    chunk.html = this.proxyHistory + chunk.actual;
    this.proxyHistory += chunk.proxy;
    this.proxyRoot.innerHTML = chunk.html;
 
    Iif (DEBUG_CHUNK) {
      chunk.proxyInnerHTML = this.proxyRoot.innerHTML;
    }
 
    this._walkChunk();
 
    Iif (DEBUG_CHUNK) {
      chunk.actualInnerHTML = this.root.innerHTML;
    }
 
    return chunk;
  }
 
  /**
   * Build a chunk.
   *
   * @param {Array<Object>} tokens The tokens to use.
   * @returns {{tokens: *, raw: string, actual: string, proxy: string}}
   * @private
   */
  _buildChunk(tokens) {
    let nextId = this.actuals.length;
 
    // The raw html of this chunk.
    const raw = [];
 
    // The html to create the nodes in the tokens (with id's injected).
    const actual = [];
 
    // Html that can later be used to proxy the nodes in the tokens.
    const proxy = [];
 
    const len = tokens.length;
    for (let i = 0; i < len; i++) {
      const tok = tokens[i];
      const tokenRaw = tok.toString();
 
      raw.push(tokenRaw);
 
      if (tok.attrs) { // tok.attrs <==> startTag or atomicTag or cursor
        // Ignore noscript tags. They are atomic, so we don't have to worry about children.
        Eif (!(/^noscript$/i).test(tok.tagName)) {
          const id = nextId++;
 
          // Actual: inject id attribute: replace '>' at end of start tag with id attribute + '>'
          actual.push(tokenRaw.replace(/(\/?>)/, ` ${BASEATTR}id=${id} $1`));
 
          // Don't proxy scripts: they have no bearing on DOM structure.
          if (tok.attrs.id !== PROXY_SCRIPT && tok.attrs.id !== PROXY_STYLE) {
            // Proxy: strip all attributes and inject proxyof attribute
            proxy.push(
              // ignore atomic tags (e.g., style): they have no "structural" effect
              tok.type === 'atomicTag' ? '' :
              `<${tok.tagName} ${BASEATTR}proxyof=${id}` + (tok.unary ? ' />' : '>')
            );
          }
        }
      } else {
        // Visit any other type of token
        // Actual: append.
        actual.push(tokenRaw);
 
        // Proxy: append endTags. Ignore everything else.
        proxy.push(tok.type === 'endTag' ? tokenRaw : '');
      }
    }
 
    return {
      tokens,
      raw: raw.join(''),
      actual: actual.join(''),
      proxy: proxy.join('')
    };
  }
 
  /**
   * Walk the chunks.
   *
   * @private
   */
  _walkChunk() {
    let node;
    const stack = [this.proxyRoot];
 
    // use shift/unshift so that children are walked in document order
 
    while (utils.existy(node = stack.shift())) {
      const isElement = node.nodeType === 1;
      const isProxy = isElement && getData(node, 'proxyof');
 
      // Ignore proxies
      if (!isProxy) {
        if (isElement) {
          // New actual element: register it and remove the the id attr.
          this.actuals[getData(node, 'id')] = node;
          setData(node, 'id');
        }
 
        // Is node's parent a proxy?
        const parentIsProxyOf = node.parentNode &&
          getData(node.parentNode, 'proxyof');
        if (parentIsProxyOf) {
          // Move node under actual parent.
          this.actuals[parentIsProxyOf].appendChild(node);
        }
      }
 
      // prepend childNodes to stack
      stack.unshift.apply(stack, utils.toArray(node.childNodes));
    }
  }
 
  /**
   * Handles Script tokens
   *
   * @param {Object} tok The token
   */
  _handleScriptToken(tok) {
    const remainder = this.parser.clear();
 
    if (remainder) {
      // Write remainder immediately behind this script.
      this.writeQueue.unshift(remainder);
    }
 
    tok.src = tok.attrs.src || tok.attrs.SRC;
 
    tok = this.options.beforeWriteToken(tok);
    if (!tok) {
      // User has removed this token
      return;
    }
 
    if (tok.src && this.scriptStack.length) {
      // Defer this script until scriptStack is empty.
      // Assumption 1: This script will not start executing until
      // scriptStack is empty.
      this.deferredRemote = tok;
    } else {
      this._onScriptStart(tok);
    }
 
    // Put the script node in the DOM.
    this._writeScriptToken(tok, () => {
      this._onScriptDone(tok);
    });
  }
 
  /**
   * Handles style tokens
   *
   * @param {Object} tok The token
   */
  _handleStyleToken(tok) {
    const remainder = this.parser.clear();
 
    if (remainder) {
      // Write remainder immediately behind this style.
      this.writeQueue.unshift(remainder);
    }
 
    tok.type = tok.attrs.type || tok.attrs.TYPE || 'text/css';
 
    tok = this.options.beforeWriteToken(tok);
 
    if (tok) {
      // Put the style node in the DOM.
      this._writeStyleToken(tok);
    }
 
    if (remainder) {
      this.write();
    }
  }
 
  /**
   * Build a style and insert it into the DOM.
   *
   * @param {Object} tok The token
   */
  _writeStyleToken(tok) {
    const el = this._buildStyle(tok);
 
    this._insertStyle(el);
 
    // Set content
    Eif (tok.content) {
      Iif (el.styleSheet && !el.sheet) {
        el.styleSheet.cssText = tok.content;
      } else {
        el.appendChild(this.doc.createTextNode(tok.content));
      }
    }
  }
 
  /**
   * Build a style element from an atomic style token.
   *
   * @param {Object} tok The token
   * @returns {Element}
   */
  _buildStyle(tok) {
    const el = this.doc.createElement(tok.tagName);
 
    el.setAttribute('type', tok.type);
 
    // Set attributes
    utils.eachKey(tok.attrs, (name, value) => {
      el.setAttribute(name, value);
    });
 
    return el;
  }
 
  /**
   * Append a span to the stream. That span will act as a cursor
   * (i.e. insertion point) for the style.
   *
   * @param {String} id The ID of the span.
   * @private
   */
  _insertSpan(id) {
    this._writeImpl(`<span id="${id}"/>`);
 
    // Grab that span from the DOM.
    return this.doc.getElementById(id);
  }
 
  /**
   * Insert style into DOM where it would naturally be written.
   *
   * @param {Object} el The element
   */
  _insertStyle(el) {
    const cursor = this._insertSpan(PROXY_STYLE);
 
    // Replace cursor with style.
    Eif (cursor) {
      cursor.parentNode.replaceChild(el, cursor);
    }
  }
 
  /**
   * Insert script into DOM where it would naturally be written.
   *
   * @param {Object} el The element
   */
  _insertScript(el) {
    const cursor = this._insertSpan(PROXY_SCRIPT);
 
    // Replace cursor with script.
    Eif (cursor) {
      cursor.parentNode.replaceChild(el, cursor);
    }
  }
 
  /**
   * Called when a script is started.
   *
   * @param {Object} tok The token
   * @private
   */
  _onScriptStart(tok) {
    tok.outerWrites = this.writeQueue;
    this.writeQueue = [];
    this.scriptStack.unshift(tok);
  }
 
  /**
   * Called when a script is done.
   *
   * @param {Object} tok The token
   * @private
   */
  _onScriptDone(tok) {
    // Pop script and check nesting.
    Iif (tok !== this.scriptStack[0]) {
      this.options.error({message: 'Bad script nesting or script finished twice'});
      return;
    }
 
    this.scriptStack.shift();
 
    // Append outer writes to queue and process them.
    this.write.apply(this, tok.outerWrites);
 
    // Check for pending remote
 
    // Assumption 2: if remote_script1 writes remote_script2 then
    // the we notice remote_script1 finishes before remote_script2 starts.
    // I think this is equivalent to assumption 1
    if (!this.scriptStack.length && this.deferredRemote) {
      this._onScriptStart(this.deferredRemote);
      this.deferredRemote = null;
    }
  }
 
  /**
   * Build a script and insert it into the DOM.
   * Done is called once script has executed.
   *
   * @param {Object} tok The token
   * @param {Function} done The callback when complete
   */
  _writeScriptToken(tok, done) {
    const el = this._buildScript(tok);
    const asyncRelease = this._shouldRelease(el);
    const afterAsync = this.options.afterAsync;
 
    if (tok.src) {
      // Fix for attribute "SRC" (capitalized). IE does not recognize it.
      el.src = tok.src;
      this._scriptLoadHandler(el, !asyncRelease ? () => {
        done();
        afterAsync();
      } : afterAsync);
    }
 
    try {
      this._insertScript(el);
      if (!el.src || asyncRelease) {
        done();
      }
    } catch (e) {
      this.options.error(e);
      done();
    }
  }
 
  /**
   * Build a script element from an atomic script token.
   *
   * @param {Object} tok The token
   * @returns {Element}
   */
  _buildScript(tok) {
    const el = this.doc.createElement(tok.tagName);
 
    // Set attributes
    utils.eachKey(tok.attrs, (name, value) => {
      el.setAttribute(name, value);
    });
 
    // Set content
    if (tok.content) {
      el.text = tok.content;
    }
 
    return el;
  }
 
  /**
   * Setup the script load handler on an element.
   *
   * @param {Object} el The element
   * @param {Function} done The callback
   * @private
   */
  _scriptLoadHandler(el, done) {
    function cleanup() {
      el = el.onload = el.onreadystatechange = el.onerror = null;
    }
 
    const error = this.options.error;
 
    function success() {
      cleanup();
      done();
    }
 
    function failure(err) {
      cleanup();
      error(err);
      done();
    }
 
    // Set handlers
    Object.assign(el, {
      onload: () => success(),
 
      onreadystatechange: () => {
        if (/^(loaded|complete)$/.test(el.readyState)) {
          success();
        }
      },
 
      onerror: () => failure({message: `remote script failed ${el.src}`})
    });
  }
 
  /**
   * Determines whether to release.
   *
   * @param {Object} el The element
   * @returns {boolean}
   * @private
   */
  _shouldRelease(el) {
    const isScript = (/^script$/i).test(el.nodeName);
    return !isScript || !!(this.options.releaseAsync && el.src && el.hasAttribute('async'));
  }
 
}