From f9a9b7012397ec4c4a18f49352d919266816dbd7 Mon Sep 17 00:00:00 2001 From: Domovoy Date: Fri, 14 Sep 2012 05:30:21 +0200 Subject: [PATCH 1/3] Detect the namespace uri from token name --- library/HTML5/TreeBuilder.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/library/HTML5/TreeBuilder.php b/library/HTML5/TreeBuilder.php index 578e73682..26cd410bf 100644 --- a/library/HTML5/TreeBuilder.php +++ b/library/HTML5/TreeBuilder.php @@ -3037,8 +3037,22 @@ class HTML5_TreeBuilder { } private function insertElement($token, $append = true) { - $el = $this->dom->createElementNS(self::NS_HTML, $token['name']); - + $sep_pos = strpos($token['name'],':'); + $ns_uri = self::NS_HTML; + if($sep_pos !== FALSE) { + // This tag has a namespace + $ns = substr($token['name'], 0, $sep_pos); + switch($ns) { + case 'g': + $ns_uri = 'http://base.google.com/ns/1.0'; + break; + default: + logger("HTML5/TreeBuilder.php: Unknown namespace '". $ns ."', tag = ". $token['name'], LOGGER_DEBUG); + break; + } + } + $el = $this->dom->createElementNS($ns_uri, $token['name']); + if (!empty($token['attr'])) { foreach($token['attr'] as $attr) { From 09413a0f0a04ad84c9869fc8e9f8fbd75317d35e Mon Sep 17 00:00:00 2001 From: Domovoy Date: Fri, 14 Sep 2012 05:50:35 +0200 Subject: [PATCH 2/3] Better integration with the existing code --- library/HTML5/TreeBuilder.php | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/library/HTML5/TreeBuilder.php b/library/HTML5/TreeBuilder.php index 26cd410bf..257e73c3e 100644 --- a/library/HTML5/TreeBuilder.php +++ b/library/HTML5/TreeBuilder.php @@ -127,6 +127,7 @@ class HTML5_TreeBuilder { const NS_XLINK = 'http://www.w3.org/1999/xlink'; const NS_XML = 'http://www.w3.org/XML/1998/namespace'; const NS_XMLNS = 'http://www.w3.org/2000/xmlns/'; + const NS_GOOGLE = 'http://base.google.com/ns/1.0'; public function __construct() { $this->mode = self::INITIAL; @@ -1430,6 +1431,14 @@ class HTML5_TreeBuilder { // parse error break; + /* Google */ + case 'g:plusone': + /* Reconstruct the active formatting elements, if any. */ + $this->reconstructActiveFormattingElements(); + + $this->insertForeignElement($token, self::NS_GOOGLE); + break; + /* A start tag token not covered by the previous entries */ default: /* Reconstruct the active formatting elements, if any. */ @@ -3037,21 +3046,7 @@ class HTML5_TreeBuilder { } private function insertElement($token, $append = true) { - $sep_pos = strpos($token['name'],':'); - $ns_uri = self::NS_HTML; - if($sep_pos !== FALSE) { - // This tag has a namespace - $ns = substr($token['name'], 0, $sep_pos); - switch($ns) { - case 'g': - $ns_uri = 'http://base.google.com/ns/1.0'; - break; - default: - logger("HTML5/TreeBuilder.php: Unknown namespace '". $ns ."', tag = ". $token['name'], LOGGER_DEBUG); - break; - } - } - $el = $this->dom->createElementNS($ns_uri, $token['name']); + $el = $this->dom->createElementNS(self::NS_HTML, $token['name']); if (!empty($token['attr'])) { foreach($token['attr'] as $attr) { From a31c779a15e0457054b8a2d02569dda5ab8fe9b4 Mon Sep 17 00:00:00 2001 From: Domovoy Date: Fri, 14 Sep 2012 07:59:54 +0200 Subject: [PATCH 3/3] Transform prefixed elements to classic ones --- library/HTML5/TreeBuilder.php | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/library/HTML5/TreeBuilder.php b/library/HTML5/TreeBuilder.php index 257e73c3e..2a789f4df 100644 --- a/library/HTML5/TreeBuilder.php +++ b/library/HTML5/TreeBuilder.php @@ -127,7 +127,6 @@ class HTML5_TreeBuilder { const NS_XLINK = 'http://www.w3.org/1999/xlink'; const NS_XML = 'http://www.w3.org/XML/1998/namespace'; const NS_XMLNS = 'http://www.w3.org/2000/xmlns/'; - const NS_GOOGLE = 'http://base.google.com/ns/1.0'; public function __construct() { $this->mode = self::INITIAL; @@ -159,6 +158,8 @@ class HTML5_TreeBuilder { if ($this->ignore_lf_token) $this->ignore_lf_token--; $this->ignored = false; + + $token['name'] = str_replace(':', '-', $token['name']); // indenting is a little wonky, this can be changed later on switch ($mode) { @@ -1430,15 +1431,7 @@ class HTML5_TreeBuilder { case 'tbody': case 'td': case 'tfoot': case 'th': case 'thead': case 'tr': // parse error break; - - /* Google */ - case 'g:plusone': - /* Reconstruct the active formatting elements, if any. */ - $this->reconstructActiveFormattingElements(); - - $this->insertForeignElement($token, self::NS_GOOGLE); - break; - + /* A start tag token not covered by the previous entries */ default: /* Reconstruct the active formatting elements, if any. */