Permalink
Browse files

Put in setting switch on simpletest to allow parser selection.

  • Loading branch information...
1 parent 72baf80 commit efd897fb267cc43f7cd16aa66d4b678b81214c08 dgheath committed Jul 31, 2009
Showing with 65 additions and 15 deletions.
  1. +9 −9 browser.php
  2. +17 −1 php_parser.php
  3. +25 −0 simpletest.php
  4. +14 −5 tidy_parser.php
View
@@ -18,6 +18,9 @@
require_once(dirname(__FILE__) . '/selector.php');
require_once(dirname(__FILE__) . '/frames.php');
require_once(dirname(__FILE__) . '/user_agent.php');
+if (! SimpleTest::getPageBuilders()) {
+ SimpleTest::setPageBuilders(array(new SimplePHPPageBuilder()));
+}
/**#@-*/
if (!defined('DEFAULT_MAX_NESTED_FRAMES')) {
@@ -210,14 +213,15 @@ protected function createHistory() {
* parser is used.
* @return SimplePHPPageBuilder or SimpleTidyPageBuilder
*/
- protected function createBuilder() {
+ protected function getBuilder() {
if ($this->builder) {
return $this->builder;
}
- if (false and extension_loaded('tidy')) {
- return new SimpleTidyPageBuilder();
+ foreach (SimpleTest::getPageBuilders() as $builder) {
+ if ($builder->can()) {
+ return $builder;
+ }
}
- return new SimplePHPPageBuilder();
}
/**
@@ -292,11 +296,7 @@ protected function parse($response, $depth = 0) {
* @return SimplePage Parsed top level page.
*/
protected function buildPage($response) {
- $builder = $this->createBuilder();
- $page = $builder->parse($response);
- $builder->free();
- unset($builder);
- return $page;
+ return $this->getBuilder()->parse($response);
}
/**
View
@@ -708,6 +708,20 @@ function free() {
unset($this->tags);
unset($this->page);
unset($this->private_content_tags);
+ $this->open_forms = array();
+ $this->complete_forms = array();
+ $this->frameset = false;
+ $this->loading_frames = array();
+ $this->frameset_nesting_level = 0;
+ $this->left_over_labels = array();
+ }
+
+ /**
+ * This builder is always available.
+ * @return boolean Always true.
+ */
+ function can() {
+ return true;
}
/**
@@ -723,7 +737,9 @@ function parse($response) {
$parser = $this->createParser($this);
$parser->parse($response->getContent());
$this->acceptPageEnd();
- return $this->page;
+ $page = $this->page;
+ $this->free();
+ return $page;
}
/**
View
@@ -175,6 +175,30 @@ static function getDefaultProxyPassword() {
}
/**
+ * Accessor for default page builders.
+ * These are actually parsers of the HTML.
+ * @return array List of parsers to try in
+ * order until one responds true
+ * to can().
+ */
+ static function getPageBuilders() {
+ $registry = &SimpleTest::getRegistry();
+ return $registry['PageBuilders'];
+ }
+
+ /**
+ * Set the list of page builders.
+ * These are actually parsers of the HTML.
+ * @param array $builders List of parsers to try in
+ * order until one responds true
+ * to can().
+ */
+ static function setPageBuilders($builders) {
+ $registry = &SimpleTest::getRegistry();
+ $registry['PageBuilders'] = $builders;
+ }
+
+ /**
* Accessor for global registry of options.
* @return hash All stored values.
* @access private
@@ -208,6 +232,7 @@ static function getContext() {
*/
protected static function getDefaults() {
return array(
+ 'PageBuilders' => false,
'MockBaseClass' => 'SimpleMock',
'IgnoreList' => array(),
'DefaultProxy' => false,
View
@@ -23,12 +23,19 @@ public function __destruct() {
/**
* Frees up any references so as to allow the PHP garbage
* collection from unset() to work.
- * @access public
*/
- public function free() {
+ private function free() {
unset($this->page);
- unset($this->forms);
- unset($this->labels);
+ $this->forms = array();
+ $this->labels = array();
+ }
+
+ /**
+ * This builder is only available if the 'tidy' extension is loaded.
+ * @return boolean True if available.
+ */
+ function can() {
+ return extension_loaded('tidy');
}
/**
@@ -47,7 +54,9 @@ function parse($response) {
}
$this->walkTree($tidied->html());
$this->page->setForms($this->attachLabels($this->forms, $this->labels));
- return $this->page;
+ $page = $this->page;
+ $this->free();
+ return $page;
}
/**

0 comments on commit efd897f

Please sign in to comment.