all files / src/ tokens.js

85.86% Statements 85/99
84.62% Branches 22/26
90% Functions 18/20
81.43% Lines 57/70
1 statement, 1 function, 1 branch 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                                                                                           30× 30× 30× 30×       19×                                                                     49× 49× 49× 24× 24×   24× 24×   23×         49×     49×   46×     49×     49×                                     79× 79× 79× 79× 79× 79× 79× 79× 79× 79×       40×                                   10× 10× 10× 10× 10× 10× 10× 10× 10× 10×                                 41× 41× 41× 41× 41×       22×        
import {escapeQuotes} from './utils';
 
/**
 * Token is a base class for all token types parsed.  Note we don't actually
 * use intheritance due to IE8's non-existent ES5 support.
 */
export class Token {
  /**
   * Constructor.
   *
   * @param {string} type The type of the Token.
   * @param {Number} length The length of the Token text.
   */
  constructor(type, length) {
    this.type = type;
    this.length = length;
    this.text = '';
  }
}
 
/**
 * CommentToken represents comment tags.
 */
export class CommentToken {
  /**
   * Constructor.
   *
   * @param {string} content The content of the comment
   * @param {Number} length The length of the Token text.
   */
  constructor(content, length) {
    this.type = 'comment';
    this.length = length || (content ? content.length : 0);
    this.text = '';
    this.content = content;
  }
 
  toString() {
    return `<!--${this.content}`;
  }
}
 
/**
 * CharsToken represents non-tag characters.
 */
export class CharsToken {
  /**
   * Constructor.
   *
   * @param {Number} length The length of the Token text.
   */
  constructor(length) {
    this.type = 'chars';
    this.length = length;
    this.text = '';
  }
 
  toString() {
    return this.text;
  }
}
 
/**
 * TagToken is a base class for all tag-based Tokens.
 */
export class TagToken {
  /**
   * Constructor.
   *
   * @param {string} type The type of the token.
   * @param {string} tagName The tag name.
   * @param {Number} length The length of the Token text.
   * @param {Object} attrs The dictionary of attributes and values
   * @param {Object} booleanAttrs If an entry has 'true' then the attribute
   *                              is a boolean attribute
   */
  constructor(type, tagName, length, attrs, booleanAttrs) {
    this.type = type;
    this.length = length;
    this.text = '';
    this.tagName = tagName;
    this.attrs = attrs;
    this.booleanAttrs = booleanAttrs;
    this.unary = false;
    this.html5Unary = false;
  }
 
  /**
   * Formats the given token tag.
   *
   * @param {TagToken} tok The TagToken to format.
   * @param {?string} [content=null] The content of the token.
   * @returns {string} The formatted tag.
   */
  static formatTag(tok, content = null) {
    let str = `<${tok.tagName}`;
    for (let key in tok.attrs) {
      Eif (tok.attrs.hasOwnProperty(key)) {
        str += ` ${key}`;
 
        const val = tok.attrs[key];
        if (typeof tok.booleanAttrs === 'undefined' ||
            typeof tok.booleanAttrs[key] === 'undefined') {
          str += `="${escapeQuotes(val)}"`;
        }
      }
    }
 
    if (tok.rest) {
      str += ` ${tok.rest}`;
    }
 
    if (tok.unary && !tok.html5Unary) {
      str += '/>';
    } else {
      str += '>';
    }
 
    if (content !== undefined && content !== null) {
      str += `${content}</${tok.tagName}>`;
    }
 
    return str;
  }
}
 
/**
 * StartTagToken represents a start token.
 */
export class StartTagToken {
  /**
   * Constructor.
   *
   * @param {string} tagName The tag name.
   * @param {Number} length The length of the Token text
   * @param {Object} attrs The dictionary of attributes and values
   * @param {Object} booleanAttrs If an entry has 'true' then the attribute
   *                              is a boolean attribute
   * @param {boolean} unary True if the tag is a unary tag
   * @param {string} rest The rest of the content.
   */
  constructor(tagName, length, attrs, booleanAttrs, unary, rest) {
    this.type = 'startTag';
    this.length = length;
    this.text = '';
    this.tagName = tagName;
    this.attrs = attrs;
    this.booleanAttrs = booleanAttrs;
    this.html5Unary = false;
    this.unary = unary;
    this.rest = rest;
  }
 
  toString() {
    return TagToken.formatTag(this);
  }
}
 
/**
 * AtomicTagToken represents an atomic tag.
 */
export class AtomicTagToken {
  /**
   * Constructor.
   *
   * @param {string} tagName The name of the tag.
   * @param {Number} length The length of the tag text.
   * @param {Object} attrs The attributes.
   * @param {Object} booleanAttrs If an entry has 'true' then the attribute
   *                              is a boolean attribute
   * @param {string} content The content of the tag.
   */
  constructor(tagName, length, attrs, booleanAttrs, content) {
    this.type = 'atomicTag';
    this.length = length;
    this.text = '';
    this.tagName = tagName;
    this.attrs = attrs;
    this.booleanAttrs = booleanAttrs;
    this.unary = false;
    this.html5Unary = false;
    this.content = content;
  }
 
  toString() {
    return TagToken.formatTag(this, this.content);
  }
}
 
/**
 * EndTagToken represents an end tag.
 */
export class EndTagToken {
  /**
   * Constructor.
   *
   * @param {string} tagName The name of the tag.
   * @param {Number} length The length of the tag text.
   */
  constructor(tagName, length) {
    this.type = 'endTag';
    this.length = length;
    this.text = '';
    this.tagName = tagName;
  }
 
  toString() {
    return `</${this.tagName}>`;
  }
}