How to use the parse5.parseFragment 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 pluralsight-projects / Vue-BookListing / test / unit / mocha / part1 / book-list-will-contain-title-interpolation.spec.js View on Github external
it('should contain title interpolation in h1 @book-list-will-contain-title-interpolation', () => {
    let file;
    try {
      file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookList.vue'), 'utf8');
    } catch (e) {
      assert(false, 'The BookList.vue file does not exist');
    }

    // Parse document
    const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
    const nodes = doc.childNodes;

    // Parse for HTML in template
    const template = nodes.filter(node => node.nodeName === 'template');
    
    if (template.length == 0) {
      assert(false, "The BookList component does not contain a template tag")
    }
    
    const content = parse5.serialize(template[0].content);
    const dom = new JSDOM(content, { includeNodeLocations: true });
    const document = dom.window.document;

    // Test for booklist in the app div
    const results = document.querySelector('h1');
    assert(results != null, "The BookList template does not contain an h1 tag")
github pluralsight-projects / Vue-BookListing / test / unit / mocha / part1 / book-list-will-have-correct-styles.spec.js View on Github external
it('should contain correct styles @book-list-vue-will-have-correct-styles', () => {
    let file;
    try {
      file = fs.readFileSync(path.join(process.cwd(), 'src/components/BookList.vue'), 'utf8');
    } catch (e) {
      assert(false, 'The BookList.vue file does not exist');
    }

    // Parse document and retrieve the style section
    const doc = parse5.parseFragment(file.replace(/\n/g, ''), { locationInfo: true });
    const nodes = doc.childNodes;
    const styles = nodes.filter(node => node.nodeName === 'style');

    if (styles.length == 0) {
      assert(false, "The BookList.vue file does not contain a style element.")
    }
    if (styles[0].childNodes.length == 0) {
      assert(false, "The BookList style tag does not contain any CSS rules.")
    }
    
    const style = styles[0].childNodes[0].value;
    const parsed = cssom.parse(style);

    const results = parsed.cssRules.find(node => node.selectorText);

    assert(results.selectorText, 'The `"h1, h2"` selector is not present in BookList\'s styles');
github DefinitelyTyped / DefinitelyTyped / types / parse5 / parse5-tests.ts View on Github external
scriptingEnabled: false,
    treeAdapter: defaultAdapter
};

parse5.parse("", opt); // $ExpectType Document

opt = {
    sourceCodeLocationInfo: true,
    scriptingEnabled: false,
    treeAdapter: defaultAdapter
};

parse5.parse("", opt); // $ExpectType Document

// parseFragment
const fragment = parse5.parseFragment("<div>");

fragment; // $ExpectType DocumentFragment

parse5.parseFragment("<div>", {});
parse5.parseFragment("<div>", { sourceCodeLocationInfo: true });
parse5.parseFragment("<div>", { treeAdapter: defaultAdapter });
parse5.parseFragment("<div>", {
    sourceCodeLocationInfo: true,
    treeAdapter: defaultAdapter
});
parse5.parseFragment("<div>", {
    sourceCodeLocationInfo: true,
    treeAdapter: defaultAdapter
});

const element = (parse5.parseFragment(</div></div></div></div></div></div>
github gimenete / unicycle / src / sketch.ts View on Github external
const parseSvg = (
    text: string,
    additionalAttributes: parse5.AST.Default.Attribute[]
  ) => {
    const parsedSVG = parse5.parseFragment(
      text
    ) as parse5.AST.Default.DocumentFragment
    const root = parsedSVG.childNodes.find(
      node => node.nodeName === 'svg'
    )! as parse5.AST.Default.Element

    // if it's a super simple SVG, ignore it
    const isSuperSimple = (node: parse5.AST.Default.Node): boolean => {
      const elemnt = node as parse5.AST.Default.Element
      if (!elemnt.childNodes) return true
      const notSimpleElement = !['svg', 'path', 'g', 'defs', 'desc'].includes(
        elemnt.nodeName
      )
      if (notSimpleElement) {
        return false
      }
github vuejs / rollup-plugin-vue / src / compiler.js View on Github external
function parseContent (content) {
  // noinspection JSValidateTypes
  return parse5.parseFragment(content, {locationInfo: true})
}
github DevExpress / testcafe-hammerhead / src / processing / resources / page.ts View on Github external
private _createRestoreStoragesScript (storageKey, storages) {
        const scriptStr              = createSelfRemovingScript(`
            window.localStorage.setItem("${ storageKey }", ${ JSON.stringify(storages.localStorage) });
            window.sessionStorage.setItem("${ storageKey }", ${ JSON.stringify(storages.sessionStorage) });
        `);
        const parsedDocumentFragment = parse5.parseFragment(scriptStr);

        return parsedDocumentFragment.childNodes[0];
    }
github hsuanxyz / nz-ssvgg / index.js View on Github external
inTagMatch.forEach(e => {
    const htmlFragment = parse5.parseFragment(e);
    let _names = [];
    if (htmlFragment && htmlFragment.childNodes && htmlFragment.childNodes[0]) {
      let type = htmlFragment.childNodes[0].attrs.find(attr => ['type', '[type]', 'nztype', '[nztype]'].indexOf(attr.name) !== -1);
      let theme = htmlFragment.childNodes[0].attrs.find(attr => ['theme', '[theme]', 'nztheme', '[nztheme]'].indexOf(attr.name) !== -1);

      /**
       * type="icon"
       * nzType="icon"
       */
      if (type && ['type', 'nztype'].indexOf(type.name) !== -1 && /^[A-Za-z]/g.test(type.value) && type.value.indexOf(' ') === -1) {
        _names.push(type.value);
      }

      /**
       * [type]="value ? 'icon' : 'icon'"
       * [nzType]="value ? 'icon' : 'icon'"
github Promact / md2 / libs / @angular / platform-server / @angular / platform-server.js View on Github external
setInnerHTML(el, value) {
        this.clearNodes(el);
        const /** @type {?} */ content = parse5$1.parseFragment(value, { treeAdapter });
        for (let /** @type {?} */ i = 0; i &lt; content.childNodes.length; i++) {
            treeAdapter.appendChild(el, content.childNodes[i]);
        }
    }
    /**
github angular / components / src / cdk / schematics / ng-update / html-parsing / elements.ts View on Github external
export function findElementsWithAttribute(html: string, attributeName: string) {
  const document = parseFragment(html, {sourceCodeLocationInfo: true}) as DefaultTreeDocument;
  const elements: DefaultTreeElement[] = [];

  const visitNodes = nodes => {
    nodes.forEach(node => {
      if (node.childNodes) {
        visitNodes(node.childNodes);
      }

      if (node.attrs && node.attrs.some(attr => attr.name === attributeName.toLowerCase())) {
        elements.push(node);
      }
    });
  };

  visitNodes(document.childNodes);
github ionic-team / stencil / src / mock-doc / parse-util.ts View on Github external
export function parseFragmentUtil(ownerDocument: any, html: string) {
  if (typeof html === 'string') {
    html = html.trim();
  } else {
    html = '';
  }
  const frag = parseFragment(
    html,
    getParser(ownerDocument)
  ) as any;
  return frag;
}