ComunicWeb/assets/js/components/textParser.js

147 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2018-05-03 20:05:06 +00:00
/**
* Comunic specific text parser
*
* @author Pierre HUBERT
*/
ComunicWeb.components.textParser = {
/**
* Parse an element that contains some user input
*
* @param {object} info Information about the element to parse
*/
parse: function(info){
2018-05-03 20:44:22 +00:00
//Add space at the begining and the end of the content to ensure
//parsing will not encounter errors
info.element.innerHTML = " " + info.element.innerHTML + " ";
//Prepare users tag parsing
this._prepare_user_tag_parsing(info.element);
2018-05-13 10:16:32 +00:00
//Prepare URL parsing
this._prepare_url_tag_parsing(info.element);
2018-05-03 20:05:06 +00:00
//Parse emojies
ComunicWeb.components.emoji.parser.parse({
element: info.element,
user: info.user
2018-05-03 20:05:06 +00:00
});
2018-05-03 20:44:22 +00:00
//Parse users tags
this._parse_users_tag(info.element);
2018-05-13 10:16:32 +00:00
//Parse URLs
this._parse_urls(info.element);
2018-05-03 20:44:22 +00:00
},
/**
* Prepare users tag parsing
*
* @param {HTMLElement} target The target element to prepare
*/
_prepare_user_tag_parsing: function(target){
//Find all occurences of users tag
2018-05-13 09:59:54 +00:00
while(target.innerHTML.match(/ @[a-zA-Z0-9.]+/i)){
2018-05-03 20:44:22 +00:00
//Get user tag
2018-05-13 09:59:54 +00:00
var userTag = target.innerHTML.match(/ @[a-zA-Z0-9.]+/i)[0];
var userID = userTag.replace(" @", "");
2018-05-03 20:44:22 +00:00
2018-05-14 15:04:21 +00:00
target.innerHTML = target.innerHTML.replace(userTag, " <userTag>"+userID+"</userTag>");
2018-05-03 20:44:22 +00:00
}
},
2018-05-13 10:16:32 +00:00
/**
* Prepare URL parsing
*
* @param {HTMLElement} target The target element to prepare
*/
_prepare_url_tag_parsing: function(target){
//Find all occurences of users tag
2018-07-02 05:39:04 +00:00
while(target.innerHTML.match(/ [a-zA-Z]{2,5}:\/\/[a-zA-Z0-9.=@\?\-\_\&\;:\/]+/i)){
2018-05-13 10:16:32 +00:00
//Get URL and save it
2018-07-02 05:39:04 +00:00
var URL = target.innerHTML.match(/ [a-zA-Z]{2,5}:\/\/[a-zA-Z0-9.=@\?\-\_\&\;:\/]+/i)[0];
2018-05-13 10:16:32 +00:00
tempURL = URL.replace("://", ":/");
target.innerHTML = target.innerHTML.replace(URL, "<innerURL>"+tempURL+"</innerURL>");
}
},
2018-05-03 20:44:22 +00:00
/**
* Parse users tag
*
* @param {HTMLElement} target The target element where user tags has
* to be parsed
*/
_parse_users_tag: function(target){
//Get the list of user tags of the target
var nodes = target.getElementsByTagName("userTag");
2018-05-08 10:39:16 +00:00
for (var num in nodes) {
2018-05-03 20:44:22 +00:00
if (nodes.hasOwnProperty(num)) {
const node = nodes[num];
//Get target user ID
const userID = node.innerHTML;
//Adapt node content
node.innerHTML = "@" + userID;
node.className = "a";
2018-05-09 10:05:11 +00:00
//Small fix for iOS 9
if(!node.addEventListener)
continue;
2018-05-03 20:44:22 +00:00
//Set event listener
node.addEventListener("click", function(ev){
//Open user page
2018-07-14 12:18:21 +00:00
openPage(userID);
2018-05-03 20:44:22 +00:00
});
}
}
},
2018-05-03 20:05:06 +00:00
2018-05-13 10:16:32 +00:00
/**
* 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>";
}
}
},
2018-05-03 20:05:06 +00:00
}