本文整理汇总了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};
}
}
}
示例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};
}
示例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};
}
示例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;
}
示例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: ''};
}
示例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.
*/
//.........这里部分代码省略.........
示例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};
}
}
示例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));
});
});