Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
refactor(write-stream): refactor to remove redundant code
Browse files Browse the repository at this point in the history
Both _insertScript and _insertStyle shared the majority of code

Fixes #183
  • Loading branch information
Seth Yates committed Apr 28, 2016
1 parent 55d39df commit bb33c6b
Showing 1 changed file with 7 additions and 32 deletions.
39 changes: 7 additions & 32 deletions src/write-stream.js
Expand Up @@ -386,7 +386,7 @@ export default class WriteStream {
_writeStyleToken(tok) {
const el = this._buildStyle(tok);

this._insertStyle(el);
this._insertCursor(el, PROXY_STYLE);

// Set content
if (tok.content) {
Expand Down Expand Up @@ -419,41 +419,16 @@ export default class WriteStream {

/**
* 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.
* (i.e. insertion point) for the element.
*
* @param {Object} el The element
* @param {string} which The type of proxy element
*/
_insertStyle(el) {
const cursor = this._insertSpan(PROXY_STYLE);

// Replace cursor with style.
if (cursor) {
cursor.parentNode.replaceChild(el, cursor);
}
}
_insertCursor(el, which) {
this._writeImpl(`<span id="${which}"/>`);

/**
* Insert script into DOM where it would naturally be written.
*
* @param {Object} el The element
*/
_insertScript(el) {
const cursor = this._insertSpan(PROXY_SCRIPT);
const cursor = this.doc.getElementById(which);

// Replace cursor with script.
if (cursor) {
cursor.parentNode.replaceChild(el, cursor);
}
Expand Down Expand Up @@ -522,7 +497,7 @@ export default class WriteStream {
}

try {
this._insertScript(el);
this._insertCursor(el, PROXY_SCRIPT);
if (!el.src || asyncRelease) {
done();
}
Expand Down

0 comments on commit bb33c6b

Please sign in to comment.