/** * Tests CDom for simple BBCode parsing * * @covers CDomAttribute * @covers CDomAttributesList * @covers CDomDocument * @covers CDomNode * @covers CDomNodeTag * @covers CDomNodeText * @covers CDomSelector * @covers CDom * @covers CLexer */ function testBBCode() { // Backup default values $bo = CDom::$bracketOpen; $bc = CDom::$bracketClose; $bt = CDom::$blockTags; $it = CDom::$inlineTags; $st = CDom::$selfClosingTags; $bbMarkup = <<<'TXT' [quote] [b]Bold [u]Underline[/u][/b] [i]Italic [/quote] [img width=12 height=16]url[/img] TXT; CDom::$bracketOpen = '['; CDom::$bracketClose = ']'; CDom::$blockTags = array('quote' => true); CDom::$inlineTags = array('b' => true, 'i' => true); CDom::$selfClosingTags = array(); $dom = CDom::fromString($bbMarkup); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals(2, count($dom->children)); $this->assertEquals(4, count($dom->firstChild->nodes)); $this->assertEquals(2, count($dom->firstChild->children)); $b = $dom->find('b'); $expected = '[b]Bold [u]Underline[/u][/b]'; $this->assertEquals($expected, $b->outerHtml()); $img = $dom->lastChild; $img->width = 450; $this->assertEquals('[img width="450" height="16"]url[/img]', $img->outerHtml()); CDom::$bracketOpen = $bo; CDom::$bracketClose = $bc; $expected = '<b>Bold <u>Underline</u></b>'; $this->assertEquals($expected, $b->outerHtml()); $dom->clear(); // Restore CDom::$bracketOpen = $bo; CDom::$bracketClose = $bc; CDom::$blockTags = $bt; CDom::$inlineTags = $it; CDom::$selfClosingTags = $st; }
// --- Extract contents from HTML $html = file_get_contents('http://www.google.com/'); // Dump correctly formatted contents without tags from HTML echo CDom::fromString($html)->text() . "\n"; // --- Use CDom to work with simple BBCode $bbMarkup = <<<'TXT' [quote] [b]Bold [u]Underline[/u][/b] [i]Italic [/quote] [img width=12 height=16]url[/img] TXT; CDom::$bracketOpen = '['; CDom::$bracketClose = ']'; CDom::$blockTags = array('quote' => true); CDom::$inlineTags = array('b' => true, 'i' => true, 'u' => true); CDom::$selfClosingTags = array(); // Create DOM from string $dom = CDom::fromString($bbMarkup); // Find [b] $b = $dom->find('b'); $expected = '[b]Bold [u]Underline[/u][/b]'; echo $b->outerHtml() . "\n"; // Output: [b]Bold [u]Underline[/u][/b] // Change [img] width $img = $dom->lastChild; $img->width = 450; echo $img->outerHtml() . "\n"; // Output: [img width="450" height="16"]url[/img] // Convert [b] to html CDom::$bracketOpen = '<';