2016-06-14 14:31:51 -04:00
|
|
|
import { sanitize } from 'pretty-text/sanitizer';
|
|
|
|
import { default as PrettyText, buildOptions } from 'pretty-text/pretty-text';
|
|
|
|
import { hashString } from 'discourse/lib/hash';
|
|
|
|
|
|
|
|
// Run the MDTest spec
|
|
|
|
module("MDTest");
|
2013-09-11 15:52:37 -04:00
|
|
|
|
|
|
|
// This is cheating, but the trivial differences between sanitization
|
2015-11-12 12:02:13 -05:00
|
|
|
// do not affect formatting.
|
2013-09-11 15:52:37 -04:00
|
|
|
function normalize(str) {
|
|
|
|
return str.replace(/\n\s*/g, '').
|
2014-07-03 16:54:56 -04:00
|
|
|
replace(/ \/\>/g, '>').
|
2013-09-11 15:52:37 -04:00
|
|
|
replace(/ ?/g, "\t").
|
|
|
|
replace(/"/g, '"');
|
|
|
|
}
|
|
|
|
|
2014-07-03 16:54:56 -04:00
|
|
|
// We use a custom sanitizer for MD test that hoists out comments. In Discourse
|
|
|
|
// they are stripped, but to be compliant with the spec they should not be.
|
2016-06-14 14:31:51 -04:00
|
|
|
function sanitizer(result, whiteLister) {
|
|
|
|
let hoisted;
|
|
|
|
const m = result.match(/<!--[\s\S]*?-->/g);
|
2014-07-03 16:54:56 -04:00
|
|
|
if (m && m.length) {
|
|
|
|
hoisted = [];
|
2016-06-14 14:31:51 -04:00
|
|
|
for (let i=0; i<m.length; i++) {
|
|
|
|
const c = m[i];
|
|
|
|
const id = hashString("discourse:hoisted-comment:" + i).toString();
|
2014-07-03 16:54:56 -04:00
|
|
|
result = result.replace(c, id);
|
|
|
|
hoisted.push([c, id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
result = sanitize(result, whiteLister);
|
2014-07-03 16:54:56 -04:00
|
|
|
if (hoisted) {
|
2016-06-14 14:31:51 -04:00
|
|
|
hoisted.forEach(tuple => result = result.replace(tuple[1], tuple[0]));
|
2014-07-03 16:54:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-21 11:39:48 -04:00
|
|
|
function md(input, expected, text, settings) {
|
2016-06-14 14:31:51 -04:00
|
|
|
|
2016-10-21 11:39:48 -04:00
|
|
|
const opts = buildOptions({ siteSettings: settings||{} });
|
2016-06-14 14:31:51 -04:00
|
|
|
opts.traditionalMarkdownLinebreaks = true;
|
|
|
|
opts.sanitizer = sanitizer;
|
|
|
|
|
|
|
|
const cooker = new PrettyText(opts);
|
|
|
|
const result = cooker.cook(input);
|
|
|
|
const resultNorm = normalize(result);
|
|
|
|
const expectedNorm = normalize(expected);
|
|
|
|
const same = (result === expected) || (resultNorm === expectedNorm);
|
2013-09-11 15:52:37 -04:00
|
|
|
|
|
|
|
if (same) {
|
|
|
|
ok(same, text);
|
|
|
|
} else {
|
|
|
|
equal(resultNorm, expectedNorm, text);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
test("first", function(){
|
|
|
|
equal(1, 1, "cool")
|
|
|
|
});
|
|
|
|
|
|
|
|
<%
|
|
|
|
def mdtest_suite
|
|
|
|
result = ""
|
|
|
|
Dir.glob("#{Rails.root}/test/javascripts/mdtest/fixtures/*.text").each do |f|
|
|
|
|
|
|
|
|
filename_no_ext = f.sub(/\.text$/, '')
|
|
|
|
filename = Pathname.new(filename_no_ext)
|
|
|
|
|
|
|
|
text = File.read(f)
|
2013-10-18 15:20:27 -04:00
|
|
|
html = File.read("#{filename_no_ext}.xhtml");
|
2013-09-11 15:52:37 -04:00
|
|
|
result << "test(\"#{filename}\", function() { md(#{text.to_json}, #{html.to_json}, 'passes MDTest'); });\n"
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
%>
|
|
|
|
|
2014-02-11 18:00:53 -05:00
|
|
|
<%= mdtest_suite %>
|
2016-10-21 11:39:48 -04:00
|
|
|
|
|
|
|
test("whitelisted url scheme", function() {
|
|
|
|
md("[Steam URL Scheme](steam://store/452530)", '<p><a href="steam://store/452530">Steam URL Scheme</a></p>', 'whitelists the steam url', {allowed_href_schemes: "macappstore|steam"});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("forbidden url scheme", function() {
|
|
|
|
md("[Steam URL Scheme](steam://store/452530)", '<p><a>Steam URL Scheme</a></p>', 'removes the href', {allowed_href_schemes: "macappstore|itunes"});
|
|
|
|
});
|