Skip to content
Browse files

added html encoding w/ new test suite (now failing some conformance t…

…ests)
  • Loading branch information...
1 parent 4793613 commit 814b6fb576b8ce15a2e03b49d6b5739bc7e39cba @bcherry bcherry committed
Showing with 129 additions and 71 deletions.
  1. BIN .DS_Store
  2. +1 −1 .gitignore
  3. +10 −4 Rakefile
  4. BIN test/.DS_Store
  5. +82 −0 test/conformance.html
  6. +1 −64 test/test.html
  7. +19 −0 test/tests.js
  8. +16 −2 twitter-text.js
View
BIN .DS_Store
Binary file not shown.
View
2 .gitignore
@@ -1 +1 @@
-test/cases.js
+test/conformance.js
View
14 Rakefile
@@ -4,8 +4,6 @@ require 'json'
require 'date'
require 'digest'
-
-
def conformance_version(dir)
Dir[File.join(dir, '*')].inject(Digest::SHA1.new){|digest, file| digest.update(Digest::SHA1.file(file).hexdigest) }
end
@@ -41,7 +39,7 @@ namespace :test do
test_files = ['autolink', 'extract', 'hit_highlighting']
r = {}
- f = File.open(File.join(File.dirname(__FILE__), "test", "cases.js"), "w")
+ f = File.open(File.join(File.dirname(__FILE__), "test", "conformance.js"), "w")
f.write("var cases = {};")
test_files.each do |test_file|
@@ -55,11 +53,19 @@ namespace :test do
desc "Run conformance test suite"
task :run do
- exec('open test/test.html')
+ exec('open test/conformance.html')
end
end
desc "Run conformance test suite"
task :conformance => ['conformance:latest', 'conformance:prepare', 'conformance:run'] do
end
+
+ desc "Run JavaScript test suite"
+ task :run do
+ exec('open test/test.html')
+ end
end
+
+desc "Run JavaScript test suite"
+task :test => ['test:run']
View
BIN test/.DS_Store
Binary file not shown.
View
82 test/conformance.html
@@ -0,0 +1,82 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <script src="../lib/jquery-1.4.2.js"></script>
+ <link rel="stylesheet" href="../lib/qunit.css" type="text/css" media="screen" />
+ <script src="../lib/qunit.js"></script>
+
+ <script src="../twitter-text.js"></script>
+
+ <script src="../test/conformance.js"></script>
+
+ <script>
+ twttr.util = {
+ hitHighlight: function(text) { return text; },
+ };
+
+ function getTester(suite, section) {
+ switch (suite) {
+ case "autolink":
+ return function(test) {
+ return twttr.txt.autoLink(test.text, {suppressNoFollow: true, suppressDataScreenName: true});
+ };
+ case "extract":
+ switch (section) {
+ case "mentions":
+ return function(test) {
+ return twttr.txt.extractMentions(test.text);
+ };
+ case "replies":
+ return function(test) {
+ return twttr.txt.extractReplies(test.text);
+ };
+ case "urls":
+ return function(test) {
+ return twttr.txt.extractUrls(test.text);
+ };
+ case "hashtags":
+ return function(test) {
+ return twttr.txt.extractHashtags(test.text);
+ };
+ case "hashtags_with_indices":
+ return function(test) {
+ return twttr.txt.extractHashtagsWithIndices(test.text);
+ };
+ }
+ case "hit_highlighting":
+ return function(test) {
+ return twttr.txt.hitHighlight(test.text, test.hits);
+ };
+ }
+
+ }
+
+
+ for (var suite in cases) {
+ (function(suite) {
+ module(suite);
+
+ for (var section in cases[suite]) {
+ (function(section) {
+ var tester = getTester(suite, section);
+ test(section, function() {
+ for (var testCase in cases[suite][section]) {
+ same(tester(cases[suite][section][testCase]), cases[suite][section][testCase].expected, cases[suite][section][testCase].description);
+ }
+ });
+ }(section));
+ }
+ }(suite));
+
+ }
+ </script>
+
+</head>
+<body>
+ <h1 id="qunit-header">Twitter Text Conformance Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+</body>
+</html>
View
65 test/test.html
@@ -8,70 +8,7 @@
<script src="../twitter-text.js"></script>
- <script src="../test/cases.js"></script>
-
- <script>
- twttr.util = {
- hitHighlight: function(text) { return text; },
- };
-
- function getTester(suite, section) {
- switch (suite) {
- case "autolink":
- return function(test) {
- return twttr.txt.autoLink(test.text, {suppressNoFollow: true, suppressDataScreenName: true});
- };
- case "extract":
- switch (section) {
- case "mentions":
- return function(test) {
- return twttr.txt.extractMentions(test.text);
- };
- case "replies":
- return function(test) {
- return twttr.txt.extractReplies(test.text);
- };
- case "urls":
- return function(test) {
- return twttr.txt.extractUrls(test.text);
- };
- case "hashtags":
- return function(test) {
- return twttr.txt.extractHashtags(test.text);
- };
- case "hashtags_with_indices":
- return function(test) {
- return twttr.txt.extractHashtagsWithIndices(test.text);
- };
- }
- case "hit_highlighting":
- return function(test) {
- return twttr.txt.hitHighlight(test.text, test.hits);
- };
- }
-
- }
-
-
- for (var suite in cases) {
- (function(suite) {
- module(suite);
-
- for (var section in cases[suite]) {
- (function(section) {
- var tester = getTester(suite, section);
- test(section, function() {
- for (var testCase in cases[suite][section]) {
- same(tester(cases[suite][section][testCase]), cases[suite][section][testCase].expected, cases[suite][section][testCase].description);
- }
- });
- }(section));
- }
- }(suite));
-
- }
- </script>
-
+ <script src="../test/tests.js"></script>
</head>
<body>
<h1 id="qunit-header">twitter-text-js</h1>
View
19 test/tests.js
@@ -0,0 +1,19 @@
+module("twttr.txt");
+
+test("twttr.txt.encode", function() {
+ var tests = [
+ ["&", "&amp;"],
+ [">", "&gt;"],
+ ["<", "&lt;"],
+ ["\"", "&quot;"],
+ ["&<>\"", "&amp;&lt;&gt;&quot;"],
+ ["<div>", "&lt;div&gt;"],
+ ["a&b", "a&amp;b"],
+ ["<a href=\"http://twitter.com\" target=\"_blank\">twitter & friends</a>", "&lt;a href=&quot;http://twitter.com&quot; target=&quot;_blank&quot;&gt;twitter &amp; friends&lt;/a&gt;"],
+ ["&amp;", "&amp;amp;"]
+ ];
+
+ for (var i = 0; i < tests.length; i++) {
+ same(twttr.txt.encode(tests[i][0]), tests[i][1], tests[i][0]);
+ }
+});
View
18 twitter-text.js
@@ -6,6 +6,20 @@ if (!window.twttr) {
twttr.txt = {};
twttr.txt.regexen = {};
+ var HTML_ENTITIES = {
+ '&': '&amp;',
+ '>': '&gt;',
+ '<': '&lt;',
+ '"': '&quot;'
+ };
+
+ // HTML escaping
+ twttr.txt.encode = function(text) {
+ return text.replace(/[&"><]/g, function(character) {
+ return HTML_ENTITIES[character];
+ });
+ };
+
// Builds a RegExp
function R(r, f) {
f = f || "";
@@ -32,10 +46,10 @@ if (!window.twttr) {
}), f);
}
- // simple string interpolation
+ // simple string interpolation w/html encoding
function S(s, d) {
return s.replace(/#\{(\w+)\}/g, function(m, name) {
- return d[name] || "";
+ return twttr.txt.encode(d[name] || "");
});
}

0 comments on commit 814b6fb

Please sign in to comment.
Something went wrong with that request. Please try again.