/**
  * {@inheritdoc}
  */
 public function isEmpty()
 {
     if ($this->element->getValue()) {
         return false;
     }
     return true;
 }
 /**
  * Detect valid formatting elements within a paragraph
  *
  * @param  \Candybanana\HtmlToCarbonJson\Element
  * @return string
  */
 protected function detectFormatting(Element $element)
 {
     if ($element->hasChildren()) {
         $formats = [];
         foreach ($element->getChildren() as $node) {
             if (!$node->isText() && in_array($node->getTagName(), $this->config['allowedFormattingTags'])) {
                 $type = $this->config['formattingTags'][$node->getTagName()];
                 if (empty($node->getValue())) {
                     continue;
                 }
                 list($from, $to) = $this->getTextPosition($element->getValue(), $node->getValue());
                 $format = ['type' => $type, 'from' => $from, 'to' => $to];
                 // if <a> tag, add attributes
                 if ($type == 'a') {
                     $format['attrs'] = ['href' => $node->getAttribute('href')];
                 }
                 $formats[] = $format;
             }
         }
         return !empty($formats) ? $formats : null;
     }
 }
Exemplo n.º 3
0
 /**
  * Builds an object that will represent our JSON based on the sections we've extracted
  *
  * @param  \Candybanana\HtmlToCarbonJson\Element
  * @return string
  */
 protected function convertSection(Element $section)
 {
     $layouts = [];
     $layout = $prevComponentClass = null;
     foreach ($section->getChildren() as $sectionChild) {
         if (!($component = $this->matchWithComponent($sectionChild))) {
             // is there's a value in this element, throw error, otherwise ignore it
             if (!trim($sectionChild->getValue())) {
                 continue;
             }
             // build the tag for easy debugging
             $tag = '<' . $sectionChild->getTagName();
             foreach ($sectionChild->getAttributes() as $attribute) {
                 $tag .= ' ' . $attribute->name . '="';
                 $tag .= $sectionChild->getAttribute($attribute->name) . '"';
             }
             $tag .= '>';
             dbg($section->getValue());
             dbg($tag);
             dbg($sectionChild->getValue());
             throw new Exceptions\InvalidStructureException("No component loaded to render '{$tag}' tags.");
         }
         // if empty, skip this child
         if ($component->isEmpty()) {
             continue;
         }
         $componentClass = get_class($component);
         // create a new layout if we're not in one, or the component requires a new one
         if ($component->requiresNewLayout() || $componentClass !== $prevComponentClass) {
             // save previous layout
             if ($layout) {
                 $layouts[] = $layout->render();
             }
             // create new
             $layout = new Layout($component->getLayout());
         }
         $layout->addComponent($component->render($this));
         $prevComponentClass = $componentClass;
     }
     // render the last layout
     if ($layout) {
         $layouts[] = $layout->render();
     }
     // create final section json
     if (!empty($layouts)) {
         $section = ['name' => self::carbonId(), 'component' => 'Section', 'components' => $layouts];
         return $section;
     }
     // Strip nodes named in remove_nodes
     // $tags_to_remove = explode(' ', $this->getConfig()->getOption('remove_nodes'));
     // if (in_array($tag, $tags_to_remove)) {
     //     return false;
     // }
 }