node-schematron
Version:
A pure-JS Schematron implementation for Node, browsers and CLI
3 lines (2 loc) • 15.6 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("fontoxpath"),e=require("slimdom");function r(t,e,r,n,o,i,a){try{var u=t[i](a),s=u.value}catch(t){return void r(t)}u.done?e(s):Promise.resolve(s).then(n,o)}function n(t){return function(){var e=this,n=arguments;return new Promise((function(o,i){var a=t.apply(e,n);function u(t){r(a,o,i,u,s,"next",t)}function s(t){r(a,o,i,u,s,"throw",t)}u(void 0)}))}}function o(){return(o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(this,arguments)}function i(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}var a,u=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,r){return t[e]=r}}function s(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),i=new L(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=x(a,r);if(u){if(u===l)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=c(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===l)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}(t,r,i),o}function c(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var l={};function f(){}function h(){}function p(){}var d={};d[o]=function(){return this};var m=Object.getPrototypeOf,v=m&&m(m(R([])));v&&v!==e&&r.call(v,o)&&(d=v);var y=p.prototype=f.prototype=Object.create(d);function g(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,i){function a(){return new e((function(n,a){!function n(o,i,a,u){var s=c(t[o],t,i);if("throw"!==s.type){var l=s.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,a,u)}),(function(t){n("throw",t,a,u)})):e.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return n("throw",t,a,u)}))}u(s.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=c(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function E(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function R(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,i=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return i.next=i}}return{next:P}}function P(){return{value:void 0,done:!0}}return h.prototype=y.constructor=p,p.constructor=h,h.displayName=u(p,a,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,a,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},g(w.prototype),w.prototype[i]=function(){return this},t.AsyncIterator=w,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new w(s(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},g(y),u(y,a,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=R,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return a.type="throw",a.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(u&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,l):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),E(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:R(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(a={exports:{}}),a.exports),s=function(){function t(t,e){this.prefix=t,this.uri=e}return t.fromJson=function(e){return new t(e.prefix,e.uri)},t}();s.QUERY='map {\n\t\t"prefix": @prefix/string(),\n\t\t"uri": @uri/string()\n\t}';var c=function(){function t(t,e,r){this.assertId=e.id,this.isReport=e.isReport,this.context=t,this.message=r}return t.prototype.toJson=function(){return{assertId:this.assertId,isReport:this.isReport,context:e.serializeToWellFormedString(this.context),message:this.message}},t}(),l=function(){function e(t,e,r,n){this.id=t,this.test=e,this.message=r,this.isReport=n}var r=e.prototype;return r.createMessageString=function(e,r,n,o){return o.map((function(o){if("string"==typeof o)return o;if("name"===o.$type)return t.evaluateXPathToString("name("+(o.path||"")+")",e,null,r,n);if("value-of"===o.$type)return t.evaluateXPathToString(o.select,e,null,r,n);throw console.log(o),new Error("Unsupported element in <sch:message>")})).join("")},r.validateNode=function(e,r,n){var o=t.evaluateXPathToBoolean(this.test,e,null,r,n);return!this.isReport&&o||this.isReport&&!o?null:new c(e,this,this.createMessageString(e,r,n,this.message))},e.fromJson=function(t){return new e(t.id,t.test,t.message,t.isReport)},e}();l.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 f=function(){function e(t,e){this.name=t.trim(),this.value=e}return e.reduceVariables=function(e,r,n,o){return r.reduce((function(r,o){var i;return Object.assign(r,((i={})[o.name]=o.value?t.evaluateXPath(o.value,e,null,r,void 0,n):e,i))}),o||{})},e.fromJson=function(t){return new e(t.name,t.value)},e}();f.QUERY="map {\n\t\t'name': @name/string(),\n\t\t'value': @value/string()\n\t}";var h=function(){function t(t,e,r){this.context=t,this.variables=e,this.asserts=r}return t.prototype.validateNode=function(t,e,r){var n=f.reduceVariables(t,this.variables,r,o({},e));return this.asserts.map((function(e){return e.validateNode(t,n,r)})).filter((function(t){return null!==t}))},t.fromJson=function(e){var r=e.variables.map((function(t){return f.fromJson(t)})),n=e.asserts.map((function(t){return l.fromJson(t)}));return new t(e.context.trim(),r,n)},t}();h.QUERY="map {\n\t\t'context': @context/string(),\n\t\t'variables': array { ./(sch:let|sch:param)/"+f.QUERY+"},\n\t\t'asserts': array{ ./(sch:report|sch:assert)/"+l.QUERY+"}\n\t}";var p=function(){function e(t,e,r){this.id=t,this.rules=e,this.variables=r}return e.prototype.validateDocument=function(e,r,n){var i=this,a=f.reduceVariables(e,this.variables,n,o({},r)),u=this.rules.map((function(r){return t.evaluateXPathToNodes("//("+r.context+")",e,null,a,n)}));return Array.from(e.childNodes).reduce((function t(e,r){var o=u.findIndex((function(t){return t.includes(r)})),s=o>=0?i.rules[o]:null;return s&&e.splice.apply(e,[e.length,0].concat(s.validateNode(r,a,n))),Array.from(r.childNodes).reduce(t,e)}),[])},e.fromJson=function(t){return new e(t.id,t.rules.map((function(t){return h.fromJson(t)})),t.variables.map((function(t){return f.fromJson(t)})))},e}();p.QUERY="map {\n\t\t'id': @id/string(),\n\t\t'rules': array{ ./sch:rule/"+h.QUERY+"},\n\t\t'variables': array { ./sch:let/"+f.QUERY+"}\n\t}";var d=function(){function t(t,e,r){this.id=t,this.active=e,this.variables=r}return t.fromJson=function(e){return new t(e.id,e.active,e.variables.map((function(t){return f.fromJson(t)})))},t}();d.QUERY='map {\n\t\t"id": @id/string(),\n\t\t"active": array { ./sch:active/@pattern/string() },\n\t\t\'variables\': array { ./sch:let/'+f.QUERY+"}\n\t}";var m=function(){function r(t,e,r,n,o,i){this.title=t,this.defaultPhase=e,this.variables=r,this.phases=n,this.patterns=o,this.namespaces=i}var a=r.prototype;return a.validateString=function(t,r){return this.validateDocument(e.parseXmlDocument(t),r)},a.validateDocument=function(t,e){var r=this,n=e||{},i=n.phaseId,a=n.debug;i||(i="#DEFAULT"),"#DEFAULT"===i&&(i=this.defaultPhase||"#ALL");var u={namespaceResolver:this.getNamespaceUriForPrefix.bind(this),debug:a},s=f.reduceVariables(t,this.variables,u,{});if("#ALL"===i)return this.patterns.reduce((function(e,r){return e.concat(r.validateDocument(t,s,u))}),[]);var c=this.phases.find((function(t){return t.id===i})),l=f.reduceVariables(t,(null==c?void 0:c.variables)||[],u,o({},s));return(null==c?void 0:c.active.map((function(t){return r.patterns.find((function(e){return e.id===t}))})).reduce((function(e,r){return e.concat((null==r?void 0:r.validateDocument(t,l,u))||[])}),[]))||[]},a.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},r.getElementsByTagName=function(t,e){var r=[];return function t(n){1===n.nodeType&&n.tagName===e&&r.push(n);for(var o,a=function(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return i(t,void 0);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,void 0):void 0}}(t))){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(n.childNodes);!(o=a()).done;)t(o.value)}(t),r},r.fromJson=function(t){return new r(t.title,t.defaultPhase,t.variables.map((function(t){return f.fromJson(t)})),t.phases.map((function(t){return d.fromJson(t)})),t.patterns.map((function(t){return p.fromJson(t)})),t.namespaces.map((function(t){return s.fromJson(t)})))},r.fromDomToJson=function(o,i){var a=function(){var r=n(u.mark((function r(o,s){return u.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(null!=i&&i.fetchReference){r.next=2;break}return r.abrupt("return");case 2:return r.next=4,Promise.all(t.evaluateXPathToNodes(".//include[@href]",s).map(function(){var t=n(u.mark((function t(r){var n,s,c,l;return u.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return c=r.getAttribute("href"),t.t0=e.parseXmlDocument,t.next=4,i.fetchReference(c,o);case 4:if(t.t1=t.sent,(l=(0,t.t0)(t.t1)).documentElement){t.next=8;break}throw new Error("Included document must have a root element");case 8:return t.next=10,a([].concat(o,[c]),l);case 10:null==(n=r.parentNode)||n.insertBefore(l.documentElement,r),null==(s=r.parentNode)||s.removeChild(r);case 12:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()));case 4:case"end":return r.stop()}}),r)})));return function(t,e){return r.apply(this,arguments)}}();return i?a([],o).then((function(){return r.fromDomToJson(o)})):t.evaluateXPath(r.QUERY,o,null,{},void 0,{language:t.evaluateXPath.XQUERY_3_1_LANGUAGE})},r.fromDom=function(t,e){return e?r.fromDomToJson(t,e).then((function(t){return r.fromJson(t)})):r.fromJson(r.fromDomToJson(t))},r.fromString=function(t,n){var o=e.parseXmlDocument(t);return n?r.fromDom(o,n):r.fromDom(o)},r}();m.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/"+d.QUERY+"},\n\t\t\t'patterns': array { $context/sch:pattern/"+p.QUERY+"},\n\t\t\t'variables': array { $context/sch:let/"+f.QUERY+"},\n\t\t\t'namespaces': array { $context/sch:ns/"+s.QUERY+"}\n\t\t}\n\t",Object.defineProperty(exports,"registerCustomXPathFunction",{enumerable:!0,get:function(){return t.registerCustomXPathFunction}}),exports.Schema=m;
//# sourceMappingURL=node-schematron.cjs.production.min.js.map