angular-l10n
Version:
An Angular library to translate messages, dates and numbers
85 lines • 2.46 kB
JavaScript
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* Breadth First Search (BFS) algorithm for traversing & searching tree data structure of DOM
* explores the neighbor nodes first, before moving to the next level neighbors.
* Time complexity: between O(1) and O(|V|^2).
*/
export class BFS {
/**
* Target node is a non empty text node.
* @param {?} rootNode
* @return {?}
*/
static getTargetNode(rootNode) {
return BFS.walk(rootNode);
}
/**
* @param {?} rootNode
* @return {?}
*/
static walk(rootNode) {
/** @type {?} */
const queue = [];
/** @type {?} */
let iNode;
/** @type {?} */
let depth = 0;
/** @type {?} */
let nodeToDepthIncrease = 1;
queue.push(rootNode);
while (queue.length > 0 && depth <= BFS.MAX_DEPTH) {
iNode = queue.shift();
if (BFS.isTargetNode(iNode)) {
return iNode;
}
if (depth < BFS.MAX_DEPTH && iNode.childNodes != null) {
for (const child of iNode.childNodes) {
if (BFS.isValidNode(child)) {
queue.push(child);
}
}
}
if (--nodeToDepthIncrease == 0) {
depth++;
nodeToDepthIncrease = queue.length;
}
}
return rootNode;
}
/**
* @param {?} node
* @return {?}
*/
static isTargetNode(node) {
return typeof node !== "undefined" &&
node.nodeType == 3 &&
node.nodeValue != null &&
node.nodeValue.trim() != "";
}
/**
* A valid node is not marked for translation.
* @param {?} node
* @return {?}
*/
static isValidNode(node) {
if (typeof node !== "undefined" && node.nodeType == 1 && node.attributes) {
for (const attr of node.attributes) {
if (attr && BFS.SELECTOR.test(attr.name))
return false;
}
}
return true;
}
}
BFS.SELECTOR = new RegExp("^l10n|translate|locale");
BFS.MAX_DEPTH = 10;
if (false) {
/** @type {?} */
BFS.SELECTOR;
/** @type {?} */
BFS.MAX_DEPTH;
}
//# sourceMappingURL=bfs.js.map