UNPKG

esvit-node-schematron

Version:

A pure-JS Schematron implementation for Node, browsers and CLI

3 lines (2 loc) 8.02 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("fontoxpath"),n=(t=require("fs"))&&"object"==typeof t&&"default"in t?t.default:t,r=require("slimdom-sax-parser");function a(){return(a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var o=function(){function t(t,e){this.prefix=t,this.uri=e}return t.fromJson=function(e){return new t(e.prefix,e.uri)},t}();o.QUERY='map {\n\t\t"prefix": @prefix/string(),\n\t\t"uri": @uri/string()\n\t}';var s=function(){function t(t,e,n){this.assertId=e.id,this.isReport=e.isReport,this.context=t,this.message=n}return t.prototype.toJson=function(){return{assertId:this.assertId,isReport:this.isReport,context:this.context.outerHTML,message:this.message}},t}(),u=function(){function t(t,e,n,r){this.id=t,this.test=e,this.message=n,this.isReport=r}var n=t.prototype;return n.createMessageString=function(t,n,r,a){return a.map((function(a){if("string"==typeof a)return a;if("name"===a.$type)return e.evaluateXPathToString("name("+(a.path||"")+")",t,null,n,r);if("value-of"===a.$type)return e.evaluateXPathToString(a.select,t,null,n,r);throw console.log(a),new Error("Unsupported element in <sch:message>")})).join("")},n.validateNode=function(t,n,r){var a=e.evaluateXPathToBoolean(this.test,t,null,n,r);return!this.isReport&&a||this.isReport&&!a?null:new s(t,this,this.createMessageString(t,n,r,this.message))},t.fromJson=function(e){return new t(e.id,e.test,e.message,e.isReport)},t}();u.QUERY="map {\n\t\t'id': if (@id) then string(@id) else (),\n\t\t'test': @test/string(),\n\t\t'message': array { (./text()|./element())/local:json(.) },\n\t\t'isReport': boolean(local-name() = 'report')\n\t}";var c=function(){function t(t,e){this.name=t.trim(),this.value=e}return t.reduceVariables=function(t,n,r,a){return n.reduce((function(n,a){var i;return Object.assign(n,((i={})[a.name]=a.value?e.evaluateXPath(a.value,t,null,n,void 0,r):t,i))}),a||{})},t.fromJson=function(e){return new t(e.name,e.value)},t}();c.QUERY="map {\n\t\t'name': @name/string(),\n\t\t'value': @value/string()\n\t}";var l=function(){function t(t,e,n){this.context=t,this.variables=e,this.asserts=n}return t.prototype.validateNode=function(t,e,n){var r=c.reduceVariables(t,this.variables,n,a({},e));return this.asserts.map((function(e){return e.validateNode(t,r,n)})).filter((function(t){return null!==t}))},t.fromJson=function(e){var n=e.variables.map((function(t){return c.fromJson(t)})),r=e.asserts.map((function(t){return u.fromJson(t)}));return new t(e.context.trim(),n,r)},t}();l.QUERY="map {\n\t\t'context': @context/string(),\n\t\t'variables': array { ./(sch:let|sch:param)/"+c.QUERY+"},\n\t\t'asserts': array{ ./(sch:report|sch:assert)/"+u.QUERY+"}\n\t}";var f=function(){function t(t,e,n){this.id=t,this.rules=e,this.variables=n}return t.prototype.validateDocument=function(t,n,r){var i=this,o=c.reduceVariables(t,this.variables,r,a({},n)),s=this.rules.map((function(n){return e.evaluateXPathToNodes("//("+n.context+")",t,null,o,r)}));return Array.from(t.childNodes).reduce((function t(e,n){var a=s.findIndex((function(t){return t.includes(n)})),u=a>=0?i.rules[a]:null;return u&&e.splice.apply(e,[e.length,0].concat(u.validateNode(n,o,r))),Array.from(n.childNodes).reduce(t,e)}),[])},t.fromJson=function(e){return new t(e.id,e.rules.map((function(t){return l.fromJson(t)})),e.variables.map((function(t){return c.fromJson(t)})))},t}();f.QUERY="map {\n\t\t'id': @id/string(),\n\t\t'rules': array{ ./sch:rule/"+l.QUERY+"},\n\t\t'variables': array { ./sch:let/"+c.QUERY+"}\n\t}";var m=function(){function t(t,e,n){this.id=t,this.active=e,this.variables=n}return t.fromJson=function(e){return new t(e.id,e.active,e.variables.map((function(t){return c.fromJson(t)})))},t}();m.QUERY='map {\n\t\t"id": @id/string(),\n\t\t"active": array { ./sch:active/@pattern/string() },\n\t\t\'variables\': array { ./sch:let/'+c.QUERY+"}\n\t}";var d=function(){function t(t,e,n,r,a,i){this.title=t,this.defaultPhase=e,this.variables=n,this.phases=r,this.patterns=a,this.namespaces=i}var s=t.prototype;return s.validateString=function(t,e){return this.validateDocument(r.sync(t),e)},s.validateDocument=function(t,e){var n=this,r=e||{},i=r.phaseId,o=r.debug;i||(i="#DEFAULT"),"#DEFAULT"===i&&(i=this.defaultPhase||"#ALL");var s={namespaceResolver:this.getNamespaceUriForPrefix.bind(this),debug:o},u=c.reduceVariables(t,this.variables,s,{});if("#ALL"===i)return this.patterns.reduce((function(e,n){return e.concat(n.validateDocument(t,u,s))}),[]);var l=this.phases.find((function(t){return t.id===i})),f=c.reduceVariables(t,(null==l?void 0:l.variables)||[],s,a({},u));return(null==l?void 0:l.active.map((function(t){return n.patterns.find((function(e){return e.id===t}))})).reduce((function(e,n){return e.concat((null==n?void 0:n.validateDocument(t,f,s))||[])}),[]))||[]},s.getNamespaceUriForPrefix=function(t){if(!t)return null;var e=this.namespaces.find((function(e){return e.prefix===t}));if(!e)throw new Error('Namespace prefix "'+t+'" could not be resolved to an URI using <sch:ns>');return e.uri},t.getElementsByTagName=function(t,e){var n=[];return function t(r){1===r.nodeType&&r.tagName===e&&n.push(r);for(var a,o=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return i(t,void 0);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?i(t,void 0):void 0}}(t))){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(r.childNodes);!(a=o()).done;)t(a.value)}(t),n},t.processIncludes=function(e,a){void 0===a&&(a={});for(var i=this.getElementsByTagName(e,"include"),o=0;o<i.length;o++){var s=i[o],u=s.getAttribute("href");if(u){var c,l,f=n.readFileSync(a.resourceDir+"/"+u,"utf-8"),m=r.sync(f);t.processIncludes(m),null==(c=s.parentNode)||c.insertBefore(m.documentElement,s),null==(l=s.parentNode)||l.removeChild(s)}}},t.fromJson=function(e){return new t(e.title,e.defaultPhase,e.variables.map((function(t){return c.fromJson(t)})),e.phases.map((function(t){return m.fromJson(t)})),e.patterns.map((function(t){return f.fromJson(t)})),e.namespaces.map((function(t){return o.fromJson(t)})))},t.fromDomToJson=function(n){return e.evaluateXPath(t.QUERY,n,null,{},void 0,{language:e.evaluateXPath.XQUERY_3_1_LANGUAGE})},t.fromDom=function(e){return t.fromJson(t.fromDomToJson(e))},t.fromString=function(e,n){var a=r.sync(e);return t.processIncludes(a,n),t.fromDom(a)},t}();d.QUERY="\n\t\tdeclare namespace sch = 'http://purl.oclc.org/dsdl/schematron';\n\n\t\tdeclare function local:json($node as node()) {\n\t\t\tif ($node[self::text()])\n\t\t\t\tthen $node/string()\n\t\t\telse\n\t\t\tmap:merge((\n\t\t\t\tmap:entry('$type', $node/local-name()),\n\t\t\t\tfor $attr in $node/@*\n\t\t\t\t\treturn map:entry($attr/name(), $attr/string())\n\t\t\t))\n\t\t};\n\n\t\tlet $context := /*[1]\n\t\treturn map {\n\t\t\t'title': $context/@title/string(),\n\t\t\t'defaultPhase': $context/@defaultPhase/string(),\n\t\t\t'phases': array { $context/sch:phase/"+m.QUERY+"},\n\t\t\t'patterns': array { $context/sch:pattern/"+f.QUERY+"},\n\t\t\t'variables': array { $context/sch:let/"+c.QUERY+"},\n\t\t\t'namespaces': array { $context/sch:ns/"+o.QUERY+"}\n\t\t}\n\t",Object.defineProperty(exports,"registerCustomXPathFunction",{enumerable:!0,get:function(){return e.registerCustomXPathFunction}}),exports.Schema=d; //# sourceMappingURL=esvit-node-schematron.cjs.production.min.js.map