speech-rule-engine
Version:
A standalone speech rule engine for XML structures, based on the original engine from ChromeVox.
70 lines • 2.05 kB
JavaScript
import { Engine } from './engine.js';
import * as EngineConst from '../common/engine_const.js';
import { SystemExternal } from './system_external.js';
export const xpath = SystemExternal.xpath;
const nameSpaces = {
xhtml: 'http://www.w3.org/1999/xhtml',
mathml: 'http://www.w3.org/1998/Math/MathML',
mml: 'http://www.w3.org/1998/Math/MathML',
svg: 'http://www.w3.org/2000/svg'
};
export function resolveNameSpace(prefix) {
return nameSpaces[prefix] || null;
}
class Resolver {
constructor() {
this.lookupNamespaceURI = resolveNameSpace;
}
}
function evaluateXpath(expression, rootNode, type) {
return xpath.evaluate(expression, rootNode, new Resolver(), type, null);
}
export function evalXPath(expression, rootNode) {
let iterator;
try {
iterator = evaluateXpath(expression, rootNode, xpath.result.ORDERED_NODE_ITERATOR_TYPE);
}
catch (_err) {
return [];
}
const results = [];
for (let xpathNode = iterator.iterateNext(); xpathNode; xpathNode = iterator.iterateNext()) {
results.push(xpathNode);
}
return results;
}
export function evaluateBoolean(expression, rootNode) {
let result;
try {
result = evaluateXpath(expression, rootNode, xpath.result.BOOLEAN_TYPE);
}
catch (_err) {
return false;
}
return result.booleanValue;
}
export function evaluateString(expression, rootNode) {
let result;
try {
result = evaluateXpath(expression, rootNode, xpath.result.STRING_TYPE);
}
catch (_err) {
return '';
}
return result.stringValue;
}
export function updateEvaluator(node) {
if (Engine.getInstance().mode !== EngineConst.Mode.HTTP)
return;
let parent = node;
while (parent && !parent.evaluate) {
parent = parent.parentNode;
}
if (parent && parent.evaluate) {
xpath.currentDocument = parent;
}
else if (node.ownerDocument) {
xpath.currentDocument = node.ownerDocument;
}
}
//# sourceMappingURL=xpath_util.js.map