[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Parser/ -> FilterProcessing.js (source)

   1  /**
   2  * Execute all the attribute preprocessors of given tag
   3  *
   4  * @private
   5  *
   6  * @param {!Tag}    tag       Source tag
   7  * @param {!Object} tagConfig Tag's config
   8  */
   9  function executeAttributePreprocessors(tag, tagConfig)
  10  {
  11      if (typeof tagConfig.attributePreprocessors === 'undefined')
  12      {
  13          return;
  14      }
  15  
  16      tagConfig.attributePreprocessors.forEach(function(ap)
  17      {
  18          var attrName = ap[0], regexp = ap[1], map = ap[2];
  19          if (tag.hasAttribute(attrName))
  20          {
  21              executeAttributePreprocessor(tag, attrName, regexp, map);
  22          }
  23      });
  24  }
  25  
  26  /**
  27  * Filter the attributes of given tag
  28  *
  29  * @private
  30  *
  31  * @param {!Tag}    tag            Tag being checked
  32  * @param {!Object} tagConfig      Tag's config
  33  * @param {!Object} registeredVars Unused
  34  * @param {!Logger} logger         This parser's Logger instance
  35  */
  36  function filterAttributes(tag, tagConfig, registeredVars, logger)
  37  {
  38      var attributes = {}, attrName;
  39      for (attrName in tagConfig.attributes)
  40      {
  41          var attrConfig = tagConfig.attributes[attrName],
  42              attrValue  = false;
  43          if (tag.hasAttribute(attrName))
  44          {
  45              attrValue = executeAttributeFilterChain(attrConfig.filterChain, attrName, tag.getAttribute(attrName));
  46          }
  47  
  48          if (attrValue !== false)
  49          {
  50              attributes[attrName] = attrValue;
  51          }
  52          else if (HINT.attributeDefaultValue && typeof attrConfig.defaultValue !== 'undefined')
  53          {
  54              attributes[attrName] = attrConfig.defaultValue;
  55          }
  56          else if (attrConfig.required)
  57          {
  58              tag.invalidate();
  59          }
  60      }
  61      tag.setAttributes(attributes);
  62  }
  63  
  64  /**
  65  * Execute a tag's filterChain
  66  *
  67  * @private
  68  *
  69  * @param {!Tag} tag Tag to filter
  70  */
  71  function filterTag(tag)
  72  {
  73      var tagName   = tag.getName(),
  74          tagConfig = tagsConfig[tagName];
  75  
  76      // Record the tag being processed into the logger it can be added to the context of
  77      // messages logged during the execution
  78      logger.setTag(tag);
  79  
  80      for (var i = 0; i < tagConfig.filterChain.length; ++i)
  81      {
  82          if (tag.isInvalid())
  83          {
  84              break;
  85          }
  86          tagConfig.filterChain[i](tag, tagConfig);
  87      }
  88  
  89      // Remove the tag from the logger
  90      logger.unsetTag();
  91  }
  92  
  93  /**
  94  * Execute an attribute's filterChain
  95  *
  96  * @param  {!Array} filterChain Attribute's filterChain
  97  * @param  {string} attrName    Attribute's name
  98  * @param  {*}      attrValue   Original value
  99  * @return {*}                  Filtered value
 100  */
 101  function executeAttributeFilterChain(filterChain, attrName, attrValue)
 102  {
 103      logger.setAttribute(attrName);
 104      for (var i = 0; i < filterChain.length; ++i)
 105      {
 106          // NOTE: attrValue is intentionally set as the first argument to facilitate inlining
 107          attrValue = filterChain[i](attrValue, attrName);
 108          if (attrValue === false)
 109          {
 110              break;
 111          }
 112      }
 113      logger.unsetAttribute();
 114  
 115      return attrValue;
 116  }
 117  
 118  /**
 119  * Execute an attribute preprocessor
 120  *
 121  * @param  {!Tag}           tag
 122  * @param  {string}         attrName
 123  * @param  {!RegExp}        regexp
 124  * @param  {!Array<string>} map
 125  */
 126  function executeAttributePreprocessor(tag, attrName, regexp, map)
 127  {
 128      var attrValue = tag.getAttribute(attrName),
 129          captures  = getNamedCaptures(attrValue, regexp, map),
 130          k;
 131      for (k in captures)
 132      {
 133          // Attribute preprocessors cannot overwrite other attributes but they can
 134          // overwrite themselves
 135          if (k === attrName || !tag.hasAttribute(k))
 136          {
 137              tag.setAttribute(k, captures[k]);
 138          }
 139      }
 140  }
 141  
 142  /**
 143  * Execute a regexp and return the values of the mapped captures
 144  *
 145  * @param  {string}                 attrValue
 146  * @param  {!RegExp}                regexp
 147  * @param  {!Array<string>}         map
 148  * @return {!Object<string,string>}
 149  */
 150  function getNamedCaptures(attrValue, regexp, map)
 151  {
 152      var m = regexp.exec(attrValue);
 153      if (!m)
 154      {
 155          return [];
 156      }
 157  
 158      var values = {};
 159      map.forEach(function(k, i)
 160      {
 161          if (typeof m[i] === 'string' && m[i] !== '')
 162          {
 163              values[k] = m[i];
 164          }
 165      });
 166  
 167      return values;
 168  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1