[ Index ]

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


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1