Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API 2.0

EOOS Automotive R22-08 is released in version v0.10.0

The second official release of EOOS Automotive that is qualified for POSIX and WIN32 compatible operating systems and based on interfaces designed for EOOS RT – real time operating system.

Elements copying

Until this instant, we talked about the elements searching for adding new, or changing those attributes. Sometimes, we need to copy elements. Let’s take an example, which was used by us, and remove the second block with “article” class attribute value. So, we will try to reconstruct removed block by using the first one block.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>PHP DOM Builder test page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  </head>
  <body>
    <div id="content">
      <h1>
        Header
      </h1>
      <div class="article" id="id_article">
        <h2>
          Article 1
        </h2>
        <p>
          <a href="google.com">Text 1</a>
        </p>
      </div>
      <!-- We need to reconstruct this one
      <div class="article print">
        <h2>
          <a href="/article/">Article 2</a>
        </h2>
        <p>
          <a href="baigudin.software">Text 2</a>
        </p>
      </div>
      -->

    </div>
    <div id="footer">
      <p>
        &copy; copyright 2016
      </p>
    </div>
  </body>
</html>

Before we will proceed to copy we want to note about basic rules of PHP language and DOM with reference to our library.

We told that every  HTML document could be represented as tree list with elements. In our case, the elements are objects of Element class. When we call the create method, we create a root element which is not liked to any trees. When we call some searching method, we get some objects which are linked to document tree.

As we know, PHP language classes  are passed by references. So, when we select some elements from a tree, we get references to those elements. All methods for new element creating receive a reference to ElementNode or ElementList class as argument. When the library gets that type of argument, it checks that for linking to other trees. If argument is linked, library clones it and all its child elements. Accordingly, library has gotten a copy of given branch and links coped branch to corresponding place of tree after that.

Usually, some elements on tree have some attributes which affect on outputted HTML document. If we want to get a valid HTML document, some attributes value must be unique for it. For example, a value of "id" or "name" attribute must be only one in a document . When library clones elements, it does not check  these factors. Thus, we can have a document with equal values of critical attributes for validating.

Let's try to allow what we were talking about, and copy a branch beginning of the DIV tag with "id_article" value of ID attribute.

<?php
// Find the source element
$div $document->getElementById('id_article');
// Clone found element to the tree after itself and get cloned branch
$div $div->after($div);
// Change attributes of DIV element
$div->removeAttr('id')->addClass('print');
// Change contents of H2 element
$div->find('h2')->insert('a')->href('/article/')->html('Article 2');
// Change contents of P element
$div->find('p a')->href('baigudin.software')->html('Text 2');
?>

We hope you have understood what will be as the result of this script executing. And in the conclusion of this article, we want to say about removing. Any elements of tree can be removed by using a remove method of the library. The method unlinks a removing element from tree and returns it with whole chain of child elements. So the removed element becomes a root.

To the previous article

To the articles list

To the next article