rsnext/packages/next/compiled/amphtml-validator/index.js

17 lines
34 KiB
JavaScript
Raw Normal View History

(()=>{var e={254:(e,t,r)=>{var n=r(361).EventEmitter;var o=r(81).spawn;var i=r(17);var s=i.dirname;var a=i.basename;var u=r(147);r(837).inherits(Command,n);t=e.exports=new Command;t.Command=Command;t.Option=Option;function Option(e,t){this.flags=e;this.required=~e.indexOf("<");this.optional=~e.indexOf("[");this.bool=!~e.indexOf("-no-");e=e.split(/[ ,|]+/);if(e.length>1&&!/^[[<]/.test(e[1]))this.short=e.shift();this.long=e.shift();this.description=t||""}Option.prototype.name=function(){return this.long.replace("--","").replace("no-","")};Option.prototype.attributeName=function(){return camelcase(this.name())};Option.prototype.is=function(e){return this.short===e||this.long===e};function Command(e){this.commands=[];this.options=[];this._execs={};this._allowUnknownOption=false;this._args=[];this._name=e||""}Command.prototype.command=function(e,t,r){if(typeof t==="object"&&t!==null){r=t;t=null}r=r||{};var n=e.split(/ +/);var o=new Command(n.shift());if(t){o.description(t);this.executables=true;this._execs[o._name]=true;if(r.isDefault)this.defaultExecutable=o._name}o._noHelp=!!r.noHelp;this.commands.push(o);o.parseExpectedArgs(n);o.parent=this;if(t)return this;return o};Command.prototype.arguments=function(e){return this.parseExpectedArgs(e.split(/ +/))};Command.prototype.addImplicitHelpCommand=function(){this.command("help [cmd]","display help for [cmd]")};Command.prototype.parseExpectedArgs=function(e){if(!e.length)return;var t=this;e.forEach((function(e){var r={required:false,name:"",variadic:false};switch(e[0]){case"<":r.required=true;r.name=e.slice(1,-1);break;case"[":r.name=e.slice(1,-1);break}if(r.name.length>3&&r.name.slice(-3)==="..."){r.variadic=true;r.name=r.name.slice(0,-3)}if(r.name){t._args.push(r)}}));return this};Command.prototype.action=function(e){var t=this;var listener=function(r,n){r=r||[];n=n||[];var o=t.parseOptions(n);outputHelpIfNecessary(t,o.unknown);if(o.unknown.length>0){t.unknownOption(o.unknown[0])}if(o.args.length)r=o.args.concat(r);t._args.forEach((function(e,n){if(e.required&&r[n]==null){t.missingArgument(e.name)}else if(e.variadic){if(n!==t._args.length-1){t.variadicArgNotLast(e.name)}r[n]=r.splice(n)}}));if(t._args.length){r[t._args.length]=t}else{r.push(t)}e.apply(t,r)};var r=this.parent||this;var n=r===this?"*":this._name;r.on("command:"+n,listener);if(this._alias)r.on("command:"+this._alias,listener);return this};Command.prototype.option=function(e,t,r,n){var o=this,i=new Option(e,t),s=i.name(),a=i.attributeName();if(typeof r!=="function"){if(r instanceof RegExp){var u=r;r=function(e,t){var r=u.exec(e);return r?r[0]:t}}else{n=r;r=null}}if(!i.bool||i.optional||i.required){if(!i.bool)n=true;if(n!==undefined){o[a]=n;i.defaultValue=n}}this.options.push(i);this.on("option:"+s,(function(e){if(e!==null&&r){e=r(e,o[a]===undefined?n:o[a])}if(typeof o[a]==="boolean"||typeof o[a]==="undefined"){if(e==null){o[a]=i.bool?n||true:false}else{o[a]=e}}else if(e!==null){o[a]=e}}));return this};Command.prototype.allowUnknownOption=function(e){this._allowUnknownOption=arguments.length===0||e;return this};Command.prototype.parse=function(e){if(this.executables)this.addImplicitHelpCommand();this.rawArgs=e;this._name=this._name||a(e[1],".js");if(this.executables&&e.length<3&&!this.defaultExecutable){e.push("--help")}var t=this.parseOptions(this.normalize(e.slice(2)));var r=this.args=t.args;var n=this.parseArgs(this.args,t.unknown);var o=n.args[0];var i=null;if(o){i=this.commands.filter((function(e){return e.alias()===o}))[0]}if(this._execs[o]&&typeof this._execs[o]!=="function"){return this.executeSubCommand(e,r,t.unknown)}else if(i){r[0]=i._name;return this.executeSubCommand(e,r,t.unknown)}else if(this.defaultExecutable){r.unshift(this.defaultExecutable);return this.executeSubCommand(e,r,t.unknown)}return n};Command.prototype.executeSubCommand=function(e,t,r){t=t.concat(r);if(!t.length)this.help();if(t[0]==="help"&&t.length===1)this.help();if(t[0]==="help"){t[0]=t[1];t[1]="--help"}var n=e[1];var l=a(n,".js")+"-"+t[0];var c,f=u.lstatSync(n).isSymbolicLink()?u.readlinkSync(n):n;if(f!==n&&f.charAt(0)!=="/"){f=i.j
/**
* @license
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the license.
*/const t=__nccwpck_require__(671);const n=__nccwpck_require__(147);const o=__nccwpck_require__(685);const i=__nccwpck_require__(687);const s=__nccwpck_require__(17);const a=__nccwpck_require__(254);const u=__nccwpck_require__(544);const l=__nccwpck_require__(477);const c=__nccwpck_require__(310);const f=__nccwpck_require__(837);const p=__nccwpck_require__(144);const h="amphtml-validator";function hasPrefix(e,t){return e.indexOf(t)==0}function isHttpOrHttpsUrl(e){return hasPrefix(e,"http://")||hasPrefix(e,"https://")}function readFromFile(e){return new u((function(t,r){n.readFile(e,"utf8",(function(e,n){if(e){r(e)}else{t(n.trim())}}))}))}function readFromReadable(e,t){return new u((function(r,n){const o=[];t.setEncoding("utf8");t.on("data",(function(e){o.push(e)}));t.on("end",(function(){r(o.join(""))}));t.on("error",(function(t){n(new Error("Could not read from "+e+" - "+t.message))}))}))}function readFromStdin(){return readFromReadable("stdin",process.stdin).then((function(e){process.stdin.resume();return e}))}function readFromUrl(e,t){return new u((function(r,n){const s=hasPrefix(e,"http://")?o:i;const a=s.request(e,(function(t){if(t.statusCode!==200){t.resume();n(new Error("Unable to fetch "+e+" - HTTP Status "+t.statusCode))}else{r(t)}}));a.setHeader("User-Agent",t);a.on("error",(function(t){n(new Error("Unable to fetch "+e+" - "+t.message))}));a.end()})).then(readFromReadable.bind(null,e))}function ValidationResult(){this.status="UNKNOWN";this.errors=[]}function ValidationError(){this.severity="UNKNOWN_SEVERITY";this.line=1;this.col=0;this.message="";this.specUrl=null;this.code="UNKNOWN_CODE";this.params=[]}function Validator(e){this.sandbox=p.createContext();try{new p.Script(e).runInContext(this.sandbox)}catch(e){throw new Error("Could not instantiate validator.js - "+e.message)}}Validator.prototype.validateString=function(e,t){const r=this.sandbox.amp.validator.validateString(e,t);const n=new ValidationResult;n.status=r.status;for(let e=0;e<r.errors.length;e++){const t=r.errors[e];const o=new ValidationError;o.severity=t.severity;o.line=t.line;o.col=t.col;o.message=this.sandbox.amp.validator.renderErrorMessage(t);o.specUrl=t.specUrl;o.code=t.code;o.params=t.params;n.errors.push(o)}return n};const m={};function getInstance(e,t){const r=e||"https://cdn.ampproject.org/v0/validator.js";const n=t||h;if(m.hasOwnProperty(r)){return u.resolve(m[r])}const o=isHttpOrHttpsUrl(r)?readFromUrl(r,n):readFromFile(r);return o.then((function(e){let t;try{t=new Validator(e)}catch(e){throw e}m[r]=t;return t}))}e.getInstance=getInstance;function newInstance(e){return new Validator(e)}e.newInstance=newInstance;function logValidationResult(e,r,n){if(r.status==="PASS"){process.stdout.write(e+": "+(n?t.green("PASS"):"PASS")+"\n")}for(let o=0;o<r.errors.length;o++){const i=r.errors[o];let s=e+":"+i.line+":"+i.col+" ";if(n){s+=(i.severity==="ERROR"?t.red:t.magenta)(i.message)}else{s+=i.message}if(i.specUrl){s+=" (see "+i.specUrl+")"}process.stderr.write(s+"\n")}}function main(){a.usage("[options] <fileOrUrlOrMinus...>\n\n"+' Validates the files or urls provided as arguments. If "-" is\n'+" specified, reads from stdin instead.").option("--validator_js <fileOrUrl>","The Validator Javascript.\n"+" Latest published version by default, or\n"+" dist/validator_minified.js (built with build.py)\n"+" for development.","https://cdn.ampproject.org/v0/validator.js").option("--user-agent <userAgent>","User agent string to use in requests.",h).option("--html_format <AMP|AMP4ADS|AMP4EMAIL>","The input format to be validated.\n"+" AMP by default.","AMP").option("--format <color|text|json>","How to format the output.\n"+' "color" displays errors/warnings/success in\n'+" red/orange/green.\n"+' "text" avoids color (e.g., useful in terminals not\n'+" supporting color).\n"+' "json" emits json corresponding to the ValidationResult\n'+" message in validator.proto.","color").parse(process.argv);if(a.args.length===0){a.outputHelp();process.exit(1)}if(a.html_format!=="AMP"&&a.html_format!=="AMP4ADS"&&a.html_format!=="AMP4EMAI