[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/MediaEmbed/Parser/ -> tagFilter.js (source)

   1  /**
   2  * @param {!Tag}    tag   The original tag
   3  * @param {!Object} hosts Map of [host => siteId]
   4  * @param {!Object} sites Map of [siteId => siteConfig]
   5  */
   6  function (tag, hosts, sites)
   7  {
   8      /**
   9      * Filter a MEDIA tag
  10      *
  11      * This will always invalidate the original tag, and possibly replace it with the tag that
  12      * corresponds to the media site
  13      *
  14      * @param {!Tag}    tag   The original tag
  15      * @param {!Object} hosts Map of [host => siteId]
  16      * @param {!Object} sites Map of [siteId => siteConfig]
  17      */
  18  	function filterTag(tag, hosts, sites)
  19      {
  20          // Always invalidate this tag
  21          tag.invalidate();
  22  
  23          if (tag.hasAttribute('url'))
  24          {
  25              var url    = tag.getAttribute('url'),
  26                  siteId = getSiteIdFromUrl(url, hosts);
  27              if (sites[siteId])
  28              {
  29                  var attributes = getAttributes(url, sites[siteId]);
  30                  if (!empty(attributes))
  31                  {
  32                      createTag(siteId.toUpperCase(), tag).setAttributes(attributes);
  33                  }
  34              }
  35          }
  36      }
  37  
  38      /**
  39      * Add named captures from a set of regular expressions to a set of attributes
  40      *
  41      * @param  {!Object} attributes Associative array of strings
  42      * @param  {string}  string     Text to match
  43      * @param  {!Array}  regexps    List of [regexp, map] pairs
  44      * @return {boolean}            Whether any regexp matched
  45      */
  46  	function addNamedCaptures(attributes, string, regexps)
  47      {
  48          var matched = false;
  49          regexps.forEach(function(pair)
  50          {
  51              var regexp = pair[0],
  52                  map    = pair[1],
  53                  m      = regexp.exec(string);
  54              if (!m)
  55              {
  56                  return;
  57              }
  58  
  59              matched = true;
  60              map.forEach(function(name, i)
  61              {
  62                  if (m[i] > '' && name > '')
  63                  {
  64                      attributes[name] = m[i];
  65                  }
  66              });
  67          });
  68  
  69          return matched;
  70      }
  71  
  72      /**
  73      * Create a tag for a media embed
  74      *
  75      * @param  {string} tagName  Tag's name
  76      * @param  {!Tag}   tag      Reference tag
  77      * @return {!Tag}            New tag
  78      */
  79  	function createTag(tagName, tag)
  80      {
  81          var startPos = tag.getPos(),
  82              endTag   = tag.getEndTag(),
  83              startLen,
  84              endPos,
  85              endLen;
  86          if (endTag)
  87          {
  88              startLen = tag.getLen();
  89              endPos   = endTag.getPos();
  90              endLen   = endTag.getLen();
  91          }
  92          else
  93          {
  94              startLen = 0;
  95              endPos   = tag.getPos() + tag.getLen();
  96              endLen   = 0;
  97          }
  98  
  99          return addTagPair(tagName, startPos, startLen, endPos, endLen, tag.getSortPriority());
 100      }
 101  
 102      /**
 103      * @param  {!Object} attributes
 104      * @return {boolean}
 105      */
 106  	function empty(attributes)
 107      {
 108          for (var attrName in attributes)
 109          {
 110              return false;
 111          }
 112  
 113          return true;
 114      }
 115  
 116      /**
 117      * Return a set of attributes for given URL based on a site's config
 118      *
 119      * @param  {string}  url    Original URL
 120      * @param  {!Object} config Site config
 121      * @return {!Object}        Attributes
 122      */
 123  	function getAttributes(url, config)
 124      {
 125          var attributes = {};
 126          addNamedCaptures(attributes, url, config[0]);
 127  
 128          return attributes;
 129      }
 130  
 131      /**
 132      * Return the siteId that corresponds to given URL
 133      *
 134      * @param  {string} url   Original URL
 135      * @param  {!Array} hosts Map of [hostname => siteId]
 136      * @return {string}       URL's siteId, or an empty string
 137      */
 138  	function getSiteIdFromUrl(url, hosts)
 139      {
 140          var m    = /^https?:\/\/([^\/]+)/.exec(url.toLowerCase()),
 141              host = m[1] || '';
 142          while (host > '')
 143          {
 144              if (hosts[host])
 145              {
 146                  return hosts[host];
 147              }
 148              host = host.replace(/^[^.]*./, '');
 149          }
 150  
 151          return '';
 152      }
 153  
 154      filterTag(tag, hosts, sites);
 155  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1