Sha256: e94da017c988f5e3519b3fb99aa73ece63766b68cffa477394a564dc3af4a1fa

Contents?: true

Size: 1.66 KB

Versions: 80

Compression:

Stored size: 1.66 KB

Contents

/**
 * Expose `parse`.
 */

module.exports = parse;

/**
 * Wrap map from jquery.
 */

var map = {
  option: [1, '<select multiple="multiple">', '</select>'],
  optgroup: [1, '<select multiple="multiple">', '</select>'],
  legend: [1, '<fieldset>', '</fieldset>'],
  thead: [1, '<table>', '</table>'],
  tbody: [1, '<table>', '</table>'],
  tfoot: [1, '<table>', '</table>'],
  colgroup: [1, '<table>', '</table>'],
  caption: [1, '<table>', '</table>'],
  tr: [2, '<table><tbody>', '</tbody></table>'],
  td: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
  th: [3, '<table><tbody><tr>', '</tr></tbody></table>'],
  col: [2, '<table><tbody></tbody><colgroup>', '</colgroup></table>'],
  _default: [0, '', '']
};

/**
 * Parse `html` and return the children.
 *
 * @param {String} html
 * @return {Array}
 * @api private
 */

function parse(html) {
  if ('string' != typeof html) throw new TypeError('String expected');

  // tag name
  var m = /<([\w:]+)/.exec(html);
  if (!m) throw new Error('No elements were generated.');
  var tag = m[1];

  // body support
  if (tag == 'body') {
    var el = document.createElement('html');
    el.innerHTML = html;
    return el.removeChild(el.lastChild);
  }

  // wrap map
  var wrap = map[tag] || map._default;
  var depth = wrap[0];
  var prefix = wrap[1];
  var suffix = wrap[2];
  var el = document.createElement('div');
  el.innerHTML = prefix + html + suffix;
  while (depth--) el = el.lastChild;

  var els = el.children;
  if (1 == els.length) {
    return el.removeChild(els[0]);
  }

  var fragment = document.createDocumentFragment();
  while (els.length) {
    fragment.appendChild(el.removeChild(els[0]));
  }

  return fragment;
}

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
ende-0.5.22 components/component/domify/1.0.0/index.js
ende-0.5.21 components/component/domify/1.0.0/index.js
ende-0.5.20 components/component/domify/1.0.0/index.js
ende-0.5.19 components/component/domify/1.0.0/index.js
ende-0.5.18 components/component/domify/1.0.0/index.js
ende-0.5.17 components/component/domify/1.0.0/index.js
ende-0.5.16 components/component/domify/1.0.0/index.js
ende-0.5.15 components/component/domify/1.0.0/index.js
ende-0.5.14 components/component/domify/1.0.0/index.js
ende-0.5.13 components/component/domify/1.0.0/index.js
ende-0.5.12 components/component/domify/1.0.0/index.js
ende-0.5.10 components/component/domify/1.0.0/index.js
ende-0.5.9 components/component/domify/1.0.0/index.js
ende-0.5.8 components/component/domify/1.0.0/index.js
ende-0.5.7 components/component/domify/1.0.0/index.js
ende-0.5.6 components/component/domify/1.0.0/index.js
ende-0.4.25 vendor/components/component-domify/index.js
ende-0.5.5 components/component/domify/1.0.0/index.js
ende-0.5.4 components/component/domify/1.0.0/index.js
ende-0.5.3 components/component/domify/1.0.0/index.js