• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript polymer-analyzer.ParsedHtmlDocument类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中polymer-analyzer.ParsedHtmlDocument的典型用法代码示例。如果您正苦于以下问题:TypeScript ParsedHtmlDocument类的具体用法?TypeScript ParsedHtmlDocument怎么用?TypeScript ParsedHtmlDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了ParsedHtmlDocument类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: getAttributeAstLocation

/**
 * If the position is inside of the node's attributes section, return the
 * correct LocationResult.
 */
function getAttributeAstLocation(
    node: parse5.ASTNode,
    position: SourcePosition,
    document: ParsedHtmlDocument,
    location: Parse5Location): AttributesSection|AttributeValue|undefined {
  /**
   * TODO(rictic): upstream to @types the fact that regular locations (not just
   * element locations) can have attrs sometimes.
   */
  const attrs: parse5.AttributesLocationInfo =
      (isElementLocationInfo(location) && location.startTag.attrs) ||
      location['attrs'] || {};

  for (const attrName in attrs) {
    const range = document.sourceRangeForAttribute(node, attrName);
    if (isPositionInsideRange(position, range)) {
      if (isPositionInsideRange(
              position,
              document.sourceRangeForAttributeValue(node, attrName))) {
        return {kind: 'attributeValue', attribute: attrName, element: node};
      }
      return {kind: 'attribute', attribute: attrName, element: node};
    }
  }
}
开发者ID:Polymer,项目名称:tools,代码行数:29,代码来源:ast-from-source-position.ts


示例2: insertContentAfter

export function insertContentAfter(
    parsedDocument: ParsedHtmlDocument,
    node: dom5.Node,
    replacementText: string): Replacement {
  const tagRange = parsedDocument.sourceRangeForNode(node)!;
  const range = {
    file: tagRange.file,
    start: {line: tagRange.end.line, column: tagRange.end.column},
    end: {line: tagRange.end.line, column: tagRange.end.column}
  };
  return {replacementText, range};
}
开发者ID:MehdiRaash,项目名称:tools,代码行数:12,代码来源:util.ts


示例3: addAttribute

export function addAttribute(
    parsedDocument: ParsedHtmlDocument,
    node: dom5.Node,
    attribute: string,
    attributeValue: string): Replacement {
  const tagRange = parsedDocument.sourceRangeForStartTag(node)!;
  const range = {
    file: tagRange.file,
    start: {line: tagRange.end.line, column: tagRange.end.column - 1},
    end: {line: tagRange.end.line, column: tagRange.end.column - 1}
  };
  const replacementText = ` ${attribute}="${attributeValue}"`;
  return {replacementText, range};
}
开发者ID:MehdiRaash,项目名称:tools,代码行数:14,代码来源:util.ts


示例4: removeNode

export function removeNode(
    parsedDocument: ParsedHtmlDocument, node: dom5.Node): Replacement[] {
  const parentChildren = node.parentNode!.childNodes!;
  const prevNode = parentChildren[parentChildren.indexOf(node)! - 1];
  const fix: Replacement[] = [];
  if (prevNode && dom5.isTextNode(prevNode)) {
    const trailingWhiteSpace =
        removeTrailingWhitespace(prevNode, parsedDocument);
    if (trailingWhiteSpace) {
      fix.push(trailingWhiteSpace);
    }
  }
  fix.push(
      {replacementText: '', range: parsedDocument.sourceRangeForNode(node)!});
  return fix;
}
开发者ID:MehdiRaash,项目名称:tools,代码行数:16,代码来源:util.ts


示例5: removeTrailingWhitespace

export function removeTrailingWhitespace(
    textNode: dom5.Node, parsedDocument: ParsedHtmlDocument) {
  const prevText = dom5.getTextContent(textNode);
  const match = prevText.match(/\n?[ \t]*$/);
  if (!match) {
    return;
  }
  const range = parsedDocument.sourceRangeForNode(textNode)!;
  const lengthOfPreviousLine =
      parsedDocument.newlineIndexes[range.end.line - 1] -
      (parsedDocument.newlineIndexes[range.end.line - 2] || -1) - 1;
  const newRange: SourceRange = {
    ...range,
    start: {
      column: lengthOfPreviousLine,
      line: range.end.line - 1,
    }
  };
  return {range: newRange, replacementText: ''};
}
开发者ID:MehdiRaash,项目名称:tools,代码行数:20,代码来源:util.ts


示例6: internalGetAstLocationForPosition

function internalGetAstLocationForPosition(
    node: parse5.ASTNode, position: SourcePosition,
    document: ParsedHtmlDocument): undefined|HtmlAstLocation {
  const sourceRange = document.sourceRangeForNode(node);
  const location = node.__location;

  /**
   * An HTML5 parser must hallucinate certain nodes, even if they don't exist
   * in the original source text. e.g. <html> or <body>. So we might have
   * elements that have no sourceRange (because they don't exist in the text)
   * but they do have children that do. So we should check those children.
   */
  if (!(sourceRange && location)) {
    return findLocationInChildren(node, position, document);
  }

  if (!isPositionInsideRange(position, sourceRange)) {
    // definitively not in this node or any of its children
    return;
  }

  const locationInChildren = findLocationInChildren(node, position, document);
  if (locationInChildren) {
    return locationInChildren;
  }

  const attributeAstLocation =
      getAttributeAstLocation(node, position, document, location);
  if (attributeAstLocation) {
    return attributeAstLocation;
  }

  const startTagRange = document.sourceRangeForStartTag(node);
  const endTagRange = document.sourceRangeForEndTag(node);

  // If we're in the end tag... we're in the end tag.
  if (isPositionInsideRange(position, endTagRange, false)) {
    return {kind: 'endTag', element: node};
  }

  if (startTagRange && isPositionInsideRange(position, startTagRange, false)) {
    if (position.line === startTagRange.start.line) {
      // If the cursor is in the "<my-elem" part of the start tag.
      if (position.column <=
          startTagRange.start.column + (node.tagName || '').length + 1) {
        return {kind: 'tagName', element: node};
      }
    }
    // Otherwise we're in the start tag, but not in the tag name or any
    // particular attribute, but definitely in the attributes section.
    return {kind: 'attribute', attribute: null, element: node};
  }

  // The edges of a comment aren't part of the comment.
  if (parse5.treeAdapters.default.isCommentNode(node) &&
      isPositionInsideRange(position, sourceRange, false)) {
    return {kind: 'comment', commentNode: node};
  }

  if (parse5.treeAdapters.default.isTextNode(node)) {
    const parent = node.parentNode;
    if (parent && parent.tagName === 'script') {
      return {kind: 'scriptTagContents', textNode: node};
    }
    if (parent && parent.tagName === 'style') {
      return {kind: 'styleTagContents', textNode: node};
    }
    return {kind: 'text', textNode: node};
  }


  if (isPositionInsideRange(position, sourceRange, false)) {
    /**
     * This is tricky. Consider the position inside an empty element, i.e.
     * here:
     *    <script>|</script>.
     *
     * You can be between the start and end tags, but there won't be a text
     * node to attach to, but if you started typeing, there would be, so we
     * want to treat you as though you are.
     */
    if (startTagRange && endTagRange &&
        comparePositionAndRange(position, startTagRange, false) > 0 &&
        comparePositionAndRange(position, endTagRange, false) < 0) {
      if (node.tagName === 'script') {
        return {kind: 'scriptTagContents'};
      }
      if (node.tagName === 'style') {
        return {kind: 'styleTagContents'};
      }
      return {kind: 'text'};
    }

    /**
     * Ok, we're in this node, we're not in any of its children, but we're not
     * obviously in any attribute, tagname, start tag, or end tag. We might be
     * part of a unclosed tag in a mostly empty document. parse5 doesn't give
     * us much explicit signal in this case, but we can kinda infer it from the
     * tagName.
     */
//.........这里部分代码省略.........
开发者ID:MehdiRaash,项目名称:tools,代码行数:101,代码来源:ast-from-source-position.ts


示例7: convertStylesToScriptsThatInsertThem

  private *
      convertStylesToScriptsThatInsertThem(htmlDocument: ParsedHtmlDocument):
          Iterable<Edit> {
    const p = dom5.predicates;
    const head = dom5.nodeWalk(htmlDocument.ast, p.hasTagName('head'));
    const body = dom5.nodeWalk(htmlDocument.ast, p.hasTagName('body'));
    if (head === null || body === null) {
      throw new Error(`HTML Parser error, got a document without a head/body?`);
    }

    const tagsToInsertImperatively = [
      ...dom5.nodeWalkAll(
          head,
          p.OR(
              p.hasTagName('custom-style'),
              p.AND(
                  p.hasTagName('style'),
                  p.NOT(p.parentMatches(p.hasTagName('custom-style')))))),
    ];

    const apology = `<!-- FIXME(polymer-modulizer):
        These imperative modules that innerHTML your HTML are
        a hacky way to be sure that any mixins in included style
        modules are ready before any elements that reference them are
        instantiated, otherwise the CSS @apply mixin polyfill won't be
        able to expand the underlying CSS custom properties.
        See: https://github.com/Polymer/polymer-modulizer/issues/154
        -->
    `.split('\n').join(EOL);
    let first = true;
    for (const tag of tagsToInsertImperatively) {
      const offsets = htmlDocument.sourceRangeToOffsets(
          htmlDocument.sourceRangeForNode(tag)!);
      const scriptTag = parse5.parseFragment(`<script type="module"></script>`)
                            .childNodes![0];
      const program = jsc.program(createDomNodeInsertStatements([tag]));
      dom5.setTextContent(
          scriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      let replacementText = serializeNode(scriptTag);
      if (first) {
        replacementText = apology + replacementText;
        first = false;
      }
      yield {offsets, replacementText};
    }

    for (const bodyNode of body.childNodes || []) {
      if (bodyNode.nodeName.startsWith('#') || bodyNode.tagName === 'script') {
        continue;
      }
      const offsets = htmlDocument.sourceRangeToOffsets(
          htmlDocument.sourceRangeForNode(bodyNode)!);
      const scriptTag = parse5.parseFragment(`<script type="module"></script>`)
                            .childNodes![0];
      const program =
          jsc.program(createDomNodeInsertStatements([bodyNode], true));
      dom5.setTextContent(
          scriptTag,
          EOL +
              recast
                  .print(
                      program, {quote: 'single', wrapColumn: 80, tabWidth: 2})
                  .code +
              EOL);
      let replacementText = serializeNode(scriptTag);
      if (first) {
        replacementText = apology + replacementText;
        first = false;
      }
      yield {offsets, replacementText};
    }
  }
开发者ID:,项目名称:,代码行数:78,代码来源:


示例8: addOrUpdateSourcemapComment

 const promises = inlineScripts.map(scriptAst => {
   let content = dom5.getTextContent(scriptAst);
   const sourceRange = reparsedDoc.sourceRangeForStartTag(scriptAst)!;
   return addOrUpdateSourcemapComment(
              this.bundler.analyzer,
              oldBaseUrl,
              content,
              sourceRange.end.line,
              sourceRange.end.column,
              -sourceRange.end.line + 1,
              -sourceRange.end.column)
       .then(updatedContent => {
         dom5.setTextContent(scriptAst, encodeString(updatedContent));
       });
 });
开发者ID:Polymer,项目名称:vulcanize,代码行数:15,代码来源:html-bundler.ts



注:本文中的polymer-analyzer.ParsedHtmlDocument类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript html-parser.HtmlParser类代码示例发布时间:2022-05-25
下一篇:
TypeScript polymer-analyzer.PackageUrlResolver类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap