PHP Classes

File: reference.js

Recommend this page to a friend!
  Classes of Patricio Cardó   Selector and Validator for XHTML parts   reference.js   Download  
File: reference.js
Role: Auxiliary data
Content type: text/plain
Description: Reference data object
Class: Selector and Validator for XHTML parts
Parse HTML to validate it and extract parts
Author: By
Last change:
Date: 13 years ago
Size: 21,176 bytes
 

Contents

Class file image Download
ref = [ {func:"sacar_comillas", descript:'Remove quotes from a string. It is used within other functions where stripslashes function does not work', usage:'string $xhtml_parte-><b>sacar_comillas</b>( string <i>$str</i>)', parameters:[{param:'str',desc:'the string with quotes to be removed'}], returned:'string without quotes and without escaped internal quotes', examples:[{subt:'Removing quotes from text', code:"&#36;str1 = &#34;'quoted text'&#34;;<br />&#36;str2 = &#36;xhtml_parte->sacar_comillas(&#36;str1);<br />echo &#36;str2; // this will output: quoted text"}]}, {func:"propiedades", descript:'Get all the properties declared in a tag', usage:'array $xhtml_parte-><b>propiedades</b>( string <i>$tag</i>)', parameters:[{param:'tag',desc:'the string with the tag in xhtml code'}], returned:'array width attribute as key and value as array value', examples:[{subt:'Getting properties of a tag', code:'&#36;tag = &#34;&lt;img src=&#39;image.jpg&#39; border=&#39;0&#39; /&gt;&#34;;'+ '<br />print_r(&#36;xhtml_parte->propiedades(&#36;tag));'+ '<br />// Array ( [src] => &#39;image.jpg&#39;, [border] => 0)'}]}, {func:"recomponer", descript:'Restore an property array into a string with xhtml code tag', usage:'array $xhtml_parte-><b>recomponer</b>( string <i>$tagName</i>, array <i>$properties</i> '+ '[, string <i>$quot</i>])', parameters:[{param:'tagName',desc:'the name of the element to represent its opening tag'}, {param:'properties',desc:'Array where key is attribute name and value is the value of that attribute'}, {param:'quot',desc:'Optional. Character to quote values it can be &#39; (default) or &#34;'}], returned:'String with opening tag, or false on error and a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Compose an img tag', code:'&#36;prp = array(&#34;src&#34;=>&#34;file.jpg&#34;, &#34;alt&#34;=>&#34;just an image&#34;);'+ '<br />print &#36;xhtml_parte->recomponer(&#34;img&#34;,&#36prp);'+ '<br />// &lt;img src=&#39;file.jpg&#39; alt=&#39;just an image&#39; /&gt;'}]}, {func:"cambiar_propiedad_dom", descript:'Append, delete or replace an existing tag property in each selected tag', usage:'array $xhtml_parte-><b>cambiar_propiedad_dom</b>( string <i>$xhtml</i>, string <i>$sel_tag</i>, '+ ' string <i>$sel_attribute</i>, string <i>$sel_value</i>, '+ 'string <i>$attribute</i>, mixed <i>$value</i> [,bool <i>$over</i>])', parameters:[{param:'xhtml', desc:'Optional. The string with the xhtml code where fragment will be deleted. '+ 'If it is setted to false, object&#39;s DOM string will be assumed'}, {param:'sel_tag',desc:'The tag where property will be affected. The method selects all &lt;tag...&gt;'+ 'and append/delete/replace property. '+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'sel_attribute',desc:'The attribute that must be in tag where property will be affected. '+ 'The method selects all &lt;tag...attribute...&gt; and append/delete/replace property. '+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'sel_value',desc:'The attribute&#39;value that must be in tag where property will be affected. '+ 'The method selects all &lt;tag...attribute=attribute...&gt; and append/delete/replace property. '+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'attribute',desc:'The attribute to add, delete or replace'}, {param:'value',desc:'The attribute&#39;s value to add or replace. If it is set to FALSE, method will destroy'+ 'the argument in the tag'}, {param:'over',desc:'Optional. If it is TRUE (default), it overwrites the property if it exists'}], returned:'String with xhtml code tag, or false on error and a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Change language in divs', code:'&#36;frgm = &quot;&lt;div lang=&#039;es&#039;&gt;Night is the time&lt;/div&gt;'+ '<br />&lt;div&gt;where fair should be controlled&lt;/div&gt;'+ '<br />&lt;p&gt;But no one do it&lt;/p&gt;&quot;;'+ '<br />&#36;myfragm = new xhtml_parte(&#36;frgm);'+ '<br />print &#36;myfragm->cambiar_propiedad_dom(false, &#039;div&#039;, false, false, &#039;lang&#039;, '+ '&#039;en&#039;);'+ '<br />/* it will return'+ '<br />&lt;div lang=&#039;en&#039;&gt;Night is the time&lt;/div&gt;'+ '<br />&lt;div lang=&#039;en&#039;&gt;where fair should be controlled&lt;/div&gt;'+ '<br />&lt;p&gt;But no one do it&lt;/p&gt;<br />*/'}]}, {func:"cambiar_propiedad", descript:'Append or replace an existing tag property', usage:'array $xhtml_parte-><b>cambiar_propiedad</b>( string <i>$tag</i>, string <i>$attribute</i>, '+ 'string <i>$value</i> [,bool <i>$over</i>])', parameters:[{param:'tag',desc:'the string with the tag in xhtml code'}, {param:'attribute',desc:'The attribute to add, delete or replace'}, {param:'value',desc:'The attribute&#39;s value to add or replace. If it is set to FALSE, method will destroy'+ 'the argument in the tag'}, {param:'over',desc:'Optional. If it is TRUE (default), it overwrites the property if it exists'}], returned:'String with xhtml code tag, or false on error and a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Adding a tabindex to a input tag', code:'&#36;tag = &#34;&lt;input type=&#39;text&#39; id=&#39;inp3&#39; /&gt;&#34;;'+ '<br />&#36;tab = &#34;3&#34;;'+ '<br />print &#36;xhtml_parte->cambiar_propiedad(&#36;tag,&#34;tabindex&#34;,&#36tab);'+ '<br />// &lt;input type=&#39;text&#39; id=&#39;inp3&#39; tabindex=&#39;3&#39; /&gt;'}]}, {func:"proteger_data", descript:'Prevent that string with xhtml code generates troubles in browsers and other functions', usage:'string $xhtml_parte-><b>proteger_data</b>( string <i>$code</i>[, bool <i>$delete</i>])', parameters:[{param:'code',desc:'the string with the xhtml code'},{param:'delete', desc:'Optional. It indicates that comments must be deleted in tge returned string'}], returned:'String with xhtml code with html characters replaced in comments and script contents. It will return false if $code is not a string', examples:[{subt:'Avoiding problem of code contents in parser', code:"&#36;tag1 = &#34;&lt;h1&gt;This is a code in a text&lt;/h1&gt;<br />"+ "&lt;script&gt;<br />&lt;!--<br /> problem_code = '&lt;/script&gt;';<br />//--&gt;<br />&lt;/script&gt;&#34;;"+ "<br />print &#36;xhtml_parte->proteger_data(&#36;tag1);<br />"+ "/* It will output:<br />"+ "&lt;h1&gt;This is a code in a text&lt;/h1&gt;<br />"+ "&lt;script&gt;<br />&amp;lt;!--;<br /> problem_code = "+ "'&amp;lt;/script&amp;gt;';<br />//--&amp;gt;<br />&lt;/script&gt; */"}]}, {func:"insertar", descript:'Inserts xhtml code inside other xhtml code', usage:'string $xhtml_parte-><b>insertar</b>( string <i>$frag</i>[, mixed <i>$position</i> [, string <i>$xhtml</i> [, '+ 'string <i>$tag</i> [, string <i>$attribute</i> [, string <i>$value</i>] ] ] ] ])', parameters:[{param:'frag', desc:'the string with the xhtml code to be inserted'}, {param:'position', desc:'Optional. Integer or boolean FALSE. It indicates from 0 to number of tags in '+ 'first order, the position where the fragment will be inserted. If it is FALSE or a lower value, '+ '$position will be converted to <i>-1</i>, that is, at the end of xhtml code'}, {param:'xhtml', desc:'Optional. The string with the xhtml code where fragment will be inserted. '+ 'If it is not setted, object&#39;s DOM string will be assumed'}, {param:'tag', desc:'Optional. The tag where $fragment will be inserted. The method selects all &lt;tag...&gt;'+ 'and inserts $fragment at the indicated position. '+ 'If it is not setted, all first order elements (tags) will be selected'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'attribute', desc:'Optional. The attribute that must be defined in tags where $fragment will be inserted.'+ ' The method selects all &lt;$tag...$attribute...&gt;'+ 'and inserts $fragment at the indicated position. '+ 'If it is not setted, all first order $tag elements will be selected.'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'value', desc:'Optional. The value of attribute that must be defined in tags where $fragment'+ ' will be inserted.'+ ' The method selects all &lt;$tag...$attribute...&gt;'+ 'and inserts $fragment at the indicated position. '+ '<br />It requires $attribute to be setted. If it is not setted, or it is setted as FALSE or "*"'+ ', all first order $tag elements with $attribute will be selected'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, ], returned:'Xhtml code string with fragment inserted once or in each tag selected at the $position indicated. '+ 'If some error, it will return $xhtml, but it will add a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Appending a row in a table', code:"&#36;xhtml = &#34;&lt;table width=&#39;400&#39;&gt;&lt;tbody&gt;<br />"+ "&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td&gt;This is one row table&lt;/td&gt;&lt;/tr&gt;<br />"+ "&lt;/tbody&gt;&lt;/table&gt;&#34;;"+ "<br />&#36;tag = &#34;&lt;tr&gt;&lt;td&gt;But this is other row&lt;/td&gt;&lt;/tr&gt;&#34;;<br />"+ "print &#36;xhtml_parte->insertar(&#36;tag,-1,&#36;xhtml,&#34;tbody&#34;);<br />"+ "/* It will output:<br />"+ "&lt;table width=&#39;400&#39;&gt;&lt;tbody&gt;<br />"+ "&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td&gt;This is one row table&lt;/td&gt;&lt;/tr&gt;<br />"+ "&lt;tr&gt;&lt;td&gt;But this is other row&lt;/td&gt;&lt;/tr&gt;"+ "&lt;/tbody&gt;&lt;/table&gt; */"}]}, {func:"eliminar", descript:'Deletes tags from xhtml code string', usage:'string $xhtml_parte-><b>eliminar</b>( integer <i>$position</i> [, string <i>$xhtml</i> [, '+ 'string <i>$tag</i> [, string <i>$attribute</i> [, string <i>$value</i>] ] ] ])', parameters:[{param:'position', desc:'It indicates from 0 to number of tags in '+ 'first order, the position of the tag that will be deleted. If it is FALSE or a lower value than 0, '+ '$position will be converted to <i>-1</i>, that is, the final tag'}, {param:'xhtml', desc:'Optional. The string with the xhtml code where fragment will be deleted. '+ 'If it is not setted, object&#39;s DOM string will be assumed'}, {param:'tag', desc:'Optional. The method selects all &lt;$tag...&gt;'+ 'and deletes them. It will annul $position.'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'attribute', desc:'Optional. The attribute that must be defined in tags to be deleted.'+ ' The method selects all &lt;$tag...$attribute...&gt;'+ 'and deletes them. '+ 'If it is not setted, all $tag elements will be deleted.'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, {param:'value', desc:'Optional. The value of attribute that must be defined in tags to be deleted.'+ ' The method selects all &lt;$tag...$attribute=$value...&gt;'+ 'and deletes them. '+ '<br />It requires $attribute to be setted. If it is not setted, or it is setted as FALSE or "*"'+ ', all $tag elements with $attribute will be deleted'+ '<br />See method <a href="#" onclick="show_ref(\'etiquetas\')">etiquetas</a> for more details on this'+ 'parameter'}, ], returned:'Xhtml code string without specific tags. '+ 'If some error, it will return $xhtml, but it will add a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Deleting a row in a table', code:"&#36;xhtml = &#34;&lt;table width=&#39;400&#39;&gt;&lt;tbody&gt;<br />"+ "&nbsp;&nbsp;&nbsp;&lt;tr id=&#39one&#39; &gt;&lt;td&gt;This is the first row in table&lt;/td&gt;&lt;/tr&gt;<br />"+ "&lt;tr id=&#39two&#39&gt;&lt;td&gt;This is the first row in table&lt;/td&gt;&lt;/tr&gt;"+ "&lt;/tbody&gt;&lt;/table&gt;&#34;;"+ "print &#36;xhtml_parte->eliminar(false,&#36;xhtml,&#34;tr&#34;,&#34;id&#34;,&#34;two&#34;);<br />"+ "/* It will output:<br />"+ "&lt;table width=&#39;400&#39;&gt;&lt;tbody&gt;<br />"+ "&nbsp;&nbsp;&nbsp;&lt;tr&gt;&lt;td&gt;This is the first row in table&lt;/td&gt;&lt;/tr&gt;<br />"+ "&lt;/tbody&gt;&lt;/table&gt; */"}]}, {func:"etiquetas", descript:'Selects tags from xhtml code string', usage:'array $xhtml_parte-><b>etiquetas</b>( [ string <i>$xhtml</i> [, '+ 'string <i>$tag</i> [, string <i>$attribute</i> [, string <i>$value</i> [, string <i>$type</i>] ] ] ] ])', parameters:[{param:'xhtml', desc:'Optional. The string with the xhtml code that contains tags to select. '+ 'If it is not setted, object&#39;s DOM string will be assumed'}, {param:'tag', desc:'Optional. The method selects all &lt;$tag...&gt;'+ 'and deletes them. If it is setted as "*" or a non-string value it will assumed "*", that '+ 'means <i>all</i>. If it is prefixed by "!", all tags except indicated will be selected'}, {param:'attribute', desc:'Optional. The attribute that must be defined in tags to be deleted.'+ ' The method selects all &lt;$tag...$attribute...&gt;. '+ ' If it is setted as "*" or a non-string value it will assumed "*", that '+ 'means <i>all</i>. If it is prefixed by "!", all $tags except those with indicated attribute '+ 'will be selected'}, {param:'value', desc:'Optional. The value of attribute that must be defined in tags to be deleted.'+ ' The method selects all &lt;$tag...$attribute=$value...&gt;'+ '<br />It requires $attribute to be setted.'+ ' If it is setted as "*" or a non-string value it will assumed "*", that '+ 'means <i>all</i>. If it is prefixed by "!", all $tags with $attribute except those wich '+ 'indicated value will be selected'}, {param:'type', desc:'Optional. The way that method returns selection.<ul class="normal">'+ '<li>"nido": Will return an array <i>m</i> where:<blockquote class="bquo">'+ '<i>m[0]</i> is an array of whole tag include opening and closing.'+ '<br /><i>m[1]</i> is an array of tag names.'+ '<br /><i>m[2]</i> is an array of tag contents.</blockquote></li>'+ '<li>"cont": Will return an array of tag contents.</li>'+ '<li>"text": Will return an array of tag contents, but with '+ 'html entities tranformed to be displayed.</li>'+ '<li>"decl": Will return an array of opening tags (or complete tags in case of empty tags like '+ 'img or hr).</li>'+ '<li>"cara": Will return an array of tags in array mode, where &#39;tagName&#39; key will correspond'+ 'to the name of the tag and other keys will correspond to each attribute declared.</li>'+ '<li>"nodo": Will return an array of tag declaration, contents and close. '+ 'This is the default parameter value.</li></ul>'}, ], returned:'Array of tag string in a xhtml code string.'+ 'If some error, it will return boolean FALSE, but it will add a <a href="#" onclick="show_ref(\'log\')">log</a> entry', examples:[{subt:'Selecting tags', code:"/* Read several examples in the file selector_examples.php */"}]}, {func:"ver_estructura", descript:'Soft validation of xhtml structure in a string.'+ '<br />It just review if there are forbbiden tags inside other tags (like &lt;a&gt; inside &lt;a&gt;), '+ '<br />Unrecommended xhtml attributes inside some tags, and non repeating tags', usage:'bool $xhtml_parte-><b>ver_estructura</b>( [ string <i>$xhtml</i> ])', parameters:[{param:'xhtml', desc:'Optional. The string with the xhtml code that contains tags to select. '+ 'If it is not setted, object&#39;s DOM string will be assumed'}, ], returned:'Boolean TRUE if no problem was found and FALSE and <a href="#" onclick="show_ref(\'log\')">log</a>'+ ' entries if it detects some faults.', examples:[{subt:'Soft structure validation', code:'&#36;xhtml = &#34;&lt;a href=&#39;nowhere.html&#39;&gt;this is a link but '+ '&lt;a name=&#39;anchorname&#39;&gt; with a bad anchor inside &lt;/a&gt; &lt;/a&gt;&#34;;<br />'+ '&#36;myFragment = new xhtml_parte(&#36;xhtml);<br />'+ 'if(&#36;myFragment->ver_estructura()){<br />'+ '&nbsp;&nbsp;&nbsp;print &#34;validated&#34;;<br />'+ '}else{<br />'+ '&nbsp;&nbsp;&nbsp;print_r(&#36;myFragment->log);'+ '<br />}<br />'+ '/* It will output:<br />'+ 'Array('+ '<br />&nbsp;&nbsp;[0]=>&#34;It is not allowed tags &#39a&#39 inside tags &#39a&#39&#34;,'+ '<br />)<br />*/'}]}, {func:"validar", descript:'Validation of xhtml code in a string.'+ '<br />It checks out if there are unclosed tags and CDATA declarations. '+ '<br />It also performs some recomendations about contents, structure and syntax.', usage:'bool $xhtml_parte-><b>validar</b>( [ string <i>$xhtml</i> ])', parameters:[{param:'xhtml', desc:'Optional. The string with the xhtml code that contains tags to select. '+ 'If it is not setted, object&#39;s DOM string will be assumed'}, ], returned:'Boolean TRUE if no problem was found; FALSE and <a href="#" onclick="show_ref(\'log\')">log</a>'+ ' entries if it detects some parse problems; it also returns TRUE and log entries if it has some'+ 'recomendations', examples:[{subt:'Selecting tags', code:"/* Read several examples in the file good_bad_examples.php */"}]}, {func:"log", descript:'Object Property. Contains exceptions in object.'+ '<br />It is filled by methods.', usage:'array $xhtml_parte-><b>log</b>', parameters:[], returned:'Array with log entries. If you are interested in translate it , phrases that originates entries'+ ' starts in line 70 of xhtml_parte.php and they are used with <a href="#" onclick="show_ref(\'f\')">f</a>. array', examples:[]}, {func:"f", descript:'Object Property. Contains phrases used in log entries.'+ '<br />It is filled by constructor.', usage:'array $xhtml_parte-><b>f</b>[phrase_name]', parameters:[], returned:'Array with phrases that perform log entries. They starts in line 70 of xhtml_parte.php', examples:[]}, {func:"dom", descript:'Object Property. Contains the main string of xhtml code.'+ '<br />It is filled by constructor.', usage:'array $xhtml_parte-><b>dom</b>', parameters:[], returned:'String with xhtml code passed to constructor during object declaration: '+ '<i>$myFrag = new xhtml_parte(<b>$xhtml_code_string</b>)</i>', examples:[]}, {func:"TO DO", descript:'Some ideas to improve the class.', usage:'Email me to patocardo@gmail.com and recommend me improvements', parameters:[], returned:'Better xhtml selection and validation', examples:[{subt:'In future versions I may add', code:'<ul class="normal"><li><b>&#34;tree&#34;</b> as <i>etiquetas</i> parameter. '+ 'In order to return dom tree as multi-level array</li>'+ '<li><b>Position</b> of tag. Much needed in validation, with row and column in the string'+ ' not realize that if there are more closing tags that needed</li>'+ '<li><b>Stricter</b> structure validation. That returns perhaps, if tables are properly setted</li>'+ '<li><b>Unique</b> insertion. That only inserts if the fragment does not '+ 'exists in the xhtml code string</li>'+ '<li><b>More flexible</b> selection. That allows, perhaps, to select tags with &#39;tabindex&#39; '+ 'property higher than 3</li>'+ '</ul>'}]}, {func:"pc_matriz_dif", descript:'External function. It calculates the differences between '+ 'two arrays and returns the difference.'+ '<br /><strong>Unlike array_diff() It computes repeated values</strong>.'+ '<br />It is declared when the xhtml_parte.php file is included.', usage:'array <b>pc_matriz_dif</b>( array <i>$arr1</i>, array <i>$arr2</i>)', parameters:[{param:'arr1', desc:'First array to compare'}, {param:'arr2', desc:'Second array to compare'}], returned:'Array with values that are not repeated between arr1 and arr2', examples:[{subt:'Which tag is missing', code:'&#36;arr1 = array(&#34;table&#34;,&#34;tr&#34;,&#34;td&#34;,&#34;td&#34;);'+ '<br />&#36;arr2 = array(&#34;table&#34;,&#34;tr&#34;,&#34;td&#34;);'+ '<br />print_r(pc_matriz_dif(&#36;arr1,&#36;arr2));'+ '<br />// Array (&#34;td&#34;)'}]}, ];