| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace League\HTMLToMarkdown\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use League\HTMLToMarkdown\Configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use League\HTMLToMarkdown\ConfigurationAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use League\HTMLToMarkdown\ElementInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class LinkConverter implements ConverterInterface, ConfigurationAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var Configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param Configuration $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 99 |  |     public function setConfig(Configuration $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 99 |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 99 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param ElementInterface $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 9 |  |     public function convert(ElementInterface $element) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 9 |  |         $href = $element->getAttribute('href'); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 9 |  |         $title = $element->getAttribute('title'); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 9 |  |         $text = trim($element->getValue(), "\t\n\r\0\x0B"); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 9 |  |         if ($title !== '') { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 3 |  |             $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 9 |  |         } elseif ($href === $text && $this->isValidAutolink($href)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 3 |  |             $markdown = '<' . $href . '>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 9 |  |         } elseif ($href === 'mailto:' . $text && $this->isValidEmail($text)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 3 |  |             $markdown = '<' . $text . '>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 2 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 9 |  |             if (stristr($href, ' ')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 $href = '<'.$href.'>'; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 9 |  |             $markdown = '[' . $text . '](' . $href . ')'; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 9 |  |         if (!$href) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 3 |  |             if ($this->shouldStrip()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 3 |  |                 $markdown = $text; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 3 |  |                 $markdown = html_entity_decode($element->getChildrenAsString()); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 9 |  |         return $markdown; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 99 |  |     public function getSupportedTags() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 99 |  |         return array('a'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $href | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |     private function isValidAutolink($href) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 6 |  |         $useAutolinks = $this->config->getOption('use_autolinks'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |         return $useAutolinks && (preg_match('/^[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*/i', $href) === 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 3 |  |     private function isValidEmail($email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // Email validation is messy business, but this should cover most cases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         return filter_var($email, FILTER_VALIDATE_EMAIL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |     private function shouldStrip() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |         return $this->config->getOption('strip_placeholder_links'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |