本文整理匯總了TypeScript中polymer-analyzer.ParsedHtmlDocument.sourceRangeForNode方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript ParsedHtmlDocument.sourceRangeForNode方法的具體用法?TypeScript ParsedHtmlDocument.sourceRangeForNode怎麽用?TypeScript ParsedHtmlDocument.sourceRangeForNode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類polymer-analyzer.ParsedHtmlDocument
的用法示例。
在下文中一共展示了ParsedHtmlDocument.sourceRangeForNode方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: 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};
}
示例2: 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;
}
示例3: 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: ''};
}
示例4: 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.
*/
//.........這裏部分代碼省略.........
示例5: 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};
}
}