Home | History | Annotate | Download | only in RegExp
      1 /*
      2 * The contents of this file are subject to the Netscape Public
      3 * License Version 1.1 (the "License"); you may not use this file
      4 * except in compliance with the License. You may obtain a copy of
      5 * the License at http://www.mozilla.org/NPL/
      6 *
      7 * Software distributed under the License is distributed on an "AS  IS"
      8 * basis, WITHOUT WARRANTY OF ANY KIND, either expressed
      9 * or implied. See the License for the specific language governing
     10 * rights and limitations under the License.
     11 *
     12 * The Original Code is mozilla.org code.
     13 *
     14 * The Initial Developer of the Original Code is Netscape
     15 * Communications Corporation.  Portions created by Netscape are
     16 * Copyright (C) 1998 Netscape Communications Corporation.
     17 * All Rights Reserved.
     18 *
     19 * Contributor(s): bedney (at) technicalpursuit.com, pschwartau (at) netscape.com
     20 * Date: 04 October 2001
     21 *
     22 * SUMMARY:  Arose from Bugzilla bug 103087:
     23 * "The RegExp MarkupSPE in demo crashes Mozilla"
     24 *
     25 * See http://bugzilla.mozilla.org/show_bug.cgi?id=103087
     26 * The SpiderMonkey shell crashed on some of these regexps.
     27 *
     28 * The reported crash was on i=24 below ('MarkupSPE' regexp)
     29 * I crashed on that, and also on i=43 ('XML_SPE' regexp)
     30 */
     31 //-----------------------------------------------------------------------------
     32 var UBound = 0;
     33 var bug = 103087;
     34 var summary = "Testing that we don't crash on any of these regexps -";
     35 var re = '';
     36 var lm = '';
     37 var lc = '';
     38 var rc = '';
     39 
     40 
     41 // the regexps are built in pieces  -
     42 var NameStrt = "[A-Za-z_:]|[^\\x00-\\x7F]";
     43 var NameChar = "[A-Za-z0-9_:.-]|[^\\x00-\\x7F]";
     44 var Name = "(" + NameStrt + ")(" + NameChar + ")*";
     45 var TextSE = "[^<]+";
     46 var UntilHyphen = "[^-]*-";
     47 var Until2Hyphens = UntilHyphen + "([^-]" + UntilHyphen + ")*-";
     48 var CommentCE = Until2Hyphens + ">?";
     49 var UntilRSBs = "[^]]*]([^]]+])*]+";
     50 var CDATA_CE = UntilRSBs + "([^]>]" + UntilRSBs + ")*>";
     51 var S = "[ \\n\\t\\r]+";
     52 var QuoteSE = '"[^"]' + "*" + '"' + "|'[^']*'";
     53 var DT_IdentSE = S + Name + "(" + S + "(" + Name + "|" + QuoteSE + "))*";
     54 var MarkupDeclCE = "([^]\"'><]+|" + QuoteSE + ")*>";
     55 var S1 = "[\\n\\r\\t ]";
     56 var UntilQMs = "[^?]*\\?+";
     57 var PI_Tail = "\\?>|" + S1 + UntilQMs + "([^>?]" + UntilQMs + ")*>";
     58 var DT_ItemSE = "<(!(--" + Until2Hyphens + ">|[^-]" + MarkupDeclCE + ")|\\?" + Name + "(" + PI_Tail + "))|%" + Name + ";|" + S;
     59 var DocTypeCE = DT_IdentSE + "(" + S + ")?(\\[(" + DT_ItemSE + ")*](" + S + ")?)?>?";
     60 var DeclCE = "--(" + CommentCE + ")?|\\[CDATA\\[(" + CDATA_CE + ")?|DOCTYPE(" + DocTypeCE + ")?";
     61 var PI_CE = Name + "(" + PI_Tail + ")?";
     62 var EndTagCE = Name + "(" + S + ")?>?";
     63 var AttValSE = '"[^<"]' + "*" + '"' + "|'[^<']*'";
     64 var ElemTagCE = Name + "(" + S + Name + "(" + S + ")?=(" + S + ")?(" + AttValSE + "))*(" + S + ")?/?>?";
     65 var MarkupSPE = "<(!(" + DeclCE + ")?|\\?(" + PI_CE + ")?|/(" + EndTagCE + ")?|(" + ElemTagCE + ")?)";
     66 var XML_SPE = TextSE + "|" + MarkupSPE;
     67 var CommentRE = "<!--" + Until2Hyphens + ">";
     68 var CommentSPE = "<!--(" + CommentCE + ")?";
     69 var PI_RE = "<\\?" + Name + "(" + PI_Tail + ")";
     70 var Erroneous_PI_SE = "<\\?[^?]*(\\?[^>]+)*\\?>";
     71 var PI_SPE = "<\\?(" + PI_CE + ")?";
     72 var CDATA_RE = "<!\\[CDATA\\[" + CDATA_CE;
     73 var CDATA_SPE = "<!\\[CDATA\\[(" + CDATA_CE + ")?";
     74 var ElemTagSE = "<(" + NameStrt + ")([^<>\"']+|" + AttValSE + ")*>";
     75 var ElemTagRE = "<" + Name + "(" + S + Name + "(" + S + ")?=(" + S + ")?(" + AttValSE + "))*(" + S + ")?/?>";
     76 var ElemTagSPE = "<" + ElemTagCE;
     77 var EndTagRE = "</" + Name + "(" + S + ")?>";
     78 var EndTagSPE = "</(" + EndTagCE + ")?";
     79 var DocTypeSPE = "<!DOCTYPE(" + DocTypeCE + ")?";
     80 var PERef_APE = "%(" + Name + ";?)?";
     81 var HexPart = "x([0-9a-fA-F]+;?)?";
     82 var NumPart = "#([0-9]+;?|" + HexPart + ")?";
     83 var CGRef_APE = "&(" + Name + ";?|" + NumPart + ")?";
     84 var Text_PE = CGRef_APE + "|[^&]+";
     85 var EntityValue_PE = CGRef_APE + "|" + PERef_APE + "|[^%&]+";
     86 
     87 
     88 var rePatterns = new Array(AttValSE, CDATA_CE, CDATA_RE, CDATA_SPE, CGRef_APE, CommentCE, CommentRE, CommentSPE, DT_IdentSE, DT_ItemSE, DeclCE, DocTypeCE, DocTypeSPE, ElemTagCE, ElemTagRE, ElemTagSE, ElemTagSPE, EndTagCE, EndTagRE, EndTagSPE, EntityValue_PE, Erroneous_PI_SE, HexPart, MarkupDeclCE, MarkupSPE, Name, NameChar, NameStrt, NumPart, PERef_APE, PI_CE, PI_RE, PI_SPE, PI_Tail, QuoteSE, S, S1, TextSE, Text_PE, Until2Hyphens, UntilHyphen, UntilQMs, UntilRSBs, XML_SPE);
     89 
     90 
     91 // here's a big string to test the regexps on -
     92 var str = '';
     93 str += '<html xmlns="http://www.w3.org/1999/xhtml"' + '\n';
     94 str += '      xmlns:xlink="http://www.w3.org/XML/XLink/0.9">' + '\n';
     95 str += '  <head><title>Three Namespaces</title></head>' + '\n';
     96 str += '  <body>' + '\n';
     97 str += '    <h1 align="center">An Ellipse and a Rectangle</h1>' + '\n';
     98 str += '    <svg xmlns="http://www.w3.org/Graphics/SVG/SVG-19991203.dtd" ' + '\n';
     99 str += '         width="12cm" height="10cm">' + '\n';
    100 str += '      <ellipse rx="110" ry="130" />' + '\n';
    101 str += '      <rect x="4cm" y="1cm" width="3cm" height="6cm" />' + '\n';
    102 str += '    </svg>' + '\n';
    103 str += '    <p xlink:type="simple" xlink:href="ellipses.html">' + '\n';
    104 str += '      More about ellipses' + '\n';
    105 str += '    </p>' + '\n';
    106 str += '    <p xlink:type="simple" xlink:href="rectangles.html">' + '\n';
    107 str += '      More about rectangles' + '\n';
    108 str += '    </p>' + '\n';
    109 str += '    <hr/>' + '\n';
    110 str += '    <p>Last Modified February 13, 2000</p>    ' + '\n';
    111 str += '  </body>' + '\n';
    112 str += '</html>';
    113 
    114 
    115 
    116 //-----------------------------------------------------------------------------
    117 test();
    118 //-----------------------------------------------------------------------------
    119 
    120 
    121 
    122 function test()
    123 {
    124   enterFunc ('test');
    125   printBugNumber (bug);
    126   printStatus (summary);
    127 
    128   for (var i=0; i<rePatterns.length; i++)
    129   {
    130     status = inSection(i);
    131     re = new RegExp(rePatterns[i]);
    132 
    133     // Test that we don't crash on any of these -
    134     re.exec(str);
    135     getResults();
    136 
    137     // Just for the heck of it, test the current leftContext
    138     re.exec(lc);
    139     getResults();
    140 
    141     // Test the current rightContext
    142     re.exec(rc);
    143     getResults();
    144   }
    145 
    146   exitFunc ('test');
    147 }
    148 
    149 
    150 function getResults()
    151 {
    152   lm = RegExp.lastMatch;
    153   lc = RegExp.leftContext;
    154   rc = RegExp.rightContext;
    155 }
    156