本文整理汇总了TypeScript中polymer-analyzer.ParsedHtmlDocument.sourceRangeToOffsets方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ParsedHtmlDocument.sourceRangeToOffsets方法的具体用法?TypeScript ParsedHtmlDocument.sourceRangeToOffsets怎么用?TypeScript ParsedHtmlDocument.sourceRangeToOffsets使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类polymer-analyzer.ParsedHtmlDocument
的用法示例。
在下文中一共展示了ParsedHtmlDocument.sourceRangeToOffsets方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: 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};
}
}