Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Ensure that flat matches with metachars in them don't have their metachars |
michael@0 | 3 | * interpreted as special. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | function isPatternSyntaxError(pattern) { |
michael@0 | 7 | try { |
michael@0 | 8 | new RegExp(pattern); |
michael@0 | 9 | return false; |
michael@0 | 10 | } catch (e if e instanceof SyntaxError) { |
michael@0 | 11 | return true; |
michael@0 | 12 | } |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | // Bug example. |
michael@0 | 16 | assertEq("1+2".replace("1+2", "$&+3", "g"), "1+2+3"); |
michael@0 | 17 | assertEq("1112".replace("1+2", "", "g"), "1112"); |
michael@0 | 18 | |
michael@0 | 19 | // ^ |
michael@0 | 20 | assertEq("leading text^my hat".replace("^my hat", "", "g"), "leading text"); |
michael@0 | 21 | assertEq("my hat".replace("^my hat", "", "g"), "my hat"); |
michael@0 | 22 | |
michael@0 | 23 | // $ |
michael@0 | 24 | assertEq("leading text$my money".replace("leading text$", "", "g"), "my money"); |
michael@0 | 25 | assertEq("leading text".replace("leading text$", "", "g"), "leading text"); |
michael@0 | 26 | |
michael@0 | 27 | // \ |
michael@0 | 28 | var BSL = '\\'; |
michael@0 | 29 | assertEq(("dir C:" + BSL + "Windoze").replace("C:" + BSL, "", "g"), |
michael@0 | 30 | "dir Windoze"); |
michael@0 | 31 | assertEq(isPatternSyntaxError("C:" + BSL), true); |
michael@0 | 32 | |
michael@0 | 33 | // . |
michael@0 | 34 | assertEq("This is a sentence. It has words.".replace(".", "!", "g"), |
michael@0 | 35 | "This is a sentence! It has words!"); |
michael@0 | 36 | assertEq("This is an unterminated sentence".replace(".", "", "g"), |
michael@0 | 37 | "This is an unterminated sentence"); |
michael@0 | 38 | |
michael@0 | 39 | // * |
michael@0 | 40 | assertEq("Video killed the radio *".replace(" *", "", "g"), "Video killed the radio"); |
michael@0 | 41 | assertEq("aaa".replace("a*", "", "g"), "aaa"); |
michael@0 | 42 | |
michael@0 | 43 | // + |
michael@0 | 44 | assertEq("On the + side".replace(" +", "", "g"), "On the side"); |
michael@0 | 45 | assertEq("1111111111111".replace("1+", "", "g"), "1111111111111"); |
michael@0 | 46 | |
michael@0 | 47 | // \+ |
michael@0 | 48 | assertEq(("Inverse cone head: " + BSL + "++/").replace(BSL + "+", "", "g"), |
michael@0 | 49 | "Inverse cone head: +/"); |
michael@0 | 50 | assertEq((BSL + BSL + BSL).replace(BSL + "+", "", "g"), |
michael@0 | 51 | BSL + BSL + BSL); |
michael@0 | 52 | |
michael@0 | 53 | // \\+ |
michael@0 | 54 | assertEq((BSL + BSL + "+").replace(BSL + BSL + "+", "", "g"), ""); |
michael@0 | 55 | assertEq((BSL + BSL + BSL).replace(BSL + BSL + "+", "", "g"), (BSL + BSL + BSL)); |
michael@0 | 56 | |
michael@0 | 57 | // \\\ |
michael@0 | 58 | assertEq((BSL + BSL + BSL + BSL).replace(BSL + BSL + BSL, "", "g"), BSL); |
michael@0 | 59 | assertEq(isPatternSyntaxError(BSL + BSL + BSL), true); |
michael@0 | 60 | |
michael@0 | 61 | // \\\\ |
michael@0 | 62 | assertEq((BSL + BSL + BSL + BSL).replace(BSL + BSL + BSL + BSL, "", "i"), ""); |
michael@0 | 63 | assertEq((BSL + BSL).replace(BSL + BSL + BSL + BSL, "", "g"), BSL + BSL); |
michael@0 | 64 | |
michael@0 | 65 | |
michael@0 | 66 | // ? |
michael@0 | 67 | assertEq("Pressing question?".replace("?", ".", "g"), "Pressing question."); |
michael@0 | 68 | assertEq("a".replace("a?", "", "g"), "a"); |
michael@0 | 69 | |
michael@0 | 70 | // ( |
michael@0 | 71 | assertEq("(a".replace("(", "", "g"), "a"); |
michael@0 | 72 | |
michael@0 | 73 | // ) |
michael@0 | 74 | assertEq("a)".replace(")", "", "g"), "a"); |
michael@0 | 75 | |
michael@0 | 76 | // ( and ) |
michael@0 | 77 | assertEq("(foo)".replace("(foo)", "", "g"), ""); |
michael@0 | 78 | assertEq("a".replace("(a)", "", "g"), "a"); |
michael@0 | 79 | |
michael@0 | 80 | // [ |
michael@0 | 81 | assertEq("[a".replace("[", "", "g"), "a"); |
michael@0 | 82 | |
michael@0 | 83 | // ] |
michael@0 | 84 | assertEq("a]".replace("]", "", "g"), "a"); |
michael@0 | 85 | |
michael@0 | 86 | // [ and ] |
michael@0 | 87 | assertEq("a".replace("[a-z]", "", "g"), "a"); |
michael@0 | 88 | assertEq("You would write your regexp as [a-z]".replace("[a-z]", "", "g"), |
michael@0 | 89 | "You would write your regexp as "); |
michael@0 | 90 | |
michael@0 | 91 | // { |
michael@0 | 92 | assertEq("Numbers may be specified in the interval {1,100}".replace("{1,", "", "g"), |
michael@0 | 93 | "Numbers may be specified in the interval 100}"); |
michael@0 | 94 | |
michael@0 | 95 | // } |
michael@0 | 96 | assertEq("Numbers may be specified in the interval {1,100}".replace(",100}", "", "g"), |
michael@0 | 97 | "Numbers may be specified in the interval {1"); |
michael@0 | 98 | |
michael@0 | 99 | // { and } |
michael@0 | 100 | assertEq("Numbers may be specified in the interval {1,100}".replace(" {1,100}", "", "g"), |
michael@0 | 101 | "Numbers may be specified in the interval"); |
michael@0 | 102 | assertEq("aaa".replace("a{1,10}", "", "g"), "aaa"); |
michael@0 | 103 | |
michael@0 | 104 | // | |
michael@0 | 105 | assertEq("Mr. Gorbachev|Tear down this wall!".replace("|Tear down this wall!", "", "g"), |
michael@0 | 106 | "Mr. Gorbachev"); |
michael@0 | 107 | assertEq("foobar".replace("foo|bar", "", "g"), "foobar"); |
michael@0 | 108 | |
michael@0 | 109 | print("PASS"); |