Skip to content

Commit 76f8df9

Browse files
committed
Refine example
1 parent e9a04c3 commit 76f8df9

2 files changed

Lines changed: 141 additions & 14 deletions

File tree

  • examples
    • basic-manipulation-filter-and-retrieval
    • parsing-xml-from-url-and-dynamically-generating-html
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../vendor/autoload.php';
4+
5+
/*
6+
* HTML Example
7+
*/
8+
$html = <<<EOF
9+
<table>
10+
<tr id="row1">
11+
<td>one</td>
12+
<td>two</td>
13+
<td>three</td>
14+
</tr>
15+
16+
<tr id="row2">
17+
<td>four</td>
18+
<td>five</td>
19+
<td>six</td>
20+
</tr>
21+
</table>
22+
EOF;
23+
24+
echo '<h1>Basic HTML Usage</h1>';
25+
echo 'The following HTML chunk will get parsed, traverse, filtered, and manipulated:';
26+
echo '<pre><code>' . htmlspecialchars($html) . '</code></pre>';
27+
28+
echo '<h2>Example 1</h2>';
29+
echo 'Add the attribute <code>class="cell"</code> to all <code>&lt;td&gt;</code> elements:';
30+
31+
echo '<pre><code>';
32+
33+
echo htmlspecialchars(
34+
html5qp($html, 'td')
35+
->attr('class', 'cell')
36+
->top() // return to <html> tag
37+
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
38+
);
39+
40+
echo '</code></pre>';
41+
42+
echo '<h2>Example 2</h2>';
43+
echo 'Use <code>html5qp($html)->find(\'#row2 > td:nth-child(2)\')->text();</code> to display the contents of the second <code>&lt;td&gt;</code> in the second <code>&lt;tr&gt;</code>: <br><strong>';
44+
45+
echo html5qp($html)
46+
->find('#row2 > td:nth-child(2)')
47+
->text();
48+
49+
echo '</strong>';
50+
51+
echo '<h2>Example 3</h2>';
52+
echo 'Append another row to the HTML and output the results:';
53+
echo '<code><pre>';
54+
55+
echo htmlspecialchars(
56+
html5qp($html, 'tr:last')
57+
->after("\n\n\t<tr>\n\t\t<td>seven</td>\n\t\t<td>eight</td>\n\t\t<td>nine</td>\n\t</tr>")
58+
->top() // return to <html> tag
59+
->innerHTML5() // get mark-up without <html>. Use ->html5() to return a valid HTML document (Doctype and all)
60+
);
61+
62+
echo '</pre></code>';
63+
64+
65+
/*
66+
* XML Example
67+
*/
68+
$xml = <<<EOF
69+
<?xml version="1.0"?>
70+
<categories>
71+
<category name="DOM">
72+
<desc>This is the DOM description...</desc>
73+
</category>
74+
75+
<category name="Traversing">
76+
<desc>This is the Traversing description...</desc>
77+
</category>
78+
79+
<category name="Filtering">
80+
<desc>This is the Filtering description...</desc>
81+
</category>
82+
83+
<category name="Selectors">
84+
<desc>This is the Selectors description...</desc>
85+
</category>
86+
</categories>
87+
EOF;
88+
89+
echo '<h1>Basic XML Usage</h1>';
90+
echo 'The following XML will get parsed, traverse, filtered, and manipulated:';
91+
echo '<pre><code>' . htmlspecialchars($xml) . '</code></pre>';
92+
93+
echo '<h2>Example 1</h2>';
94+
echo 'Add the attribute <code>class="item"</code> to all <code>&lt;desc&gt;</code> elements:';
95+
96+
echo '<pre><code>';
97+
98+
echo htmlspecialchars(
99+
qp($xml, 'desc')
100+
->attr('class', 'item')
101+
->top() // return to the <categories> tag
102+
->xml() // output a valid XML document. Use ->innerXML() to get the contents of <categories /> instead.
103+
);
104+
105+
echo '</code></pre>';
106+
107+
echo '<h2>Example 2</h2>';
108+
echo 'Use <code>qp($xml)->find(\'categories > category:nth-child(3) desc\')->text();</code> to display the contents of the third <code>&lt;desc&gt;</code>: <br><strong>';
109+
110+
echo qp($xml)
111+
->find('categories > category:nth-child(3) desc')
112+
->text();
113+
114+
echo '</strong>';
115+
116+
echo '<h2>Example 3</h2>';
117+
echo 'Append another category to the XML and output the results:';
118+
echo '<code><pre>';
119+
120+
echo htmlspecialchars(
121+
qp($xml, 'category:last')
122+
->after("\n\n\t<category nam=\"Appended\">\n\t\t<desc>The appended node...</desc>\n\t</category>")
123+
->top()
124+
->xml()
125+
);
126+
127+
echo '</pre></code>';

examples/parsing-xml-from-url-and-dynamically-generating-html/index.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ function addClasses(string $name): string
3333

3434
// Only display jQuery methods from these categories
3535
$categories = [
36-
'traversing/tree-traversal' => 'Tree Traversal',
37-
'selectors/child-filter-selectors' => 'Child Filter',
38-
'selectors/attribute-selectors' => 'Attribute',
36+
'traversing/tree-traversal' => 'Tree Traversal',
37+
'selectors/child-filter-selectors' => 'Child Filter',
38+
'selectors/attribute-selectors' => 'Attribute',
3939
'selectors/content-filter-selector' => 'Content Filter',
40-
'selectors/basic-filter-selectors' => 'Basic Filter',
41-
'selectors/hierarchy-selectors' => 'Hierarchy',
42-
'selectors/basic-css-selectors' => 'Basic',
43-
'traversing/filtering' => 'Filtering',
40+
'selectors/basic-filter-selectors' => 'Basic Filter',
41+
'selectors/hierarchy-selectors' => 'Hierarchy',
42+
'selectors/basic-css-selectors' => 'Basic',
43+
'traversing/filtering' => 'Filtering',
4444
'traversing/miscellaneous-traversal' => 'Miscellaneous Traversing',
45-
'manipulation/dom-insertion-outside' => 'DOM Insertion, Outside',
46-
'manipulation/dom-insertion-inside' => 'DOM Insertion, Inside',
47-
'manipulation/style-properties' => 'Style Properties',
45+
'manipulation/dom-insertion-outside' => 'DOM Insertion, Outside',
46+
'manipulation/dom-insertion-inside' => 'DOM Insertion, Inside',
47+
'manipulation/style-properties' => 'Style Properties',
4848
];
4949

5050
$jquery = [];
@@ -53,11 +53,11 @@ function addClasses(string $name): string
5353
// Search through the xml file to find all entries of jQuery entities
5454
foreach (qp('https://api.jquery.com/resources/api.xml', 'entry') as $entry) {
5555
foreach ($entry->find('category') as $item) {
56-
$category = $categories[ $item->attr('slug') ] ?? '';
56+
$category = $categories[$item->attr('slug')] ?? '';
5757
if ($category) {
58-
$jquery[ $entry->attr('name') ] = [
58+
$jquery[$entry->attr('name')] = [
5959
'longdesc' => $entry->find('longdesc')->innerXML(),
60-
'name' => sprintf('%s: %s', $category, $entry->attr('name')),
60+
'name' => sprintf('%s: %s', $category, $entry->attr('name')),
6161
];
6262

6363
break;
@@ -82,7 +82,7 @@ function addClasses(string $name): string
8282
$qp->top()->find('#rightfunction')->text('Function: ' . ucfirst($key));
8383
$qp->top()->find('#rightdesc')->remove();
8484
$qp->top()->find('#righttitle')->text('jQuery Documentation');
85-
$qp->top()->find('#righttext')->append($jquery[ $key ]['longdesc']);
85+
$qp->top()->find('#righttext')->append($jquery[$key]['longdesc']);
8686

8787
$qp->top()->find('#current-year')->text(date('Y'));
8888

0 commit comments

Comments
 (0)