Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed attribute issue in shortcode #2406

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixed attribute issue in shortcode
iam-pranav committed Nov 28, 2023
commit 0d02fffe5f1f7d187e3144b990a2308d210861d4
69 changes: 39 additions & 30 deletions js/qsm-quiz.js
Original file line number Diff line number Diff line change
@@ -1484,41 +1484,50 @@ jQuery(function () {
});
});

function qsm_check_shortcode(message = null) {
const videoAttributePatterns = [
/\ssrc="([^"]+)"/,
/\smp4="([^"]+)"/,
/\sm4v="([^"]+)"/,
/\swebm="([^"]+)"/,
/\sogv="([^"]+)"/,
/\swmv="([^"]+)"/,
/\sflv="([^"]+)"/,
/\swidth="(\d+)"/,
/\sheight="(\d+)"/
];

const videoAttributePatterns = [
/\ssrc="([^"]+)"/,
/\smp4="([^"]+)"/,
/\sm4v="([^"]+)"/,
/\swebm="([^"]+)"/,
/\sogv="([^"]+)"/,
/\swmv="([^"]+)"/,
/\sflv="([^"]+)"/,
/\swidth="(\d+)"/,
/\sheight="(\d+)"/
];

function parseAttributes(match, src, width, height) {
let videoAttrs = { src: '', width: '', height: '' };

videoAttributePatterns.forEach(pattern => {
const attrMatch = match.match(pattern);
if (attrMatch) {
const value = attrMatch[1] || '';
if (pattern.toString().includes('width')) {
videoAttrs.width = value;
} else if (pattern.toString().includes('height')) {
videoAttrs.height = value;
} else {
videoAttrs.src = value;
}
}
});

return videoAttrs;
}

function generateVideoTag(src, width, height, content) {
return `<video src="${src}" width="${width}" height="${height}" controls>${content}</video>`;
}

function qsm_check_shortcode(message = null) {
const videoContentRegex = /\[video(?:\s(?:src|mp4|m4v|webm|ogv|wmv|flv|width|height)="[^"]*")*\](.*?)\[\/video\]/g;
let videoMatch = message.match(videoContentRegex);

if (videoMatch) {
let videoHTML = message.replace(videoContentRegex, function(match, content) {
let src = '';
let width = '';
let height = '';

videoAttributePatterns.forEach(pattern => {
const attrMatch = match.match(pattern);
if (attrMatch) {
if (pattern.toString().includes('width')) {
width = attrMatch[1] || '';
} else if (pattern.toString().includes('height')) {
height = attrMatch[1] || '';
} else {
src = attrMatch[1] || '';
}
}
});
const videoTag = `<video src="${src}" width="${width}" height="${height}" controls>${content}</video>`;
const { src, width, height } = parseAttributes(match);
const videoTag = generateVideoTag(src, width, height, content);
return `<div class="video-content">${videoTag}</div>`;
});
return videoHTML;