How to use the parse5/lib/tree-adapters/default.createElement function in parse5

To help you get started, we’ve selected a few parse5 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bazelbuild / rules_nodejs / packages / inject-html / index.js View on Github external
return matches.length > 0;
      }

      // Note: empty string value is equivalent to a bare attribute, according to
      // https://github.com/inikulin/parse5/issues/1
      const nomodule = hasMatchingModule(s, jsFiles) ? [{name: 'nomodule', value: ''}] : [];

      const noModuleScript = treeAdapter.createElement('script', undefined, nomodule.concat([
        {name: 'src', value: `/${relative(s)}?v=${timestamp()}`},
      ]));
      treeAdapter.appendChild(body, noModuleScript);
    }
  }

  for (const s of params.filter(s => /\.css$/.test(s))) {
    const stylesheet = treeAdapter.createElement('link', undefined, [
      {name: 'rel', value: 'stylesheet'},
      {name: 'href', value: `/${relative(s)}?v=${timestamp()}`},
    ]);
    treeAdapter.appendChild(head, stylesheet);
  }

  const content = parse5.serialize(document, {treeAdapter});
  write(outputFile, content, {encoding: 'utf-8'});
  return 0;
}
github bazelbuild / rules_nodejs / packages / inject-html / index.js View on Github external
function hasMatchingModule(file, files) {
        const noExt = file.substring(0, file.length - 3);
        const testMjs = (noExt + '.mjs').toLowerCase();
        const testEs2015 = (noExt + '.es2015.js').toLowerCase();
        const matches = files.filter(t => {
          const lc = t.toLowerCase();
          return lc === testMjs || lc === testEs2015;
        });
        return matches.length > 0;
      }

      // Note: empty string value is equivalent to a bare attribute, according to
      // https://github.com/inikulin/parse5/issues/1
      const nomodule = hasMatchingModule(s, jsFiles) ? [{name: 'nomodule', value: ''}] : [];

      const noModuleScript = treeAdapter.createElement('script', undefined, nomodule.concat([
        {name: 'src', value: `/${relative(s)}?v=${timestamp()}`},
      ]));
      treeAdapter.appendChild(body, noModuleScript);
    }
  }

  for (const s of params.filter(s => /\.css$/.test(s))) {
    const stylesheet = treeAdapter.createElement('link', undefined, [
      {name: 'rel', value: 'stylesheet'},
      {name: 'href', value: `/${relative(s)}?v=${timestamp()}`},
    ]);
    treeAdapter.appendChild(head, stylesheet);
  }

  const content = parse5.serialize(document, {treeAdapter});
  write(outputFile, content, {encoding: 'utf-8'});
github bazelbuild / rules_nodejs / packages / inject-html / index.js View on Github external
if (execPath.startsWith(r)) {
        return execPath.substring(r.length);
      }
    }
    return execPath;
  }

  const jsFiles = params.filter(s => /\.m?js$/i.test(s));
  for (const s of jsFiles) {
    // Differential loading: for filenames like
    //  foo.mjs
    //  bar.es2015.js
    // we use a