Ejemplo n.º 1
0
/**
 * Parse a polymer component and return a parse result object
 *
 * @param {String} src
 *        Web-component html source
 *
 * @param {String} filepath
 *        Web-component filepath
 *
 * @return {Object}
 *         Parse result object:
 *         - `imports` {Array}: Relative path to other components to require as commonjs module
 *         - `scripts` {Array}: Relative path to other javascript modules
 *         - `inline` {Array}: Inline script sources
 *         - `head` {String}: Source to insert into the main document's head
 *         - `body` {String}: Source to insert into the document's body
 */
function parseSource(src, filepath) {
  var result  = {};

  // Use whacko (cheerio) to parse html source
  var $       = whacko.load(src);

  // Extract sources and remove tags
  result.imports = extractImports($);
  result.scripts = extractScripts($);
  result.inline  = extractInline($)

  // Inline external stylesheets
  inlineStylesheet($, filepath)

  // Inline css minification and remove comments
  minifyHtml($)

  // Extract transformed html source:
  result.head = $("head").html().trim();
  result.body = $("body").html().trim();

  return result;
}
Ejemplo n.º 2
0
function handleMainDocument() {
  // reset shared buffers
  read = {};
  var content = options.inputSrc ? options.inputSrc.toString() : readFile(options.input);
  var $ = whacko.load(content);
  var dir = path.dirname(options.input);
  pathresolver.resolvePaths($, dir, options.outputDir, options.abspath);
  processImports($, true);
  if (options.inline) {
    inlineSheets($, dir, options.outputDir);
  }

  if (options.inline) {
    inlineScripts($, options.outputDir);
  }

  searchAll($, constants.COFFEE_INLINE).each(function() {
    var el = $(this);
    var content = getTextContent(el);
    setTextContent(el, CoffeeScript.compile(content , {bare: true}));
    setAttribute(el, "type", "text/javascript");
  });

  searchAll($, constants.JS_INLINE).each(function() {
    var el = $(this);
    var content = getTextContent(el);
    // find ancestor polymer-element node
    var parentElement = el.closest('polymer-element').get(0);
    if (parentElement) {
      var match = constants.POLYMER_INVOCATION.exec(content);
      var elementName = $(parentElement).attr('name');
      if (match) {
        var invocation = utils.processPolymerInvocation(elementName, match);
        content = content.replace(match[0], invocation);
        setTextContent(el, content);
      }
    }
  });


  // strip noscript from elements, and instead inject explicit Polymer() invocation
  // script, so registration order is preserved
  searchAll($, constants.ELEMENTS_NOSCRIPT).each(function() {
    var el = $(this);
    var name = el.attr('name');
    if (options.verbose) {
      console.log('Injecting explicit Polymer invocation for noscript element "' + name + '"');
    }
    el.append('<script>Polymer(\'' + name + '\');</script>');
    el.attr('noscript', null);
  });

  // strip scripts into a separate file
  if (options.csp) {
    if (options.verbose) {
      console.log('Separating scripts into separate file');
    }

    // CSPify main page by removing inline scripts
    var scripts = [];
    searchAll($, constants.JS_INLINE).each(function() {
      var el = $(this);
      var content = getTextContent(el);
      scripts.push(content);
      el.remove();
    });

    // join scripts with ';' to prevent breakages due to EOF semicolon insertion
    var scriptName = path.basename(options.output, '.html') + '.js';
    var scriptContent = scripts.join(';' + constants.EOL);
    if (options.strip) {
      scriptContent = compressJS(scriptContent, false);
    }

    writeFileSync(path.resolve(options.outputDir, scriptName), scriptContent);
    // insert out-of-lined script into document
    findScriptLocation($).append('<script charset="utf-8" src="' + scriptName + '"></script>');
  }

  deduplicateImports($);

  if (options.strip) {
    removeCommentsAndWhitespace($);
  }

  writeFileSync(options.output, $.html(), true);
}
Ejemplo n.º 3
0
 .then((content) => {
   let $ = whacko.load(content);
   return Promise.resolve($);
 })
Ejemplo n.º 4
0
var baseOutputPath = "./out/";
var specFile = 'single-page.html';

if(process.argv[2] !== undefined) {
	if(process.argv[3] !== undefined) {
		baseOutputPath = process.argv[3];
	}
	specFile = process.argv[2];
}

console.log('This platform is', process.platform, 'node', process.version);

console.log('Loading single-page.html');

var $ = whacko.load(fs.readFileSync(specFile));

var sections = [
     { id: "introduction" }
  ,  { id: "infrastructure" }
  ,  { id: "dom" }
  ,  { id: "semantics" }
  ,  { id: "document-metadata" }
  ,  { id: "sections" }
  ,  { id: "grouping-content" }
  ,  { id: "textlevel-semantics" }
  ,  { id: "edits" }
  ,  { id: "semantics-embedded-content" }
  ,  { id: "links" }
  ,  { id: "tabular-data" }
  ,  { id: "sec-forms" }
Ejemplo n.º 5
0
 }, function(e, res, body) {
   if (e) return cb(e)
   var $ = html.load(body)
   cb(null, parseHostID($))
 })