correct bbcode parsing edge case

This commit is contained in:
Sam 2017-07-20 13:02:31 -04:00
parent eb6c5a54ea
commit 1096dcd602
3 changed files with 15 additions and 12 deletions

View File

@ -11,7 +11,7 @@ function trailingSpaceOnly(src, start, max) {
return true;
}
const ATTR_REGEX = /(([a-z0-9]*)\s*=)/ig;
const ATTR_REGEX = /((([a-z0-9]*)\s*)=)(["'].*["']|\S+)/ig;
// parse a tag [test a=1 b=2] to a data structure
// {tag: "test", attrs={a: "1", b: "2"}
@ -76,20 +76,18 @@ export function parseBBCodeTag(src, start, max, multiline) {
let match, key, val;
while(match = ATTR_REGEX.exec(raw)) {
if (key) {
val = raw.slice(attrs[key],match.index) || '';
key = match[3];
if (key === '') {
key = '_default';
}
val = match[4];
if (val) {
val = val.trim();
val = val.replace(/^["'](.*)["']$/, '$1');
attrs[key] = val;
}
key = match[2] || '_default';
attrs[key] = match.index + match[0].length;
}
if (key) {
val = raw.slice(attrs[key]);
val = val.replace(/^["'](.*)["']$/, '$1');
attrs[key] = val;
}
}

View File

@ -167,7 +167,6 @@ export function setup(helper) {
ruler.push('url', {
tag: 'url',
wrap: function(startToken, endToken, tagInfo, content) {
const url = (tagInfo.attrs['_default'] || content).trim();
if (simpleUrlRegex.test(url)) {

View File

@ -879,6 +879,12 @@ HTML
expect(cooked).to eq(html)
end
it "supports query params in bbcode url" do
cooked = PrettyText.cook("[url=https://www.amazon.com/Camcorder-Hausbell-302S-Control-Infrared/dp/B01KLOA1PI/?tag=discourse]BBcode link[/url]")
html = '<p><a href="https://www.amazon.com/Camcorder-Hausbell-302S-Control-Infrared/dp/B01KLOA1PI/?tag=discourse" data-bbcode="true" rel="nofollow noopener">BBcode link</a></p>'
expect(cooked).to eq(html)
end
it "supports inline code bbcode" do
cooked = PrettyText.cook "Testing [code]codified **stuff** and `more` stuff[/code]"
html = "<p>Testing <code>codified **stuff** and `more` stuff</code></p>"