Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] Detecting javascript code (UNCLASSIFIED)

Expand Messages
  • Cheney, Edward A SSG RES USAR USARC
    Classification: UNCLASSIFIED ... This is the logic I use for Pretty Diff: if (apilang === auto ) { autotest = true; if (new RegExp(/^( s*
    Message 1 of 1 , Feb 27, 2011
    View Source
    • 0 Attachment
      Classification: UNCLASSIFIED

      > I'm trying to build a tool that detectsjava_script code in any kind of language or file format, for example PHP, ASP.NET, etc. . Not just HTML.

      This is the logic I use for Pretty Diff:

      if (apilang === "auto") {
      autotest = true;
      if (new RegExp(/^(\s*<)/).test(apisource) === false && new RegExp(/(>\s*)$/).test(apisource) === false) {
      if (new RegExp(/^(\s*\{)/).test(apisource) === true && new RegExp(/(\}\s*)$/).test(apisource) === true && apisource.indexOf(",") !== -1) {
      apilang = "javascript";
      auto = "JSON";
      } else if (new RegExp(/((\}?(\(\))?\)*;?\s*)|([a-z0-9]("|')?\)*);?(\s*\})*)$/i).test(apisource) === true && (new RegExp(/var\s+[a-z]+[a-zA-Z0-9]*/).test(apisource) === true || new RegExp(/(\=\s*function)|(\s*function\s+[a-zA-Z])/).test(apisource) === true || apisource.indexOf("{") === -1)) {
      apilang = "javascript";
      auto = "JavaScript";
      } else if (new RegExp(/^(\s*[\.#@a-z0-9])/i).test(apisource) === true && apisource.indexOf("{") !== -1) {
      apilang = "css";
      auto = "CSS";
      } else {
      apilang = "javascript";
      auto = "unknown";
      }
      } else if (new RegExp(/>([a-z0-9\s])*<\/?[a-z]+>/i).test(apisource) === true && new RegExp(/^(\s*<)/).test(apisource) === true && new RegExp(/(>\s*)$/).test(apisource) === true) {
      apilang = "markup";
      if (apihtml === true) {
      auto = "HTML";
      } else {
      auto = "markup";
      }
      } else {
      apilang = "javascript";
      auto = "unknown";
      }
      if (auto === "unknown") {
      auto = "<p>Language set to <strong>auto</strong>, but language could not be determined. Language defaulted to <em>JavaScript</em>.";
      } else {
      auto = "<p>Language set to <strong>auto</strong>. Presumed language is <em>" + auto + "</em>.</p>";
      }
      }

      I have never seen this logic fail to accurately tell the difference between JavaScript, JSON, CSS, and markup. Just I have never seen it fail and nobody has reported its failure to me does not necessarily mean it is perfect, so use at your own risk or provide a manual language selection alternative.

      Austin Cheney, CISSP
      http://prettydiff.com/
      Classification: UNCLASSIFIED
    Your message has been successfully submitted and would be delivered to recipients shortly.