URLs can be parsed

This commit is contained in:
Pierre 2018-05-13 12:16:32 +02:00
parent 7a3387b550
commit 1a922704ed

View File

@ -20,6 +20,9 @@ ComunicWeb.components.textParser = {
//Prepare users tag parsing //Prepare users tag parsing
this._prepare_user_tag_parsing(info.element); this._prepare_user_tag_parsing(info.element);
//Prepare URL parsing
this._prepare_url_tag_parsing(info.element);
//Parse emojies //Parse emojies
ComunicWeb.components.emoji.parser.parse({ ComunicWeb.components.emoji.parser.parse({
element: info.element element: info.element
@ -27,6 +30,9 @@ ComunicWeb.components.textParser = {
//Parse users tags //Parse users tags
this._parse_users_tag(info.element); this._parse_users_tag(info.element);
//Parse URLs
this._parse_urls(info.element);
}, },
/** /**
@ -49,6 +55,25 @@ ComunicWeb.components.textParser = {
}, },
/**
* Prepare URL parsing
*
* @param {HTMLElement} target The target element to prepare
*/
_prepare_url_tag_parsing: function(target){
//Find all occurences of users tag
while(target.innerHTML.match(/ [a-zA-Z]{2,5}:\/\/[a-zA-Z0-9.=@\?\&:\/]+/i)){
//Get URL and save it
var URL = target.innerHTML.match(/ [a-zA-Z]{2,5}:\/\/[a-zA-Z0-9.=@\?\&:\/]+/i)[0];
tempURL = URL.replace("://", ":/");
target.innerHTML = target.innerHTML.replace(URL, "<innerURL>"+tempURL+"</innerURL>");
}
},
/** /**
* Parse users tag * Parse users tag
* *
@ -87,5 +112,35 @@ ComunicWeb.components.textParser = {
}, },
/**
* Parse URLs
*
* @param {HTMLElement} target The target element where URLs
* to be parsed
*/
_parse_urls: function(target){
//Get the list of user tags of the target
var nodes = target.getElementsByTagName("innerURL");
for (var num in nodes) {
if (nodes.hasOwnProperty(num)) {
const node = nodes[num];
//Small fix for iOS 9
if(!node.addEventListener)
continue;
//Get target URL
var url = node.innerHTML;
url = url.replace(":/", "://");
//Adapt node
node.innerHTML = "<a href='" + url + "' target='_blank'>" + url + "</a>";
}
}
},
} }