idx
int64
0
60.3k
question
stringlengths
64
4.24k
target
stringlengths
5
618
44,100
public function exists ( $ key ) { $ key = $ this -> normalizeKey ( $ key ) ; return array_key_exists ( $ key , $ this -> items ) ; }
Test whether an item of given key exists
44,101
public function set ( $ key , $ value ) { $ key = $ this -> normalizeKey ( $ key ) ; $ this -> items [ $ key ] = $ this -> normalizeValue ( $ value ) ; return $ this -> items [ $ key ] ; }
Set and overwrite a value in this collection
44,102
public function deduplicate ( ) { if ( $ this -> useCount > 1 ) { $ this -> isDeduplicated = true ; $ this -> decrementUseCount ( $ this -> useCount - 1 ) ; } }
Mark this value as deduplicated if it s been used more than once
44,103
protected function decrementUseCount ( $ step = 1 ) { $ this -> useCount -= $ step ; foreach ( $ this -> value as $ value ) { if ( $ value instanceof ConfigValue ) { $ value -> decrementUseCount ( $ step ) ; } } }
Decrement the use counter of this value as well as the values it contains
44,104
protected function getSource ( $ expr ) { $ php = 'switch(' . $ expr . '){' ; foreach ( $ this -> getValuesPerCodeBranch ( ) as $ branchCode => $ values ) { foreach ( $ values as $ value ) { $ php .= 'case' . var_export ( ( string ) $ value , true ) . ':' ; } $ php .= $ branchCode . 'break;' ; } if ( $ this -> defaultCode > '' ) { $ php .= 'default:' . $ this -> defaultCode ; } $ php = preg_replace ( '(break;$)' , '' , $ php ) . '}' ; return $ php ; }
Return the source code for this switch statement
44,105
protected function getValuesPerCodeBranch ( ) { $ values = [ ] ; foreach ( $ this -> branchesCode as $ value => $ branchCode ) { $ values [ $ branchCode ] [ ] = $ value ; } return $ values ; }
Group branches by their content and return the switch values for each branch
44,106
public static function filterTag ( Tag $ tag , TagStack $ tagStack , array $ hosts , array $ sites , $ cacheDir ) { $ tag -> invalidate ( ) ; if ( $ tag -> hasAttribute ( 'url' ) ) { $ url = $ tag -> getAttribute ( 'url' ) ; $ siteId = self :: getSiteIdFromUrl ( $ url , $ hosts ) ; if ( isset ( $ sites [ $ siteId ] ) ) { $ attributes = self :: getAttributes ( $ url , $ sites [ $ siteId ] , $ cacheDir ) ; if ( ! empty ( $ attributes ) ) { self :: createTag ( strtoupper ( $ siteId ) , $ tagStack , $ tag ) -> setAttributes ( $ attributes ) ; } } } }
Filter a MEDIA tag
44,107
protected static function addNamedCaptures ( array & $ attributes , $ string , array $ regexps ) { $ matched = 0 ; foreach ( $ regexps as list ( $ regexp , $ map ) ) { $ matched += preg_match ( $ regexp , $ string , $ m ) ; foreach ( $ map as $ i => $ name ) { if ( isset ( $ m [ $ i ] ) && $ m [ $ i ] !== '' && $ name !== '' ) { $ attributes [ $ name ] = $ m [ $ i ] ; } } } return ( bool ) $ matched ; }
Add named captures from a set of regular expressions to a set of attributes
44,108
protected static function createTag ( $ tagName , TagStack $ tagStack , Tag $ tag ) { $ startPos = $ tag -> getPos ( ) ; $ endTag = $ tag -> getEndTag ( ) ; if ( $ endTag ) { $ startLen = $ tag -> getLen ( ) ; $ endPos = $ endTag -> getPos ( ) ; $ endLen = $ endTag -> getLen ( ) ; } else { $ startLen = 0 ; $ endPos = $ tag -> getPos ( ) + $ tag -> getLen ( ) ; $ endLen = 0 ; } return $ tagStack -> addTagPair ( $ tagName , $ startPos , $ startLen , $ endPos , $ endLen , $ tag -> getSortPriority ( ) ) ; }
Create a tag for a media embed
44,109
protected static function getAttributes ( $ url , array $ config , $ cacheDir ) { $ attributes = [ ] ; self :: addNamedCaptures ( $ attributes , $ url , $ config [ 0 ] ) ; foreach ( $ config [ 1 ] as $ scrapeConfig ) { self :: scrape ( $ attributes , $ url , $ scrapeConfig , $ cacheDir ) ; } return $ attributes ; }
Return a set of attributes for given URL based on a site s config
44,110
protected static function getHttpClient ( $ cacheDir ) { if ( ! isset ( self :: $ client ) || self :: $ clientCacheDir !== $ cacheDir ) { self :: $ client = ( isset ( $ cacheDir ) ) ? Http :: getCachingClient ( $ cacheDir ) : Http :: getClient ( ) ; self :: $ clientCacheDir = $ cacheDir ; } return self :: $ client ; }
Return a cached instance of the HTTP client
44,111
protected static function getSiteIdFromUrl ( $ url , array $ hosts ) { $ host = ( preg_match ( '(^https?://([^/]+))' , strtolower ( $ url ) , $ m ) ) ? $ m [ 1 ] : '' ; while ( $ host > '' ) { if ( isset ( $ hosts [ $ host ] ) ) { return $ hosts [ $ host ] ; } $ host = preg_replace ( '(^[^.]*.)' , '' , $ host ) ; } return '' ; }
Return the siteId that corresponds to given URL
44,112
protected static function scrape ( array & $ attributes , $ url , array $ config , $ cacheDir ) { $ vars = [ ] ; if ( self :: addNamedCaptures ( $ vars , $ url , $ config [ 'match' ] ) ) { if ( isset ( $ config [ 'url' ] ) ) { $ url = self :: interpolateVars ( $ config [ 'url' ] , $ vars + $ attributes ) ; } if ( preg_match ( '(^https?://[^#]+)i' , $ url , $ m ) ) { $ response = self :: wget ( $ m [ 0 ] , $ cacheDir , $ config ) ; self :: addNamedCaptures ( $ attributes , $ response , $ config [ 'extract' ] ) ; } } }
Scrape values and add them to current attributes
44,113
protected static function wget ( $ url , $ cacheDir , $ config ) { $ options = [ 'headers' => ( isset ( $ config [ 'header' ] ) ) ? ( array ) $ config [ 'header' ] : [ ] ] ; return @ self :: getHttpClient ( $ cacheDir ) -> get ( $ url , $ options ) ; }
Retrieve external content
44,114
public function convert ( $ expr ) { $ match = $ this -> getMatch ( $ expr ) ; if ( ! isset ( $ match ) ) { throw new RuntimeException ( "Cannot convert '" . $ expr . "'" ) ; } list ( $ name , $ args ) = $ match ; return call_user_func_array ( $ this -> callbacks [ $ name ] , $ args ) ; }
Convert given XPath expression to PHP
44,115
public function setConvertors ( array $ convertors ) { $ this -> callbacks = [ ] ; $ this -> matchGroup = [ ] ; $ this -> groups = [ ] ; $ this -> regexps = [ ] ; foreach ( $ convertors as $ convertor ) { $ this -> addConvertor ( $ convertor ) ; } $ this -> sortRegexps ( ) ; foreach ( $ this -> groups as $ group => $ captures ) { sort ( $ captures ) ; $ this -> regexps [ $ group ] = '(?<' . $ group . '>' . implode ( '|' , $ captures ) . ')' ; } $ this -> regexp = '(^(?:' . implode ( '|' , $ this -> regexps ) . ')$)' ; }
Set the list of convertors used by this instance
44,116
protected function addConvertor ( AbstractConvertor $ convertor ) { foreach ( $ convertor -> getRegexpGroups ( ) as $ name => $ group ) { $ this -> matchGroup [ $ name ] = $ group ; $ this -> groups [ $ group ] [ ] = '(?&' . $ name . ')' ; } foreach ( $ convertor -> getRegexps ( ) as $ name => $ regexp ) { $ regexp = $ this -> insertCaptureNames ( $ name , $ regexp ) ; $ regexp = str_replace ( ' ' , '\\s*' , $ regexp ) ; $ regexp = '(?<' . $ name . '>' . $ regexp . ')' ; $ this -> callbacks [ $ name ] = [ $ convertor , 'convert' . $ name ] ; $ this -> regexps [ $ name ] = $ regexp ; } }
Add a convertor to the list used by this instance
44,117
protected function getArguments ( array $ matches , $ name ) { $ args = [ ] ; $ i = 0 ; while ( isset ( $ matches [ $ name . $ i ] ) ) { $ args [ ] = $ matches [ $ name . $ i ] ; ++ $ i ; } return $ args ; }
Get the list of arguments produced by a regexp s match
44,118
protected function getDefaultConvertors ( ) { $ convertors = [ ] ; $ convertors [ ] = new BooleanFunctions ( $ this ) ; $ convertors [ ] = new BooleanOperators ( $ this ) ; $ convertors [ ] = new Comparisons ( $ this ) ; $ convertors [ ] = new Core ( $ this ) ; $ convertors [ ] = new Math ( $ this ) ; if ( extension_loaded ( 'mbstring' ) ) { $ convertors [ ] = new MultiByteStringManipulation ( $ this ) ; } $ convertors [ ] = new SingleByteStringFunctions ( $ this ) ; $ convertors [ ] = new SingleByteStringManipulation ( $ this ) ; return $ convertors ; }
Return the default list of convertors
44,119
protected function getMatch ( $ expr ) { if ( preg_match ( $ this -> regexp , $ expr , $ m ) ) { foreach ( $ m as $ name => $ match ) { if ( $ match !== '' && isset ( $ this -> callbacks [ $ name ] ) ) { return [ $ name , $ this -> getArguments ( $ m , $ name ) ] ; } } } return null ; }
Get the match generated by this instance s regexp on given XPath expression
44,120
protected function insertCaptureNames ( $ name , $ regexp ) { $ i = 0 ; return preg_replace_callback ( '((?<!\\\\)\\((?!\\?))' , function ( $ m ) use ( & $ i , $ name ) { return '(?<' . $ name . $ i ++ . '>' ; } , $ regexp ) ; }
Insert capture names into given regexp
44,121
protected function sortRegexps ( ) { uasort ( $ this -> regexps , function ( $ a , $ b ) { return strlen ( $ b ) - strlen ( $ a ) ; } ) ; }
Sort regexps by length
44,122
public function getTemplates ( ) { $ templates = [ 'br' => '<br/>' , 'e' => '' , 'i' => '' , 'p' => '<p><xsl:apply-templates/></p>' , 's' => '' ] ; foreach ( $ this -> configurator -> tags as $ tagName => $ tag ) { if ( isset ( $ tag -> template ) ) { $ templates [ $ tagName ] = ( string ) $ tag -> template ; } } ksort ( $ templates ) ; return $ templates ; }
Get the templates defined in all the targs
44,123
public function setEngine ( $ engine ) { if ( ! ( $ engine instanceof RendererGenerator ) ) { $ className = 's9e\\TextFormatter\\Configurator\\RendererGenerators\\' . $ engine ; $ reflection = new ReflectionClass ( $ className ) ; $ engine = $ reflection -> newInstanceArgs ( array_slice ( func_get_args ( ) , 1 ) ) ; } $ this -> engine = $ engine ; return $ engine ; }
Set the RendererGenerator instance used
44,124
public function asDOM ( ) { $ xml = '<xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform">' . $ this -> __toString ( ) . '</xsl:template>' ; $ dom = new TemplateDocument ( $ this ) ; $ dom -> loadXML ( $ xml ) ; return $ dom ; }
Return the content of this template as a DOMDocument
44,125
public function getInspector ( ) { if ( ! isset ( $ this -> inspector ) ) { $ this -> inspector = new TemplateInspector ( $ this -> __toString ( ) ) ; } return $ this -> inspector ; }
Return an instance of TemplateInspector based on this template s content
44,126
public function normalize ( TemplateNormalizer $ templateNormalizer ) { $ this -> inspector = null ; $ this -> template = $ templateNormalizer -> normalizeTemplate ( $ this -> template ) ; $ this -> isNormalized = true ; }
Normalize this template s content
44,127
public function replaceTokens ( $ regexp , $ fn ) { $ this -> inspector = null ; $ this -> template = TemplateModifier :: replaceTokens ( $ this -> template , $ regexp , $ fn ) ; $ this -> isNormalized = false ; }
Replace parts of this template that match given regexp
44,128
public function setContent ( $ template ) { $ this -> inspector = null ; $ this -> template = ( string ) $ template ; $ this -> isNormalized = false ; }
Replace this template s content
44,129
protected function addRelAttribute ( DOMElement $ element ) { $ rel = $ element -> getAttribute ( 'rel' ) ; if ( preg_match ( '(\\S$)' , $ rel ) ) { $ rel .= ' ' ; } $ rel .= 'noreferrer' ; $ element -> setAttribute ( 'rel' , $ rel ) ; }
Add a rel = noreferrer attribute to given element
44,130
protected function linkTargetCanAccessOpener ( DOMElement $ element ) { if ( ! $ element -> hasAttribute ( 'target' ) ) { return false ; } if ( preg_match ( '(\\bno(?:open|referr)er\\b)' , $ element -> getAttribute ( 'rel' ) ) ) { return false ; } return true ; }
Test whether given link element will let the target access window . opener
44,131
protected function addTag ( $ tagPos , $ tagLen , $ chr , $ prio = 0 ) { $ tag = $ this -> parser -> addSelfClosingTag ( $ this -> config [ 'tagName' ] , $ tagPos , $ tagLen , $ prio ) ; $ tag -> setAttribute ( $ this -> config [ 'attrName' ] , $ chr ) ; return $ tag ; }
Add a fancy replacement tag
44,132
protected function parseDashesAndEllipses ( ) { if ( strpos ( $ this -> text , '...' ) === false && strpos ( $ this -> text , '--' ) === false ) { return ; } $ chrs = [ '--' => "\xE2\x80\x93" , '---' => "\xE2\x80\x94" , '...' => "\xE2\x80\xA6" ] ; $ regexp = '/---?|\\.\\.\\./S' ; preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { $ this -> addTag ( $ m [ 1 ] , strlen ( $ m [ 0 ] ) , $ chrs [ $ m [ 0 ] ] ) ; } }
Parse dashes and ellipses
44,133
protected function parseFractions ( ) { if ( strpos ( $ this -> text , '/' ) === false ) { return ; } $ map = [ '1/4' => "\xC2\xBC" , '1/2' => "\xC2\xBD" , '3/4' => "\xC2\xBE" , '1/7' => "\xE2\x85\x90" , '1/9' => "\xE2\x85\x91" , '1/10' => "\xE2\x85\x92" , '1/3' => "\xE2\x85\x93" , '2/3' => "\xE2\x85\x94" , '1/5' => "\xE2\x85\x95" , '2/5' => "\xE2\x85\x96" , '3/5' => "\xE2\x85\x97" , '4/5' => "\xE2\x85\x98" , '1/6' => "\xE2\x85\x99" , '5/6' => "\xE2\x85\x9A" , '1/8' => "\xE2\x85\x9B" , '3/8' => "\xE2\x85\x9C" , '5/8' => "\xE2\x85\x9D" , '7/8' => "\xE2\x85\x9E" , '0/3' => "\xE2\x86\x89" ] ; $ regexp = '/\\b(?:0\\/3|1\\/(?:[2-9]|10)|2\\/[35]|3\\/[458]|4\\/5|5\\/[68]|7\\/8)\\b/S' ; preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { $ this -> addTag ( $ m [ 1 ] , strlen ( $ m [ 0 ] ) , $ map [ $ m [ 0 ] ] ) ; } }
Parse vulgar fractions
44,134
protected function parseGuillemets ( ) { if ( strpos ( $ this -> text , '<<' ) === false ) { return ; } $ regexp = '/<<( ?)(?! )[^\\n<>]*?[^\\n <>]\\1>>(?!>)/' ; preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { $ left = $ this -> addTag ( $ m [ 1 ] , 2 , "\xC2\xAB" ) ; $ right = $ this -> addTag ( $ m [ 1 ] + strlen ( $ m [ 0 ] ) - 2 , 2 , "\xC2\xBB" ) ; $ left -> cascadeInvalidationTo ( $ right ) ; } }
Parse guillemets - style quotation marks
44,135
protected function parseQuotePairs ( $ regexp , $ leftQuote , $ rightQuote ) { preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { $ left = $ this -> addTag ( $ m [ 1 ] , 1 , $ leftQuote ) ; $ right = $ this -> addTag ( $ m [ 1 ] + strlen ( $ m [ 0 ] ) - 1 , 1 , $ rightQuote ) ; $ left -> cascadeInvalidationTo ( $ right ) ; } }
Parse pairs of quotes
44,136
protected function parseSingleQuotes ( ) { if ( ! $ this -> hasSingleQuote ) { return ; } $ regexp = "/(?<=\\pL)'|(?<!\\S)'(?=\\pL|[0-9]{2})/uS" ; preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { $ this -> addTag ( $ m [ 1 ] , 1 , "\xE2\x80\x99" , 10 ) ; } }
Parse single quotes in general
44,137
protected function parseSymbolsAfterDigits ( ) { if ( ! $ this -> hasSingleQuote && ! $ this -> hasDoubleQuote && strpos ( $ this -> text , 'x' ) === false ) { return ; } $ map = [ "'s" => "\xE2\x80\x99" , "'" => "\xE2\x80\xB2" , "' " => "\xE2\x80\xB2" , "'x" => "\xE2\x80\xB2" , '"' => "\xE2\x80\xB3" , '" ' => "\xE2\x80\xB3" , '"x' => "\xE2\x80\xB3" ] ; $ regexp = "/[0-9](?>'s|[\"']? ?x(?= ?[0-9])|[\"'])/S" ; preg_match_all ( $ regexp , $ this -> text , $ matches , PREG_OFFSET_CAPTURE ) ; foreach ( $ matches [ 0 ] as $ m ) { if ( substr ( $ m [ 0 ] , - 1 ) === 'x' ) { $ this -> addTag ( $ m [ 1 ] + strlen ( $ m [ 0 ] ) - 1 , 1 , "\xC3\x97" ) ; } $ str = substr ( $ m [ 0 ] , 1 , 2 ) ; if ( isset ( $ map [ $ str ] ) ) { $ this -> addTag ( $ m [ 1 ] + 1 , 1 , $ map [ $ str ] ) ; } } }
Parse symbols found after digits
44,138
public function check ( DOMElement $ template , Tag $ tag ) { $ node = $ template -> getElementsByTagNameNS ( $ this -> namespaceURI , $ this -> elName ) -> item ( 0 ) ; if ( $ node ) { throw new UnsafeTemplateException ( "Element '" . $ node -> nodeName . "' is disallowed" , $ node ) ; } }
Test for the presence of an element of given name in given namespace
44,139
protected function exportXPath ( $ expr ) { $ phpTokens = [ ] ; foreach ( $ this -> tokenizeXPathForExport ( $ expr ) as list ( $ type , $ content ) ) { $ methodName = 'exportXPath' . ucfirst ( $ type ) ; $ phpTokens [ ] = $ this -> $ methodName ( $ content ) ; } return implode ( '.' , $ phpTokens ) ; }
Export an XPath expression as PHP with special consideration for XPath variables
44,140
protected function matchXPathForExport ( $ expr ) { $ tokenExprs = [ '(?<current>\\bcurrent\\(\\))' , '(?<param>\\$\\w+)' , '(?<fragment>"[^"]*"|\'[^\']*\'|.)' ] ; preg_match_all ( '(' . implode ( '|' , $ tokenExprs ) . ')s' , $ expr , $ matches , PREG_SET_ORDER ) ; $ i = count ( $ matches ) ; while ( -- $ i > 0 ) { if ( isset ( $ matches [ $ i ] [ 'fragment' ] , $ matches [ $ i - 1 ] [ 'fragment' ] ) ) { $ matches [ $ i - 1 ] [ 'fragment' ] .= $ matches [ $ i ] [ 'fragment' ] ; unset ( $ matches [ $ i ] ) ; } } return array_values ( $ matches ) ; }
Match the relevant components of an XPath expression
44,141
protected function tokenizeXPathForExport ( $ expr ) { $ tokens = [ ] ; foreach ( $ this -> matchXPathForExport ( $ expr ) as $ match ) { foreach ( array_reverse ( $ match ) as $ k => $ v ) { if ( ! is_numeric ( $ k ) ) { $ tokens [ ] = [ $ k , $ v ] ; break ; } } } return $ tokens ; }
Tokenize an XPath expression for use in PHP
44,142
public function parse ( $ template ) { $ dom = TemplateLoader :: load ( $ template ) ; $ ir = new DOMDocument ; $ ir -> loadXML ( '<template/>' ) ; $ this -> createXPath ( $ dom ) ; $ this -> parseChildren ( $ ir -> documentElement , $ dom -> documentElement ) ; $ this -> normalizer -> normalize ( $ ir ) ; return $ ir ; }
Parse a template into an internal representation
44,143
protected function parseChildren ( DOMElement $ ir , DOMElement $ parent ) { foreach ( $ parent -> childNodes as $ child ) { switch ( $ child -> nodeType ) { case XML_COMMENT_NODE : break ; case XML_TEXT_NODE : if ( trim ( $ child -> textContent ) !== '' ) { $ this -> appendLiteralOutput ( $ ir , $ child -> textContent ) ; } break ; case XML_ELEMENT_NODE : $ this -> parseNode ( $ ir , $ child ) ; break ; default : throw new RuntimeException ( "Cannot parse node '" . $ child -> nodeName . "''" ) ; } } }
Parse all the children of a given element
44,144
protected function parseNode ( DOMElement $ ir , DOMElement $ node ) { if ( $ node -> namespaceURI === self :: XMLNS_XSL ) { $ methodName = 'parseXsl' . str_replace ( ' ' , '' , ucwords ( str_replace ( '-' , ' ' , $ node -> localName ) ) ) ; if ( ! method_exists ( $ this , $ methodName ) ) { throw new RuntimeException ( "Element '" . $ node -> nodeName . "' is not supported" ) ; } return $ this -> $ methodName ( $ ir , $ node ) ; } $ element = $ this -> appendElement ( $ ir , 'element' ) ; $ element -> setAttribute ( 'name' , $ node -> nodeName ) ; $ xpath = new DOMXPath ( $ node -> ownerDocument ) ; foreach ( $ xpath -> query ( 'namespace::*' , $ node ) as $ ns ) { if ( $ node -> hasAttribute ( $ ns -> nodeName ) ) { $ irAttribute = $ this -> appendElement ( $ element , 'attribute' ) ; $ irAttribute -> setAttribute ( 'name' , $ ns -> nodeName ) ; $ this -> appendLiteralOutput ( $ irAttribute , $ ns -> nodeValue ) ; } } foreach ( $ node -> attributes as $ attribute ) { $ irAttribute = $ this -> appendElement ( $ element , 'attribute' ) ; $ irAttribute -> setAttribute ( 'name' , $ attribute -> nodeName ) ; $ this -> appendAVT ( $ irAttribute , $ attribute -> value ) ; } $ this -> parseChildren ( $ element , $ node ) ; }
Parse a given node into the internal representation
44,145
protected function elementHasSafeUrl ( DOMElement $ element ) { return $ element -> firstChild instanceof DOMText && $ this -> isSafeUrl ( $ element -> firstChild -> textContent ) ; }
Test whether given element contains a known - safe URL
44,146
public static function getJSNodes ( DOMDocument $ dom ) { $ regexp = '/^(?:data-s9e-livepreview-postprocess$|on)/i' ; $ nodes = array_merge ( self :: getAttributesByRegexp ( $ dom , $ regexp ) , self :: getElementsByRegexp ( $ dom , '/^script$/i' ) ) ; return $ nodes ; }
Return all DOMNodes whose content is JavaScript
44,147
public static function getURLNodes ( DOMDocument $ dom ) { $ regexp = '/(?:^(?:action|background|c(?:ite|lassid|odebase)|data|formaction|href|icon|longdesc|manifest|p(?:ing|luginspage|oster|rofile)|usemap)|src)$/i' ; $ nodes = self :: getAttributesByRegexp ( $ dom , $ regexp ) ; foreach ( self :: getObjectParamsByRegexp ( $ dom , '/^(?:dataurl|movie)$/i' ) as $ param ) { $ node = $ param -> getAttributeNode ( 'value' ) ; if ( $ node ) { $ nodes [ ] = $ node ; } } return $ nodes ; }
Return all DOMNodes whose content is an URL
44,148
protected static function getNodes ( DOMDocument $ dom , $ type ) { $ nodes = [ ] ; $ prefix = ( $ type === 'attribute' ) ? '@' : '' ; $ xpath = new DOMXPath ( $ dom ) ; foreach ( $ xpath -> query ( '//' . $ prefix . '*' ) as $ node ) { $ nodes [ ] = [ $ node , $ node -> nodeName ] ; } foreach ( $ xpath -> query ( '//xsl:' . $ type ) as $ node ) { $ nodes [ ] = [ $ node , $ node -> getAttribute ( 'name' ) ] ; } foreach ( $ xpath -> query ( '//xsl:copy-of' ) as $ node ) { if ( preg_match ( '/^' . $ prefix . '(\\w+)$/' , $ node -> getAttribute ( 'select' ) , $ m ) ) { $ nodes [ ] = [ $ node , $ m [ 1 ] ] ; } } return $ nodes ; }
Return all nodes of given type
44,149
public function encode ( $ xsl ) { $ this -> xsl = $ xsl ; $ this -> estimateSavings ( ) ; $ this -> filterSavings ( ) ; $ this -> buildDictionary ( ) ; $ js = json_encode ( $ this -> getCompressedStylesheet ( ) ) ; if ( ! empty ( $ this -> dictionary ) ) { $ js .= '.replace(' . $ this -> getReplacementRegexp ( ) . ',function(k){return' . json_encode ( $ this -> dictionary ) . '[k]})' ; } return $ js ; }
Encode given stylesheet into a compact JavaScript representation
44,150
protected function buildDictionary ( ) { $ keys = $ this -> getAvailableKeys ( ) ; rsort ( $ keys ) ; $ this -> dictionary = [ ] ; arsort ( $ this -> savings ) ; foreach ( array_keys ( $ this -> savings ) as $ str ) { $ key = array_pop ( $ keys ) ; if ( ! $ key ) { break ; } $ this -> dictionary [ $ key ] = $ str ; } }
Build a dictionary of all cost - effective string replacements
44,151
protected function estimateSavings ( ) { $ this -> savings = [ ] ; foreach ( $ this -> getStringsFrequency ( ) as $ str => $ cnt ) { $ len = strlen ( $ str ) ; $ originalCost = $ cnt * $ len ; $ replacementCost = $ cnt * 2 ; $ overhead = $ len + 6 ; $ this -> savings [ $ str ] = $ originalCost - ( $ replacementCost + $ overhead ) ; } }
Estimate the savings of every possible string replacement
44,152
protected function filterSavings ( ) { $ this -> savings = array_filter ( $ this -> savings , function ( $ saving ) { return ( $ saving >= $ this -> minSaving ) ; } ) ; }
Filter the savings according to the minSaving property
44,153
protected function getPossibleKeys ( ) { $ keys = [ ] ; foreach ( range ( 'a' , 'z' ) as $ char ) { $ keys [ ] = $ this -> keyPrefix . $ char ; } return $ keys ; }
Return a list of possible dictionary keys
44,154
protected function getStringsFrequency ( ) { $ regexp = '(' . implode ( '|' , $ this -> deduplicateTargets ) . ')S' ; preg_match_all ( $ regexp , $ this -> xsl , $ matches ) ; return array_count_values ( $ matches [ 0 ] ) ; }
Return the frequency of all deduplicatable strings
44,155
protected function getUnavailableKeys ( ) { preg_match_all ( '(' . preg_quote ( $ this -> keyPrefix ) . '.)' , $ this -> xsl , $ matches ) ; return array_unique ( $ matches [ 0 ] ) ; }
Return the list of possible dictionary keys that appear in the original stylesheet
44,156
protected function getPrefixLength ( array $ strings ) { $ i = 0 ; $ len = 0 ; $ maxLen = min ( array_map ( 'strlen' , $ strings ) ) ; while ( $ i < $ maxLen ) { $ c = $ strings [ 0 ] [ $ i ] ; foreach ( $ strings as $ string ) { if ( $ string [ $ i ] !== $ c ) { break 2 ; } } $ len = ++ $ i ; } return $ len ; }
Compute the number of leading characters common to all strings
44,157
protected function optimizeLeadingText ( ) { $ strings = $ this -> getTextContent ( 'firstChild' ) ; if ( empty ( $ strings ) ) { return ; } $ len = $ this -> getPrefixLength ( $ strings ) ; if ( $ len ) { $ this -> adjustTextNodes ( 'firstChild' , $ len ) ; $ this -> choose -> parentNode -> insertBefore ( $ this -> createText ( substr ( $ strings [ 0 ] , 0 , $ len ) ) , $ this -> choose ) ; } }
Move common leading text outside of current choose
44,158
protected function optimizeTrailingText ( ) { $ strings = $ this -> getTextContent ( 'lastChild' ) ; if ( empty ( $ strings ) ) { return ; } $ len = $ this -> getPrefixLength ( array_map ( 'strrev' , $ strings ) ) ; if ( $ len ) { $ this -> adjustTextNodes ( 'lastChild' , 0 , - $ len ) ; $ this -> choose -> parentNode -> insertBefore ( $ this -> createText ( substr ( $ strings [ 0 ] , - $ len ) ) , $ this -> choose -> nextSibling ) ; } }
Move common trailing text outside of current choose
44,159
public function containsCallback ( callable $ callback ) { $ pc = new ProgrammableCallback ( $ callback ) ; $ callback = $ pc -> getCallback ( ) ; foreach ( $ this -> items as $ filter ) { if ( $ callback === $ filter -> getCallback ( ) ) { return true ; } } return false ; }
Test whether this filter chain contains given callback
44,160
public function normalizeValue ( $ value ) { $ className = $ this -> getFilterClassName ( ) ; if ( $ value instanceof $ className ) { return $ value ; } if ( ! is_callable ( $ value ) ) { throw new InvalidArgumentException ( 'Filter ' . var_export ( $ value , true ) . ' is neither callable nor an instance of ' . $ className ) ; } return new $ className ( $ value ) ; }
Normalize a value into an TagFilter instance
44,161
public function getRules ( TagCollection $ tags ) { $ tagInspectors = $ this -> getTagInspectors ( $ tags ) ; return [ 'root' => $ this -> generateRootRules ( $ tagInspectors ) , 'tags' => $ this -> generateTagRules ( $ tagInspectors ) ] ; }
Generate rules for given tag collection
44,162
protected function generateTagRules ( array $ tagInspectors ) { $ rules = [ ] ; foreach ( $ tagInspectors as $ tagName => $ tagInspector ) { $ rules [ $ tagName ] = $ this -> generateRuleset ( $ tagInspector , $ tagInspectors ) ; } return $ rules ; }
Generate and return rules based on a set of TemplateInspector
44,163
protected function generateRootRules ( array $ tagInspectors ) { $ rootInspector = new TemplateInspector ( '<div><xsl:apply-templates/></div>' ) ; $ rules = $ this -> generateRuleset ( $ rootInspector , $ tagInspectors ) ; unset ( $ rules [ 'autoClose' ] ) ; unset ( $ rules [ 'autoReopen' ] ) ; unset ( $ rules [ 'breakParagraph' ] ) ; unset ( $ rules [ 'closeAncestor' ] ) ; unset ( $ rules [ 'closeParent' ] ) ; unset ( $ rules [ 'fosterParent' ] ) ; unset ( $ rules [ 'ignoreSurroundingWhitespace' ] ) ; unset ( $ rules [ 'isTransparent' ] ) ; unset ( $ rules [ 'requireAncestor' ] ) ; unset ( $ rules [ 'requireParent' ] ) ; return $ rules ; }
Generate a set of rules to be applied at the root of a document
44,164
protected function generateRuleset ( TemplateInspector $ srcInspector , array $ trgInspectors ) { $ rules = [ ] ; foreach ( $ this -> collection as $ rulesGenerator ) { if ( $ rulesGenerator instanceof BooleanRulesGenerator ) { foreach ( $ rulesGenerator -> generateBooleanRules ( $ srcInspector ) as $ ruleName => $ bool ) { $ rules [ $ ruleName ] = $ bool ; } } if ( $ rulesGenerator instanceof TargetedRulesGenerator ) { foreach ( $ trgInspectors as $ tagName => $ trgInspector ) { $ targetedRules = $ rulesGenerator -> generateTargetedRules ( $ srcInspector , $ trgInspector ) ; foreach ( $ targetedRules as $ ruleName ) { $ rules [ $ ruleName ] [ ] = $ tagName ; } } } } return $ rules ; }
Generate a set of rules for a single TemplateInspector instance
44,165
protected function getTagInspectors ( TagCollection $ tags ) { $ tagInspectors = [ ] ; foreach ( $ tags as $ tagName => $ tag ) { $ template = ( isset ( $ tag -> template ) ) ? $ tag -> template : '<xsl:apply-templates/>' ; $ tagInspectors [ $ tagName ] = new TemplateInspector ( $ template ) ; } return $ tagInspectors ; }
Inspect given list of tags
44,166
public static function filter ( $ attrValue , array $ map , $ strict ) { if ( isset ( $ map [ $ attrValue ] ) ) { return $ map [ $ attrValue ] ; } return ( $ strict ) ? false : $ attrValue ; }
Filter a value through a hash map
44,167
protected function setLinkAttributes ( Tag $ tag , $ linkInfo , $ attrName ) { $ url = trim ( $ linkInfo ) ; $ title = '' ; $ pos = strpos ( $ url , ' ' ) ; if ( $ pos !== false ) { $ title = substr ( trim ( substr ( $ url , $ pos ) ) , 1 , - 1 ) ; $ url = substr ( $ url , 0 , $ pos ) ; } $ tag -> setAttribute ( $ attrName , $ this -> text -> decode ( $ url ) ) ; if ( $ title > '' ) { $ tag -> setAttribute ( 'title' , $ this -> text -> decode ( $ title ) ) ; } }
Set a URL or IMG tag s attributes
44,168
protected function setRenderingHints ( ) { $ this -> hints [ 'postProcessing' ] = ( int ) ( strpos ( $ this -> xsl , 'data-s9e-livepreview-postprocess' ) !== false ) ; $ this -> hints [ 'ignoreAttrs' ] = ( int ) ( strpos ( $ this -> xsl , 'data-s9e-livepreview-ignore-attrs' ) !== false ) ; }
Set hints related to rendering
44,169
protected function setRulesHints ( ) { $ this -> hints [ 'closeAncestor' ] = 0 ; $ this -> hints [ 'closeParent' ] = 0 ; $ this -> hints [ 'createChild' ] = 0 ; $ this -> hints [ 'fosterParent' ] = 0 ; $ this -> hints [ 'requireAncestor' ] = 0 ; $ flags = 0 ; foreach ( $ this -> config [ 'tags' ] as $ tagConfig ) { foreach ( array_intersect_key ( $ tagConfig [ 'rules' ] , $ this -> hints ) as $ k => $ v ) { $ this -> hints [ $ k ] = 1 ; } $ flags |= $ tagConfig [ 'rules' ] [ 'flags' ] ; } $ flags |= $ this -> config [ 'rootContext' ] [ 'flags' ] ; $ parser = new ReflectionClass ( 's9e\\TextFormatter\\Parser' ) ; foreach ( $ parser -> getConstants ( ) as $ constName => $ constValue ) { if ( substr ( $ constName , 0 , 5 ) === 'RULE_' ) { $ this -> hints [ $ constName ] = ( $ flags & $ constValue ) ? 1 : 0 ; } } }
Set hints related to rules
44,170
protected function setTagAttributesHints ( array $ tagConfig ) { if ( empty ( $ tagConfig [ 'attributes' ] ) ) { return ; } foreach ( $ tagConfig [ 'attributes' ] as $ attrConfig ) { $ this -> hints [ 'attributeDefaultValue' ] |= isset ( $ attrConfig [ 'defaultValue' ] ) ; } }
Set hints based on given tag s attributes config
44,171
protected function setTagsHints ( ) { $ this -> hints [ 'attributeDefaultValue' ] = 0 ; $ this -> hints [ 'namespaces' ] = 0 ; foreach ( $ this -> config [ 'tags' ] as $ tagName => $ tagConfig ) { $ this -> hints [ 'namespaces' ] |= ( strpos ( $ tagName , ':' ) !== false ) ; $ this -> setTagAttributesHints ( $ tagConfig ) ; } }
Set hints related to tags config
44,172
public static function filterConfig ( array $ config , $ target = 'PHP' ) { $ filteredConfig = [ ] ; foreach ( $ config as $ name => $ value ) { if ( $ value instanceof FilterableConfigValue ) { $ value = $ value -> filterConfig ( $ target ) ; if ( ! isset ( $ value ) ) { continue ; } } if ( is_array ( $ value ) ) { $ value = self :: filterConfig ( $ value , $ target ) ; } $ filteredConfig [ $ name ] = $ value ; } return $ filteredConfig ; }
Recursively filter a config array to replace variants with the desired value
44,173
public static function generateQuickMatchFromList ( array $ strings ) { foreach ( $ strings as $ string ) { $ stringLen = strlen ( $ string ) ; $ substrings = [ ] ; for ( $ len = $ stringLen ; $ len ; -- $ len ) { $ pos = $ stringLen - $ len ; do { $ substrings [ substr ( $ string , $ pos , $ len ) ] = 1 ; } while ( -- $ pos >= 0 ) ; } if ( isset ( $ goodStrings ) ) { $ goodStrings = array_intersect_key ( $ goodStrings , $ substrings ) ; if ( empty ( $ goodStrings ) ) { break ; } } else { $ goodStrings = $ substrings ; } } if ( empty ( $ goodStrings ) ) { return false ; } return strval ( key ( $ goodStrings ) ) ; }
Generate a quickMatch string from a list of strings
44,174
public static function optimizeArray ( array & $ config , array & $ cache = [ ] ) { foreach ( $ config as $ k => & $ v ) { if ( ! is_array ( $ v ) ) { continue ; } self :: optimizeArray ( $ v , $ cache ) ; $ cacheKey = serialize ( $ v ) ; if ( ! isset ( $ cache [ $ cacheKey ] ) ) { $ cache [ $ cacheKey ] = $ v ; } $ config [ $ k ] = & $ cache [ $ cacheKey ] ; } unset ( $ v ) ; }
Optimize the size of a deep array by deduplicating identical structures
44,175
protected function isSafe ( $ context ) { $ methodName = 'isSafe' . $ context ; foreach ( $ this -> filterChain as $ filter ) { if ( $ filter -> $ methodName ( ) ) { return true ; } } return ! empty ( $ this -> markedSafe [ $ context ] ) ; }
Return whether this attribute is safe to be used in given context
44,176
public function generate ( Rendering $ rendering ) { $ compiledTemplates = array_map ( [ $ this , 'compileTemplate' ] , $ rendering -> getTemplates ( ) ) ; $ php = [ ] ; $ php [ ] = ' extends \\s9e\\TextFormatter\\Renderers\\PHP' ; $ php [ ] = '{' ; $ php [ ] = ' protected $params=' . self :: export ( $ rendering -> getAllParameters ( ) ) . ';' ; $ php [ ] = ' protected function renderNode(\\DOMNode $node)' ; $ php [ ] = ' {' ; $ php [ ] = ' ' . SwitchStatement :: generate ( '$node->nodeName' , $ compiledTemplates , '$this->at($node);' ) ; $ php [ ] = ' }' ; if ( $ this -> enableQuickRenderer ) { $ php [ ] = Quick :: getSource ( $ compiledTemplates ) ; } $ php [ ] = '}' ; $ php = implode ( "\n" , $ php ) ; if ( isset ( $ this -> controlStructuresOptimizer ) ) { $ php = $ this -> controlStructuresOptimizer -> optimize ( $ php ) ; } $ className = ( isset ( $ this -> className ) ) ? $ this -> className : $ this -> defaultClassPrefix . sha1 ( $ php ) ; $ this -> lastClassName = $ className ; $ header = "\n" . "/**\n" . "* @package s9e\TextFormatter\n" . "* @copyright Copyright (c) 2010-2019 The s9e Authors\n" . "* @license http://www.opensource.org/licenses/mit-license.php The MIT License\n" . "*/\n" ; $ pos = strrpos ( $ className , '\\' ) ; if ( $ pos !== false ) { $ header .= 'namespace ' . substr ( $ className , 0 , $ pos ) . ";\n\n" ; $ className = substr ( $ className , 1 + $ pos ) ; } $ php = $ header . 'class ' . $ className . $ php ; return $ php ; }
Generate the source for a PHP class that renders an intermediate representation according to given rendering configuration
44,177
protected static function export ( array $ value ) { $ pairs = [ ] ; foreach ( $ value as $ k => $ v ) { $ pairs [ ] = var_export ( $ k , true ) . '=>' . var_export ( $ v , true ) ; } return '[' . implode ( ',' , $ pairs ) . ']' ; }
Export given array as PHP code
44,178
protected function compileTemplate ( $ template ) { $ template = $ this -> normalizer -> normalizeTemplate ( $ template ) ; $ ir = TemplateParser :: parse ( $ template ) ; $ php = $ this -> serializer -> serialize ( $ ir -> documentElement ) ; if ( isset ( $ this -> optimizer ) ) { $ php = $ this -> optimizer -> optimize ( $ php ) ; } return $ php ; }
Compile a template to PHP
44,179
protected function exportCallback ( $ namespace , callable $ callback , $ argument ) { if ( is_array ( $ callback ) && is_string ( $ callback [ 0 ] ) ) { $ callback = $ callback [ 0 ] . '::' . $ callback [ 1 ] ; } if ( ! is_string ( $ callback ) ) { return 'call_user_func(' . var_export ( $ callback , true ) . ', ' . $ argument . ')' ; } if ( $ callback [ 0 ] !== '\\' ) { $ callback = '\\' . $ callback ; } if ( substr ( $ callback , 0 , 2 + strlen ( $ namespace ) ) === '\\' . $ namespace . '\\' ) { $ callback = substr ( $ callback , 2 + strlen ( $ namespace ) ) ; } return $ callback . '(' . $ argument . ')' ; }
Export a given callback as PHP code
44,180
protected function exportObject ( $ obj ) { $ str = call_user_func ( $ this -> serializer , $ obj ) ; $ str = var_export ( $ str , true ) ; return $ this -> unserializer . '(' . $ str . ')' ; }
Serialize and export a given object as PHP code
44,181
public static function parse ( $ attrValue ) { preg_match_all ( '(\\{\\{|\\{(?:[^\'"}]|\'[^\']*\'|"[^"]*")+\\}|\\{|[^{]++)' , $ attrValue , $ matches ) ; $ tokens = [ ] ; foreach ( $ matches [ 0 ] as $ str ) { if ( $ str === '{{' || $ str === '{' ) { $ tokens [ ] = [ 'literal' , '{' ] ; } elseif ( $ str [ 0 ] === '{' ) { $ tokens [ ] = [ 'expression' , substr ( $ str , 1 , - 1 ) ] ; } else { $ tokens [ ] = [ 'literal' , str_replace ( '}}' , '}' , $ str ) ] ; } } return $ tokens ; }
Parse an attribute value template
44,182
public static function replace ( DOMAttr $ attribute , callable $ callback ) { $ tokens = self :: parse ( $ attribute -> value ) ; foreach ( $ tokens as $ k => $ token ) { $ tokens [ $ k ] = $ callback ( $ token ) ; } $ attribute -> value = htmlspecialchars ( self :: serialize ( $ tokens ) , ENT_NOQUOTES , 'UTF-8' ) ; }
Replace the value of an attribute via the provided callback
44,183
public static function serialize ( array $ tokens ) { $ attrValue = '' ; foreach ( $ tokens as $ token ) { if ( $ token [ 0 ] === 'literal' ) { $ attrValue .= preg_replace ( '([{}])' , '$0$0' , $ token [ 1 ] ) ; } elseif ( $ token [ 0 ] === 'expression' ) { $ attrValue .= '{' . $ token [ 1 ] . '}' ; } else { throw new RuntimeException ( 'Unknown token type' ) ; } } return $ attrValue ; }
Serialize an array of AVT tokens back into an attribute value
44,184
public static function toXSL ( $ attrValue ) { $ xsl = '' ; foreach ( self :: parse ( $ attrValue ) as list ( $ type , $ content ) ) { if ( $ type === 'expression' ) { $ xsl .= '<xsl:value-of select="' . htmlspecialchars ( $ content , ENT_COMPAT , 'UTF-8' ) . '"/>' ; } elseif ( trim ( $ content ) !== $ content ) { $ xsl .= '<xsl:text>' . htmlspecialchars ( $ content , ENT_NOQUOTES , 'UTF-8' ) . '</xsl:text>' ; } else { $ xsl .= htmlspecialchars ( $ content , ENT_NOQUOTES , 'UTF-8' ) ; } } return $ xsl ; }
Transform given attribute value template into an XSL fragment
44,185
protected function setUp ( ) { $ tags = [ 'TABLE' => [ 'template' => '<table><xsl:apply-templates/></table>' ] , 'TBODY' => [ 'template' => '<tbody><xsl:apply-templates/></tbody>' ] , 'TD' => $ this -> generateCellTagConfig ( 'td' ) , 'TH' => $ this -> generateCellTagConfig ( 'th' ) , 'THEAD' => [ 'template' => '<thead><xsl:apply-templates/></thead>' ] , 'TR' => [ 'template' => '<tr><xsl:apply-templates/></tr>' ] ] ; foreach ( $ tags as $ tagName => $ tagConfig ) { if ( ! isset ( $ this -> configurator -> tags [ $ tagName ] ) ) { $ this -> configurator -> tags -> add ( $ tagName , $ tagConfig ) ; } } }
Create the tags used by this plugin
44,186
protected function generateCellTagConfig ( $ elName ) { $ alignFilter = new ChoiceFilter ( [ 'left' , 'center' , 'right' , 'justify' ] , true ) ; return [ 'attributes' => [ 'align' => [ 'filterChain' => [ 'strtolower' , $ alignFilter ] , 'required' => false ] ] , 'rules' => [ 'createParagraphs' => false ] , 'template' => '<' . $ elName . '> <xsl:if test="@align"> <xsl:attribute name="style">text-align:<xsl:value-of select="@align"/></xsl:attribute> </xsl:if> <xsl:apply-templates/> </' . $ elName . '>' ] ; }
Generate the tag config for give cell element
44,187
public function normalize ( DOMDocument $ ir ) { $ this -> createXPath ( $ ir ) ; $ this -> addDefaultCase ( $ ir ) ; $ this -> addElementIds ( $ ir ) ; $ this -> addCloseTagElements ( $ ir ) ; $ this -> markVoidElements ( $ ir ) ; $ this -> optimizer -> optimize ( $ ir ) ; $ this -> markConditionalCloseTagElements ( $ ir ) ; $ this -> setOutputContext ( $ ir ) ; $ this -> markBranchTables ( $ ir ) ; }
Normalize an IR
44,188
protected function getOutputContext ( DOMNode $ output ) { $ contexts = [ 'boolean(ancestor::attribute)' => 'attribute' , '@disable-output-escaping="yes"' => 'raw' , 'count(ancestor::element[@name="script"])' => 'raw' ] ; foreach ( $ contexts as $ expr => $ context ) { if ( $ this -> evaluate ( $ expr , $ output ) ) { return $ context ; } } return 'text' ; }
Get the context type for given output element
44,189
protected function getParentElementId ( DOMNode $ node ) { $ parentNode = $ node -> parentNode ; while ( isset ( $ parentNode ) ) { if ( $ parentNode -> nodeName === 'element' ) { return $ parentNode -> getAttribute ( 'id' ) ; } $ parentNode = $ parentNode -> parentNode ; } }
Get the ID of the closest element ancestor
44,190
protected function markSwitchTable ( DOMElement $ switch ) { $ cases = [ ] ; $ maps = [ ] ; foreach ( $ this -> query ( './case[@test]' , $ switch ) as $ i => $ case ) { $ map = XPathHelper :: parseEqualityExpr ( $ case -> getAttribute ( 'test' ) ) ; if ( $ map === false ) { return ; } $ maps += $ map ; $ cases [ $ i ] = [ $ case , end ( $ map ) ] ; } if ( count ( $ maps ) !== 1 ) { return ; } $ switch -> setAttribute ( 'branch-key' , key ( $ maps ) ) ; foreach ( $ cases as list ( $ case , $ values ) ) { sort ( $ values ) ; $ case -> setAttribute ( 'branch-values' , serialize ( $ values ) ) ; } }
Mark given switch element if it s used as a branch table
44,191
protected function markVoidElements ( DOMDocument $ ir ) { foreach ( $ this -> query ( '//element' ) as $ element ) { $ elName = $ element -> getAttribute ( 'name' ) ; if ( strpos ( $ elName , '{' ) !== false ) { $ element -> setAttribute ( 'void' , 'maybe' ) ; } elseif ( preg_match ( $ this -> voidRegexp , $ elName ) ) { $ element -> setAttribute ( 'void' , 'yes' ) ; } } }
Mark void elements
44,192
protected function setOutputContext ( DOMDocument $ ir ) { foreach ( $ this -> query ( '//output' ) as $ output ) { $ output -> setAttribute ( 'escape' , $ this -> getOutputContext ( $ output ) ) ; } }
Fill in output context
44,193
protected function parseTokens ( $ definition ) { $ tokenTypes = [ 'choice' => 'CHOICE[0-9]*=(?<choices>.+?)' , 'map' => '(?:HASH)?MAP[0-9]*=(?<map>.+?)' , 'parse' => 'PARSE=(?<regexps>' . self :: REGEXP . '(?:,' . self :: REGEXP . ')*)' , 'range' => 'RANGE[0-9]*=(?<min>-?[0-9]+),(?<max>-?[0-9]+)' , 'regexp' => 'REGEXP[0-9]*=(?<regexp>' . self :: REGEXP . ')' , 'other' => '(?<other>[A-Z_]+[0-9]*)' ] ; preg_match_all ( '#\\{(' . implode ( '|' , $ tokenTypes ) . ')(?<options>\\??(?:;[^;]*)*)\\}#' , $ definition , $ matches , PREG_SET_ORDER | PREG_OFFSET_CAPTURE ) ; $ tokens = [ ] ; foreach ( $ matches as $ m ) { if ( isset ( $ m [ 'other' ] [ 0 ] ) && preg_match ( '#^(?:CHOICE|HASHMAP|MAP|REGEXP|PARSE|RANGE)#' , $ m [ 'other' ] [ 0 ] ) ) { throw new RuntimeException ( "Malformed token '" . $ m [ 'other' ] [ 0 ] . "'" ) ; } $ token = [ 'pos' => $ m [ 0 ] [ 1 ] , 'content' => $ m [ 0 ] [ 0 ] , 'options' => ( isset ( $ m [ 'options' ] [ 0 ] ) ) ? $ this -> parseOptionString ( $ m [ 'options' ] [ 0 ] ) : [ ] ] ; $ head = $ m [ 1 ] [ 0 ] ; $ pos = strpos ( $ head , '=' ) ; if ( $ pos === false ) { $ token [ 'id' ] = $ head ; } else { $ token [ 'id' ] = substr ( $ head , 0 , $ pos ) ; foreach ( $ m as $ k => $ v ) { if ( ! is_numeric ( $ k ) && $ k !== 'options' && $ v [ 1 ] !== - 1 ) { $ token [ $ k ] = $ v [ 0 ] ; } } } $ token [ 'type' ] = rtrim ( $ token [ 'id' ] , '0123456789' ) ; if ( $ token [ 'type' ] === 'PARSE' ) { preg_match_all ( '#' . self :: REGEXP . '(?:,|$)#' , $ token [ 'regexps' ] , $ m ) ; $ regexps = [ ] ; foreach ( $ m [ 0 ] as $ regexp ) { $ regexps [ ] = rtrim ( $ regexp , ',' ) ; } $ token [ 'regexps' ] = $ regexps ; } $ tokens [ ] = $ token ; } return $ tokens ; }
Parse and return all the tokens contained in a definition
44,194
protected function appendFilters ( Attribute $ attribute , $ filters ) { foreach ( preg_split ( '#\\s*,\\s*#' , $ filters ) as $ filterName ) { if ( substr ( $ filterName , 0 , 1 ) !== '#' && ! in_array ( $ filterName , $ this -> allowedFilters , true ) ) { throw new RuntimeException ( "Filter '" . $ filterName . "' is not allowed in BBCodes" ) ; } $ filter = $ this -> configurator -> attributeFilters -> get ( $ filterName ) ; $ attribute -> filterChain -> append ( $ filter ) ; } }
Append a list of filters to an attribute s filterChain
44,195
protected function isFilter ( $ tokenId ) { $ filterName = rtrim ( $ tokenId , '0123456789' ) ; if ( in_array ( $ filterName , $ this -> unfilteredTokens , true ) ) { return true ; } try { if ( $ this -> configurator -> attributeFilters -> get ( '#' . $ filterName ) ) { return true ; } } catch ( Exception $ e ) { } return false ; }
Test whether a token s name is the name of a filter
44,196
protected function parseOptionString ( $ string ) { $ string = preg_replace ( '(^\\?)' , ';optional' , $ string ) ; $ options = [ ] ; foreach ( preg_split ( '#;+#' , $ string , - 1 , PREG_SPLIT_NO_EMPTY ) as $ pair ) { $ pos = strpos ( $ pair , '=' ) ; if ( $ pos === false ) { $ k = $ pair ; $ v = true ; } else { $ k = substr ( $ pair , 0 , $ pos ) ; $ v = substr ( $ pair , 1 + $ pos ) ; } $ options [ $ k ] = $ v ; } return $ options ; }
Parse the option string into an associative array
44,197
private function getFiles ( ) { $ file = $ this -> event -> get ( 'document' ) ; $ response = $ this -> http -> get ( $ this -> buildApiUrl ( 'getFile' ) , [ 'file_id' => $ file [ 'file_id' ] , ] ) ; $ responseData = json_decode ( $ response -> getContent ( ) ) ; if ( $ response -> getStatusCode ( ) !== 200 ) { throw new TelegramAttachmentException ( 'Error retrieving file url: ' . $ responseData -> description ) ; } $ url = $ this -> buildFileApiUrl ( $ responseData -> result -> file_path ) ; return [ new File ( $ url , $ file ) ] ; }
Retrieve a file from an incoming message .
44,198
protected function isValidLoginRequest ( ) { $ check_hash = $ this -> queryParameters -> get ( 'hash' ) ; $ check = $ this -> queryParameters -> except ( 'hash' ) -> map ( function ( $ value , $ key ) { return $ key . '=' . $ value ; } ) -> values ( ) -> sort ( ) ; $ check_string = implode ( "\n" , $ check -> toArray ( ) ) ; $ secret = hash ( 'sha256' , $ this -> config -> get ( 'token' ) , true ) ; $ hash = hash_hmac ( 'sha256' , $ check_string , $ secret ) ; if ( strcmp ( $ hash , $ check_hash ) !== 0 ) { return false ; } if ( ( time ( ) - $ this -> queryParameters -> get ( 'auth_date' ) ) > 86400 ) { return false ; } return true ; }
Check if the query parameters contain information about a valid Telegram login request .
44,199
public function messagesHandled ( ) { $ callback = $ this -> payload -> get ( 'callback_query' ) ; if ( $ callback !== null ) { $ callback [ 'message' ] [ 'chat' ] [ 'id' ] ; $ this -> removeInlineKeyboard ( $ callback [ 'message' ] [ 'chat' ] [ 'id' ] , $ callback [ 'message' ] [ 'message_id' ] ) ; } }
This hide the inline keyboard if is an interactive message .