Commit de556a5a authored by Chenu Denis's avatar Chenu Denis

[release] A tools for other plugin to easily update HTML part (in beforeQuestionRender for example)

parents
# toolsSmartDomDocument
Tool for another plugin : Add some libraries for easiest manipulation on DOM
## Documentation
The primary purpose is to manipulate HTML just before shown. Exemple of usage for beforeQuestionRender {ANSWERS}. Not using regexp, because find id or element in HTML is more stable.
The class can be easily imported using `$dom = new \toolsDomDocument\SmartDOMDocument();`. After 2 function extend php DOMDocument are added.
- loadPartialHTML : to load the HTML of answers
- saveHTMLExact : to get exact HTML updated.
## Copyright
- Copyright © 2016 Denis Chenu <http://sondages.pro>
- Licence : GNU General Public License <https://www.gnu.org/licenses/gpl-3.0.html>
- The class is a fork of SmartDOMDocument © Artem Russakovskii, see <http://beerpla.net> distributed in MIT Licence
<?php
/**
* This class overcomes a few common annoyances with the DOMDocument class,
* such as saving partial HTML without automatically adding extra tags
* and properly recognizing various encodings, specifically UTF-8.
*
* @author Artem Russakovskii
* @author Denis Chenu
* @version 0.5.0
* @link http://beerpla.net
* @link http://www.php.net/manual/en/class.domdocument.php
* @license MIT
*/
namespace toolsDomDocument;
class SmartDOMDocument extends \DOMDocument {
/**
* @var boolean $debug : See warning when loading HTML
*/
public $debug=false;
/**
* Adds an ability to use the SmartDOMDocument object as a string in a string context.
* For example, echo "Here is the HTML: $dom";
*/
public function __toString() {
return $this->saveHTMLExact();
}
/**
* Load HTML with a proper encoding fix/hack.
* Borrowed from the link below.
*
* @link http://www.php.net/manual/en/domdocument.loadhtml.php
*
* @param string $html
* @param string $encoding, default to UTF-8
*
* @return bool
*/
public function loadHTML($html, $encoding = "UTF-8") {
$html = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding);
if(!$this->debug)
return @parent::loadHTML($html); // suppress warnings
else
return parent::loadHTML($html);
}
/**
* Load partial HTML adding a doctype, and an empty head
*
* @see loadHTML
*
* @param string $html
* @param string $doctype, default to html (HTML5)
* @param string $encoding, default to UTF-8
*
* @return bool
*/
public function loadPartialHTML($html, $doctype = 'html', $encoding = "UTF-8") {
$html='<!DOCTYPE '.$doctype.'><html><head></head><body>'.$html.'</body></html>';
return self::loadHTML($html);
}
/**
* Return HTML while stripping the annoying auto-added <html>, <body>, and doctype.
*
* @link http://php.net/manual/en/migration52.methods.php
*
* @return string
*/
public function saveHTMLExact() {
$content = preg_replace(array("/^\<\!DOCTYPE.*?<html><body>/si",
"!</body></html>$!si"),
"",
$this->saveHTML());
return $content;
}
}
<?php
/**
* Tool for another plugin : Add some libraries for easiest manipulation on DOM
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2015-2016 Denis Chenu <http://www.sondages.pro>
* @license GPL v3
* @version 0.1.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
class toolsDomDocument extends PluginBase
{
protected $storage = 'DbStorage';
static protected $name = 'toolsDomDocument';
static protected $description = 'Tools for plugin : SmartDomDocument';
/**
* Add function to be used in beforeQuestionRender event
*/
public function init()
{
$this->subscribe('afterPluginLoad'); /* Seems the quickest way for an helper for other plugin */
}
/**
* Set the alias to get the file
*/
public function afterPluginLoad()
{
Yii::setPathOfAlias('toolsDomDocument', dirname(__FILE__)."/libraries/SmartDOMDocument/");
//~ Yii::import('toolsDomDocument.SmartDOMDocument');
//~ $dom = new \toolsDomDocument\SmartDOMDocument();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment