Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
visitContainedNodes(nodes: Node[]): void {
const length = nodes.length;
let index = 0;
while (index < length) {
if (!this.isPlaceholderContainer(nodes[index])) {
const startOfContainedNodes = index;
while (index < length - 1) {
index++;
if (this.isPlaceholderContainer(nodes[index])) {
break;
}
}
if (index - startOfContainedNodes > 1) {
// Only create a container if there are two or more contained Nodes in a row
this.renderer.startContainer();
visitAll(this, nodes.slice(startOfContainedNodes, index - 1));
this.renderer.closeContainer();
}
}
if (index < length) {
nodes[index].visit(this, undefined);
}
index++;
}
}
export function getPathToNodeAtPosition(nodes: Node[], position: number): HtmlAstPath {
const path: Node[] = [];
const visitor = new class extends RecursiveVisitor {
visit(ast: Node) {
const span = spanOf(ast);
if (inSpan(position, span)) {
path.push(ast);
} else {
// Returning a truthy value here will skip all children and terminate
// the visit.
return true;
}
}
};
visitAll(visitor, nodes);
return new AstPath(path, position);
}
visitElement(element: Element, context: any): any {
if (element.name === 'unit') {
const externalId = getAttrOrThrow(element, 'id');
if (this.translations[externalId] !== undefined) {
throw new TranslationParseError(
element.sourceSpan, `Duplicated translations for message "${externalId}"`);
}
visitAll(this, element.children, {unit: externalId});
} else if (element.name === 'segment') {
assertTranslationUnit(element, context);
const targetMessage = element.children.find(isTargetElement);
if (targetMessage === undefined) {
throw new TranslationParseError(element.sourceSpan, 'Missing required element');
}
this.translations[context.unit] = serializeTargetMessage(targetMessage);
} else {
return visitAll(this, element.children);
}
}
}
let i18nHtmlParser = new compiler.I18NHtmlParser(htmlParser);
let parsed = i18nHtmlParser.parse(content, this.resourcePath, false);
if(1 > parsed.errors.length) {
if(!templatesGenerated) {
visitor.generateTemplates(true);
templatesGenerated = true;
}
else {
visitor.generateTemplates(false);
}
result =
containers +
'' + compiler.visitAll(visitor, parsed.rootNodes).join('') + '' +
visitor.getTemplates(true);
}
else {
console.error(parsed.errors);
}
}
return result;
};
if (element.name === 'unit') {
const externalId = getAttrOrThrow(element, 'id');
if (this.translations[externalId] !== undefined) {
throw new TranslationParseError(
element.sourceSpan, `Duplicated translations for message "${externalId}"`);
}
visitAll(this, element.children, {unit: externalId});
} else if (element.name === 'segment') {
assertTranslationUnit(element, context);
const targetMessage = element.children.find(isTargetElement);
if (targetMessage === undefined) {
throw new TranslationParseError(element.sourceSpan, 'Missing required element');
}
this.translations[context.unit] = serializeTargetMessage(targetMessage);
} else {
return visitAll(this, element.children);
}
}
}
visitElement(element: Element, {parsedLocale}: BundleVisitorContext): any {
if (element.name === 'xliff') {
parsedLocale = getAttribute(element, 'trgLang');
return visitAll(this, element.children, {parsedLocale});
} else if (element.name === 'file') {
this.bundle = {
locale: parsedLocale,
translations: Xliff2TranslationVisitor.extractTranslations(element)
};
} else {
return visitAll(this, element.children, {parsedLocale});
}
}
}
static extractTranslations(file: Element): Record {
const visitor = new this();
visitAll(visitor, file.children);
return visitor.translations;
}
visitExpansionCase(expansionCase: ExpansionCase): void {
this.renderer.text(` ${expansionCase.value} {`);
this.renderer.startContainer();
visitAll(this, expansionCase.expression);
this.renderer.closeContainer();
this.renderer.text(`}`);
}
function visit(children: T[] | undefined) {
if (children) results.push(visitAll(visitor, children, context));
}
cb(visit);
static extractBundle(xliff: Node[]): ParsedTranslationBundle|undefined {
const visitor = new this();
visitAll(visitor, xliff);
return visitor.bundle;
}