angular-l10n
Version:
An Angular library to translate messages, dates and numbers
115 lines • 3.2 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).
*/
var BFS = /** @class */ (function () {
function BFS() {
}
/**
* Target node is a non empty text node.
*/
/**
* Target node is a non empty text node.
* @param {?} rootNode
* @return {?}
*/
BFS.getTargetNode = /**
* Target node is a non empty text node.
* @param {?} rootNode
* @return {?}
*/
function (rootNode) {
return BFS.walk(rootNode);
};
/**
* @param {?} rootNode
* @return {?}
*/
BFS.walk = /**
* @param {?} rootNode
* @return {?}
*/
function (rootNode) {
/** @type {?} */
var queue = [];
/** @type {?} */
var iNode;
/** @type {?} */
var depth = 0;
/** @type {?} */
var 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 (var _i = 0, _a = iNode.childNodes; _i < _a.length; _i++) {
var child = _a[_i];
if (BFS.isValidNode(child)) {
queue.push(child);
}
}
}
if (--nodeToDepthIncrease == 0) {
depth++;
nodeToDepthIncrease = queue.length;
}
}
return rootNode;
};
/**
* @param {?} node
* @return {?}
*/
BFS.isTargetNode = /**
* @param {?} node
* @return {?}
*/
function (node) {
return typeof node !== "undefined" &&
node.nodeType == 3 &&
node.nodeValue != null &&
node.nodeValue.trim() != "";
};
/**
* A valid node is not marked for translation.
*/
/**
* A valid node is not marked for translation.
* @param {?} node
* @return {?}
*/
BFS.isValidNode = /**
* A valid node is not marked for translation.
* @param {?} node
* @return {?}
*/
function (node) {
if (typeof node !== "undefined" && node.nodeType == 1 && node.attributes) {
for (var _i = 0, _a = node.attributes; _i < _a.length; _i++) {
var attr = _a[_i];
if (attr && BFS.SELECTOR.test(attr.name))
return false;
}
}
return true;
};
BFS.SELECTOR = new RegExp("^l10n|translate|locale");
BFS.MAX_DEPTH = 10;
return BFS;
}());
export { BFS };
if (false) {
/** @type {?} */
BFS.SELECTOR;
/** @type {?} */
BFS.MAX_DEPTH;
}
//# sourceMappingURL=bfs.js.map