/**
  * @param DOMElement $wtElement <w:t> element in the document xml,
  * this method should be called sequentially for all the <w:t> elements in the order they appear in the document xml
  *
  */
 private function getTemplateKeys(DOMElement $wtElement)
 {
     if (strtoupper($wtElement->tagName) != "W:T") {
         $this->log(LOG_ALERT, "Invalid element for finding template keys : Line " . $wtElement->getLineNo());
         return false;
     }
     $keys = array();
     $textContent = $wtElement->textContent;
     $incompleteText = '';
     if (count($this->incompleteKeyNodes) > 0) {
         // incomplete keys are from different <p> elements , then discard the old incomplete elements
         $firstIncompleteKey = $this->incompleteKeyNodes[0];
         if ($firstIncompleteKey->element()->parentNode->parentNode !== $wtElement->parentNode->parentNode) {
             $this->log(LOG_WARNING, "incomplete keys in paragraph : Line " . $firstIncompleteKey->element()->parentNode->parentNode->getLineNo());
             $this->incompleteKeyNodes = array();
         }
         foreach ($this->incompleteKeyNodes as $incompleteKeyNode) {
             //$incompleteKeyNode will be an instance of KeyNode class
             $incompleteText .= $incompleteKeyNode->key();
         }
     }
     $textContent = $incompleteText . $textContent;
     $textChars = str_split($textContent);
     $key = null;
     $nonKey = "";
     for ($i = 0; $i < count($textChars); $i++) {
         if ($textChars[$i] === $this->keyStartChar || $textChars[$i] === $this->keyEndChar) {
             // found keyStartChar/keyEndChar check the \ character behind the keyStartChar/keyEndChar
             $j = $i - 1;
             for (; $j >= 0; $j--) {
                 if ($textChars[$j] != "\\") {
                     break;
                 }
             }
             if (($i - $j) % 2) {
                 // if i-j is odd ,
                 // then there are even numbers of \ chars behind found keyStartChar/keyEndChar
                 // so keyStartChar/keyEndChar is not escaped and hence valid
                 if ($textChars[$i] === $this->keyStartChar) {
                     //found keyStartChar
                     if ($nonKey !== "") {
                         $keyNode = new KeyNode($nonKey, false, true, $wtElement);
                         $keys[] = $keyNode;
                     }
                     if ($key != null) {
                         $keyNode = new KeyNode($key, false, true, $wtElement);
                         $keys[] = $keyNode;
                     }
                     $key = $textChars[$i];
                     $nonKey = "";
                 } else {
                     //found keyEndChar
                     if ($key !== null) {
                         $key = $key . $textChars[$i];
                         $keyNode = new KeyNode($key, true, true, $wtElement);
                         $keys[] = $keyNode;
                         $key = null;
                         $nonKey = "";
                     } else {
                         $nonKey = $nonKey . $textChars[$i];
                     }
                 }
                 continue;
             }
         }
         //neither keyStartChar nor keyEndChar
         if ($key !== null) {
             // if a key is started, append to it
             $key = $key . $textChars[$i];
         } else {
             $nonKey = $nonKey . $textChars[$i];
         }
     }
     if ($key !== null) {
         $openKey = new KeyNode($key, true, false, $wtElement);
     }
     if ($nonKey !== "") {
         $openText = new KeyNode($nonKey, false, true, $wtElement);
     }
     $incompleteKeys = false;
     if (count($this->incompleteKeyNodes) > 0) {
         $incompleteKeys = true;
     }
     if ($incompleteKeys && (!isset($openKey) || isset($openKey) && count($keys) > 0)) {
         // if there were incomplete keys and found one or more complete keys in current textContent
         // copy the incomplete keys content to current w:t element
         for ($i = count($this->incompleteKeyNodes) - 1; $i >= 0; $i--) {
             $incompleteKeyNode = $this->incompleteKeyNodes[$i];
             $incompleteKeyElement = $incompleteKeyNode->element();
             $incompleteKey = $incompleteKeyNode->key();
             //delete content from the incompleteKeyElement
             $incompleteKeyElementContent = $incompleteKeyElement->textContent;
             $incompleteKeyElementContent = substr($incompleteKeyElementContent, 0, strlen($incompleteKeyElementContent) - strlen($incompleteKey));
             if ($this->endsWith($incompleteKeyElementContent, " ")) {
                 $incompleteKeyElement->setAttribute("xml:space", "preserve");
             }
             $this->setTextContent($incompleteKeyElement, $incompleteKeyElementContent);
             //add incomplete key to this wtElement
             $thisTextContent = $wtElement->textContent;
             $this->setTextContent($wtElement, $incompleteKey . $thisTextContent);
         }
         $this->incompleteKeyNodes = array();
     }
     if (isset($openKey) && (!$incompleteKeys || $incompleteKeys && count($keys) > 0)) {
         $this->incompleteKeyNodes[] = $openKey;
         $keys[] = $openKey;
     }
     if (isset($openKey) && $incompleteKeys && count($keys) == 0) {
         $thisTextAsKeyNode = new KeyNode($wtElement->textContent, true, false, $wtElement);
         $this->incompleteKeyNodes[] = $thisTextAsKeyNode;
         $keys[] = $thisTextAsKeyNode;
     }
     if (isset($openText)) {
         $keys[] = $openText;
     }
     return $keys;
 }
 /**
  * @param \DOMElement $description
  * @param Component   $component
  *
  * @return mixed
  * @throws \MWException
  */
 protected function getModifiedComponent(\DOMElement $description, Component $component)
 {
     if (!$description->hasAttribute('type')) {
         throw new \MWException(sprintf('%s (line %d): Modification element missing an attribute: type.', $this->getLayoutFile(), $description->getLineNo()));
     }
     $className = 'Skins\\Chameleon\\Components\\Modifications\\' . $description->getAttribute('type');
     if (!class_exists($className) || !is_subclass_of($className, 'Skins\\Chameleon\\Components\\Modifications\\Modification')) {
         throw new \MWException(sprintf('%s (line %d): Invalid modification type: %s.', $this->getLayoutFile(), $description->getLineNo(), $description->getAttribute('type')));
     }
     return new $className($component, $description);
 }
 /**
  * Returns form node unique identifier.
  *
  * @param \DOMElement $form
  *
  * @return string
  */
 private function getFormNodeId(\DOMElement $form)
 {
     return md5($form->getLineNo() . $form->getNodePath() . $form->nodeValue);
 }