當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript parse5.Parser類代碼示例

本文整理匯總了TypeScript中parse5.Parser的典型用法代碼示例。如果您正苦於以下問題:TypeScript Parser類的具體用法?TypeScript Parser怎麽用?TypeScript Parser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Parser類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: clone

 clone(node) {
   // e.g. document fragment
   if (node.type === 'root') {
     var serialized = serializer.serialize(node);
     var newParser = new parse5.Parser(parse5.TreeAdapters.htmlparser2);
     return newParser.parseFragment(serialized);
   } else {
     var temp = treeAdapter.createElement("template", null, []);
     treeAdapter.appendChild(temp, node);
     var serialized = serializer.serialize(temp);
     var newParser = new parse5.Parser(parse5.TreeAdapters.htmlparser2);
     return newParser.parseFragment(serialized).childNodes[0];
   }
 }
開發者ID:CADBOT,項目名稱:angular,代碼行數:14,代碼來源:parse5_adapter.ts

示例2: parseHtml

export function parseHtml(html: string): Node {
  let parser = new parse5.Parser(_ADAPTER);
  let root = parser.parse(html);
  // Normally, "root" is the document node and contains the optional doctype
  // node and the HTML node as children.  We'll skip the doctype node and
  // return the only HTML node.
  let element:Node = null;
  for (let child of _ADAPTER.getChildNodes(root)) {
    if (!_ADAPTER.isDocumentTypeNode(child)) {
      if (element !== null) {
        throw Error("Found more than one element at the root level while parsing HTML text");
      }
      element = child;
    }
  }
  return element;
}
開發者ID:chirayuk,項目名稱:i18n,代碼行數:17,代碼來源:parse_html.ts

示例3: setInnerHTML

 setInnerHTML(el, value) {
   this.clearNodes(el);
   var content = parser.parseFragment(value);
   for (var i = 0; i < content.childNodes.length; i++) {
     treeAdapter.appendChild(el, content.childNodes[i]);
   }
 }
開發者ID:CADBOT,項目名稱:angular,代碼行數:7,代碼來源:parse5_adapter.ts

示例4: createTemplate

 createTemplate(html) {
   var template = treeAdapter.createElement("template", 'http://www.w3.org/1999/xhtml', []);
   var content = parser.parseFragment(html);
   treeAdapter.appendChild(template, content);
   return template;
 }
開發者ID:CADBOT,項目名稱:angular,代碼行數:6,代碼來源:parse5_adapter.ts

示例5: parseDocument

export function parseDocument(documentHtml: string): Object {
  if (!documentHtml) {
    throw new Error('parseDocument requires a document string');
  }
  if (typeof documentHtml !== 'string') {
    throw new Error('parseDocument needs to be a string to be parsed correctly');
  }

  const doc = parser.parse(documentHtml);

  /*
  // Build entire doc <!doctype><html> etc
  if (documentHtml.indexOf('<html>') > -1 && documentHtml.indexOf('</html>') > -1) {
    const doc = parser.parse(documentHtml);
  }
  // ASP.NET case : parse only the fragment - don't build entire <html> doc
  const doc = parser.parseFragment(documentHtml);
  */

  let rootNode;
  let bodyNode;
  let headNode;
  let titleNode;

  for (let i = 0; i < doc.children.length; ++i) {
    const child = doc.children[i];

    if (isTag('html', child)) {
      rootNode = child;
      break;
    }
  }

  if (!rootNode) {
    rootNode = doc;
  }

  for (let i = 0; i < rootNode.children.length; ++i) {
    const child = rootNode.children[i];

    if (isTag('head', child)) {
      headNode = child;
    }

    if (isTag('body', child)) {
      bodyNode = child;
    }
  }

  if (!headNode) {
    headNode = treeAdapter.createElement('head', null, []);
    DOM.appendChild(doc, headNode);
  }

  if (!bodyNode) {
    bodyNode = treeAdapter.createElement('body', null, []);
    DOM.appendChild(doc, bodyNode);
  }

  for (let i = 0; i < headNode.children.length; ++i) {
    if (isTag('title', headNode.children[i])) {
      titleNode = headNode.children[i];
      break;
    }
  }

  if (!titleNode) {
    titleNode = treeAdapter.createElement('title', null, []);
    DOM.appendChild(headNode, titleNode);
  }

  doc._window = {};
  doc.head = headNode;
  doc.body = bodyNode;

  const titleNodeText = titleNode.children[0];

  Object.defineProperty(doc, 'title', {
    get: () => titleNodeText.data,
    set: (newTitle) => titleNodeText.data = newTitle
  });

  return doc;
}
開發者ID:Hyperkind,項目名稱:universal,代碼行數:84,代碼來源:document.ts

示例6: parseFragment

export function parseFragment(el: string): Object {
  return parser.parseFragment(el);
}
開發者ID:Hyperkind,項目名稱:universal,代碼行數:3,代碼來源:document.ts

示例7: parseFragment

export function parseFragment (el: string): any {
  return parser.parseFragment(el);
}
開發者ID:carloserodriguez2000,項目名稱:universal,代碼行數:3,代碼來源:node-document.ts

示例8: parseDocument

export function parseDocument(documentHtml: string): Object {
  const doc = parser.parse(documentHtml);
  let rootNode;
  let bodyNode;
  let headNode;
  let titleNode;

  for (let i = 0; i < doc.children.length; ++i) {
    const child = doc.children[i];

    if (isTag('html', child)) {
      rootNode = child;
      break;
    }
  }

  if (!rootNode) {
    rootNode = doc;
  }

  for (let i = 0; i < rootNode.children.length; ++i) {
    const child = rootNode.children[i];

    if (isTag('head', child)) {
      headNode = child;
    }

    if (isTag('body', child)) {
      bodyNode = child;
    }
  }

  if (!headNode) {
    headNode = treeAdapter.createElement('head', null, []);
    DOM.appendChild(doc, headNode);
  }

  if (!bodyNode) {
    bodyNode = treeAdapter.createElement('body', null, []);
    DOM.appendChild(doc, headNode);
  }

  for (let i = 0; i < headNode.children.length; ++i) {
    if (isTag('title', headNode.children[i])) {
      titleNode = headNode.children[i];
      break;
    }
  }

  if (!titleNode) {
    titleNode = treeAdapter.createElement('title', null, []);
    DOM.appendChild(headNode, titleNode);
  }

  doc._window = {};
  doc.head = headNode;
  doc.body = bodyNode;

  const titleNodeText = titleNode.children[0];

  Object.defineProperty(doc, 'title', {
    get: () => titleNodeText.data,
    set: (newTitle) => titleNodeText.data = newTitle
  });

  return doc;
}
開發者ID:acinader,項目名稱:universal,代碼行數:67,代碼來源:document.ts


注:本文中的parse5.Parser類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。