/** * Tests manipulation * * @covers CDomDocument * @covers CDomAttributesList * @covers CDomAttribute * @covers CDomList * @covers CDomNode * @covers CDomNodeCdata * @covers CDomNodeCommment * @covers CDomNodeDoctype * @covers CDomNodesList * @covers CDomNodeTag * @covers CDomNodeText * @covers CDomNodeXmlDeclaration * @covers CDom */ function testManipulation() { $dom = CDom::fromString($this->test_html); $this->checkStructure($dom); $div1 = $dom->getElementByTagName('div'); // Clone $div2 = $div1->clone(); $this->checkStructure($div2); $this->assertNotEquals($div1->uniqId, $div2->uniqId); $this->assertEmpty($div2->parent); $this->assertNotEmpty($div2->ownerDocument); $test = 1234; $div2->attr('n', $test); $this->assertNotEquals($test, (string) $div1->attr('n')); $this->assertEquals($test, (string) $div2->attr('n')); $list = $dom->find('div'); $list2 = $list->clone(); $this->assertEquals($list->length, $list2->length); $this->assertNotEquals($list[0]->uniqId, $list2[0]->uniqId); $this->assertEmpty($list2[0]->parent); $dom2 = CDom::fromString('<div/>text<br>'); $dom2 = clone $dom2; $this->checkStructure($dom2); $dom2->clear(); $dom2 = CDom::fromString('<div/>text<br>'); $dom2 = clone $dom2; $this->checkStructure($dom2); $dom2->clear(); // Empty $this->assertEquals(3, count($list->children)); $this->assertEquals(7, count($list->nodes)); $this->assertNotEmpty($list->firstChild); $this->assertNotEmpty($list->lastChild); $list->empty(); $this->assertEquals(0, count($list->children)); $this->assertEquals(0, count($list->nodes)); $this->assertEmpty($list->firstChild); $this->assertEmpty($list->lastChild); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertNotEmpty($dom->firstChild); $this->assertNotEmpty($dom->lastChild); $dom->empty(); $this->assertEquals(0, count($dom->nodes)); $this->checkStructure($dom); // Html $html = '<b>test</b>'; $dom->html($html); $this->checkStructure($dom); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertNotEmpty($dom->firstChild); $this->assertNotEmpty($dom->lastChild); $this->assertEquals($html, $dom->html()); $b = $dom->firstChild; $this->assertEquals('b', $b->name); $this->assertEquals(0, count($b->children)); $this->assertEquals(1, count($b->nodes)); $text = $b->firstNode; $this->assertEquals(CDom::NODE_TEXT, $text->type); $text->html('<br>'); $this->assertEquals(0, count($text->children)); $this->assertEquals(0, count($text->nodes)); $this->assertEquals('<br>', $text->value); $list = array(new CDomNodeTag('br', true), new CDomNodeText('text'), new CDomNodeTag('b')); $dom->html($list); $this->checkStructure($dom); $this->assertEquals(2, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals('<br />text<b></b>', $dom->html()); $dom->html(''); $this->assertEquals(0, count($dom->children)); $this->assertEquals(0, count($dom->nodes)); $this->checkStructure($dom); $nlist = new CDomNodesList(); $nlist->add(array(new CDomNodeTag('br', true), new CDomNodeText('text'), new CDomNodeTag('b'))); $dom->html($nlist); $this->checkStructure($dom); $this->assertEquals(2, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals('<br />text<b></b>', $dom->html()); $this->assertEquals('text', $dom->text()); $br = new CDomNodeTag('br'); $this->assertTrue($br->selfClosed); $br->html('<p>example</p>'); $this->assertFalse($br->selfClosed); $this->assertEquals(1, count($br->children)); $this->assertEquals(1, count($br->nodes)); $this->assertEquals('<p>example</p>', $br->html()); $this->assertEquals('example', $br->text()); $this->assertEquals('<br><p>example</p></br>', $br->outerHtml()); // Text $html = '<b>test</b>'; $dom->empty()->text($html); $this->checkStructure($dom); $this->assertEquals(0, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals($html, $dom->text()); $list = array(new CDomNodeTag('br', true), new CDomNodeText('text'), new CDomNodeTag('b')); $node = new CDomNodeTag('p'); $node->text($list); $this->assertEquals(0, count($node->children)); $this->assertEquals(1, count($node->nodes)); $this->assertEquals('text', $node->text()); $this->assertEquals('text', $node->html()); $nlist = new CDomNodesList(); $nlist->add(array(new CDomNodeTag('br', true), new CDomNodeText('text'), new CDomNodeTag('b'))); $node->empty()->text($nlist); $this->assertEquals(0, count($node->children)); $this->assertEquals(1, count($node->nodes)); $this->assertEquals('text', $node->text()); $this->assertEquals('text', $node->html()); $node->empty()->text(new CDomNodeTag('br')); $this->assertEquals(0, count($node->children)); $this->assertEquals(1, count($node->nodes)); $this->assertEquals('', $node->text()); $this->assertEquals('', $node->html()); $text = 'example'; $tnode = new CDomNodeText($text); $node->text($tnode); $this->assertEquals(0, count($node->children)); $this->assertEquals(1, count($node->nodes)); $this->assertEquals($text, $node->text()); $this->assertEquals($text, $node->html()); $this->assertEquals($tnode->parent, $node); $this->assertEquals($tnode->ownerDocument, $node->ownerDocument); $tnode->text($node); $this->assertEquals(0, count($tnode->children)); $this->assertEquals(0, count($tnode->nodes)); $this->assertEquals($text, $tnode->text()); $this->assertEquals($text, $tnode->html()); $tnode->text($list); $this->assertEquals(0, count($tnode->children)); $this->assertEquals(0, count($tnode->nodes)); $this->assertEquals('text', $tnode->text()); $this->assertEquals('text', $tnode->html()); $tnode->text($nlist); $this->assertEquals(0, count($tnode->children)); $this->assertEquals(0, count($tnode->nodes)); $this->assertEquals('text', $tnode->text()); $this->assertEquals('text', $tnode->html()); $br = new CDomNodeTag('br'); $this->assertTrue($br->selfClosed); $br->text('example'); $this->assertFalse($br->selfClosed); $this->assertEquals(0, count($br->children)); $this->assertEquals(1, count($br->nodes)); $this->assertEquals('example', $br->html()); $this->assertEquals('example', $br->text()); // Detach $dom->html('<br>text<br>text<br>text'); $this->assertEquals(3, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $this->checkStructure($dom); $cnid = -1; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $d = $dom->nodes[2]; $this->assertNotEmpty($d->next); $this->assertNotEmpty($d->prev); $this->assertNotEmpty($d->parent); $d->detach(); $this->assertEmpty($d->next); $this->assertEmpty($d->prev); $this->assertEmpty($d->parent); $this->assertEquals(-1, $d->cnid); $this->assertEquals(-1, $d->chid); $this->checkStructure($dom); $this->assertEquals(2, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $cnid = -1; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $dom->html('<br>'); $dom->firstChild->detach(); $this->assertEquals(0, count($dom->children)); $this->assertEquals(0, count($dom->nodes)); $this->checkStructure($dom); $dom->html('<br>text<br>text<br>text'); $dom->nodes[1]->detach(); $this->assertEquals(3, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $this->checkStructure($dom); $list = $dom->find('br')->detach(); $this->assertEquals(0, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals(3, $list->length); $dom->append($list); $this->assertEquals('texttext<br /><br /><br />', $dom->html()); // Remove $dom->html('<p><br>text</p>'); $dom->firstChild->firstNode->remove(); $this->checkStructure($dom); $dom->firstChild->firstNode->remove(); $this->checkStructure($dom); $this->assertEquals(0, count($dom->firstChild->children)); $this->assertEquals(0, count($dom->firstChild->nodes)); $list = $dom->html('<br>text<br>text<br>text')->find('br')->remove(); $this->assertEquals(0, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals(0, $list->length); // Append $dom->empty()->append($dom); $this->assertEquals(0, count($dom->children)); $this->assertEquals(0, count($dom->nodes)); $dom2 = CDom::fromString('<div/>text<br>'); $this->checkStructure($dom); $this->assertEquals(2, count($dom2->children)); $this->assertEquals(3, count($dom2->nodes)); $div = $dom2->firstChild; $this->assertEquals('div', $div->name); $this->assertEquals($dom2, $div->ownerDocument); $this->assertEquals($dom2, $div->parent); $uid = $div->uniqId; $dom->empty()->append($dom2); $this->checkStructure($dom); $this->assertEquals(0, count($dom2->children)); $this->assertEquals(0, count($dom2->nodes)); $this->assertEmpty($dom2->firstChild); $this->assertEmpty($dom2->lastChild); $this->assertNotEquals($dom2, $div->ownerDocument); $dom2->clear(); $this->assertEquals(2, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals('div', $dom->firstChild->name); $this->assertEquals($dom, $dom->firstChild->ownerDocument); $this->assertEquals($dom, $div->ownerDocument); $this->assertEquals($dom, $div->parent); $this->assertEquals($uid, $div->uniqId); $dom->empty()->append('text'); $this->assertEquals(0, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $dom2 = CDom::fromString('<div>text</div>'); $res = $dom->empty()->append($dom2); $this->assertTrue($res instanceof CDomDocument); $this->assertEquals($dom->uniqId, $res->uniqId); $this->checkStructure($dom); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals($dom, $dom->firstChild->firstNode->ownerDocument); $dom2->clear(); $dom2 = CDom::fromString('<div>text</div>'); $list = $dom->html('<p/><p/><p/>')->find('p'); $this->assertEquals(3, $list->length); $list->append($dom2); $this->assertEquals(3, $list->length); $this->assertEmpty($dom2->nodes); $this->checkStructure($dom2); $this->checkStructure($dom); $this->assertEquals(3, count($dom->nodes)); $expected = '<p><div>text</div></p><p><div>text</div></p><p><div>text</div></p>'; $this->assertEquals($expected, $dom->html()); $dom2->clear(); // Prepend $dom->empty()->prepend('text'); $this->assertEquals(0, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('text', $dom->nodes[0]->value); $dom->prepend('string'); $this->assertEquals(0, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals('string', $dom->nodes[0]->value); $this->assertEquals('text', $dom->nodes[1]->value); $list = $dom->html('<p>l</p><p>l</p>')->find('p'); $this->assertEquals(2, $list->length); $list->prepend('<div>text</div>'); $this->assertEquals(2, $list->length); $this->checkStructure($dom); $this->assertEquals(2, count($dom->nodes)); $expected = '<p><div>text</div>l</p><p><div>text</div>l</p>'; $this->assertEquals($expected, $dom->html()); // After $string = str_repeat('<b/>text', 5); $dom->html($string); $this->checkStructure($dom); $this->assertEquals(5, count($dom->children)); $this->assertEquals(10, count($dom->nodes)); $cnid = 2; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->nodes[3]->after('string<i/>'); $this->checkStructure($dom); $this->assertEquals(6, count($dom->children)); $this->assertEquals(12, count($dom->nodes)); $cnid = 2; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('i', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->html($string); $dom->nodes[4]->after('<i/>string'); $this->checkStructure($dom); $this->assertEquals(6, count($dom->children)); $this->assertEquals(12, count($dom->nodes)); $cnid = 3; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('i', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $dom->html('string'); $dom->lastNode->after('<i/>'); $this->checkStructure($dom); $dom->html('string'); $dom->lastNode->after('text'); $this->checkStructure($dom); $dom->html('string<br/>'); $dom2 = CDom::fromString('string'); $dom->lastNode->after($dom2->nodes); $this->assertEquals(1, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->checkStructure($dom); $this->assertEquals(0, count($dom2->children)); $this->assertEquals(0, count($dom2->nodes)); $this->assertEmpty($dom2->firstChild); $this->assertEmpty($dom2->lastChild); $this->checkStructure($dom2); $dom->html('<br/>'); $dom->lastNode->after('<br/>'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->checkStructure($dom); $dom->html('string<!-- comment --><br/>'); $dom2 = CDom::fromString('string'); $dom->nodes[0]->after($dom2->nodes); $this->assertEquals(1, count($dom->children)); $this->assertEquals(4, count($dom->nodes)); $this->checkStructure($dom); $list = $dom->html('<p/><p/><p/>')->find('p'); $list->after('<br>'); $this->checkStructure($dom); $this->assertEquals(6, count($dom->nodes)); $expected = '<p /><br /><p /><br /><p /><br />'; $this->assertEquals($expected, $dom->html()); // Before $string = str_repeat('<b/>text', 4); $dom->html($string); $this->assertEquals(4, count($dom->children)); $this->assertEquals(8, count($dom->nodes)); $cnid = 1; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->nodes[3]->before('<i/>string'); $this->checkStructure($dom); $this->assertEquals(5, count($dom->children)); $this->assertEquals(10, count($dom->nodes)); $cnid = 1; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('i', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->html('<br>'); $dom->nodes[0]->before('<br>'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->checkStructure($dom); $list = $dom->html('<p/><p/><p/>')->find('p'); $list->before('<br>'); $this->checkStructure($dom); $this->assertEquals(6, count($dom->nodes)); $expected = '<br /><p /><br /><p /><br /><p />'; $this->assertEquals($expected, $dom->html()); // ReplaceWith $string = str_repeat('text<b/>', 3); $dom->html($string); $this->assertEquals(3, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $dom->nodes[3]->replaceWith('string'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $this->checkStructure($dom); $cnid = 1; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $dom->html($string); $dom->nodes[2]->replaceWith('<i/>string<i/>'); $this->assertEquals(5, count($dom->children)); $this->assertEquals(8, count($dom->nodes)); $this->checkStructure($dom); $cnid = 0; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('i', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('i', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->html($string); $dom->nodes[0]->replaceWith('<i/>string<i/>'); $this->assertEquals(5, count($dom->children)); $this->assertEquals(8, count($dom->nodes)); $this->checkStructure($dom); $dom->html($string); $dom->nodes[0]->replaceWith('<i/>'); $this->assertEquals(4, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $this->checkStructure($dom); $dom->html($string); $dom->nodes[1]->replaceWith('text'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $this->checkStructure($dom); $dom->html($string); $dom->nodes[5]->replaceWith('string'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(6, count($dom->nodes)); $this->checkStructure($dom); $dom->html('text'); $dom->nodes[0]->replaceWith('<br>'); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->checkStructure($dom); $dom->html('<br>'); $dom->nodes[0]->replaceWith('text'); $this->assertEquals(0, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->checkStructure($dom); $list = $dom->html('<p/><p/><p/>')->find('p'); $list->replaceWith('<br>'); $this->checkStructure($dom); $this->assertEquals(3, count($dom->nodes)); $expected = '<br /><br /><br />'; $this->assertEquals($expected, $dom->html()); // ReplaceAll $string = str_repeat('text<b/>', 3); $dom->html($string); $res = $dom->nodes[2]->replaceAll('b:eq(1)'); $this->assertEquals($dom->nodes[2]->uniqId, $res[0]->uniqId); $this->assertEquals(2, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $this->checkStructure($dom); $cnid = 0; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $div = new CDomNodeTag('div'); $res = $div->replaceAll('b:eq(1)'); $this->assertEquals($div, $res); $dom->html($string); $dom->nodes[2]->replaceAll($dom->nodes[3]); $this->assertEquals(2, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $this->checkStructure($dom); $cnid = 0; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->html($string); $dom->nodes[2]->replaceAll(array($dom->nodes[3], $dom->nodes[3])); $this->assertEquals(2, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $this->checkStructure($dom); $cnid = 0; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $dom->html($string); $dom->nodes[2]->value = 'ex'; $res = $dom->nodes[2]->replaceAll(array($dom->nodes[3], $dom->nodes[4])); $this->assertEquals(2, $res->length); $this->assertEquals(2, count($dom->children)); $this->assertEquals(5, count($dom->nodes)); $this->checkStructure($dom); $cnid = 0; $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $this->assertEquals('ex', $e->value); $this->assertNotEquals($e->uniqId, $dom->nodes[$cnid + 1]->uniqId); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_TEXT, $e->type); $this->assertEquals('ex', $e->value); $e = $dom->nodes[++$cnid]; $this->assertEquals(CDom::NODE_ELEMENT, $e->type); $this->assertEquals('b', $e->name); $list = $dom->html('<i/><b/><u/>')->find('*'); $dom2 = CDom::fromString('<br/><t/>'); $list2 = $dom2->find('*')->replaceAll($list); $this->assertEmpty($dom2->nodes); $this->assertEquals(6, $list2->length); $this->checkStructure($dom); $this->checkStructure($dom2); $expected = '<br /><t /><br /><t /><br /><t />'; $this->assertEquals($expected, $dom->html()); $list2->remove(); $expected = ''; $this->assertEquals($expected, $dom->html()); $this->checkStructure($dom); // Unwrap $dom->html('<p>string<br/></p>text'); $str = $dom->firstChild->firstNode; $uid = $str->uniqId; $str->unwrap(); $this->checkStructure($dom); $this->assertEquals(0, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals('string', $dom->firstNode->value); $this->assertEquals(CDom::NODE_TEXT, $dom->firstNode->type); $this->assertEquals($uid, $dom->firstNode->uniqId); $list = $dom->html('<p><i/></p><p><i/></p>')->find('i')->unwrap(); $this->assertEquals(2, $list->length); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->checkStructure($dom); $expected = '<i /><i />'; $this->assertEquals($expected, $dom->html()); // Wrap $dom->html('<br/><br/><br/>'); $i = 0; $uid = $dom->children[$i]->uniqId; $dom->children[$i]->wrap('<p/>'); $this->assertEquals(3, count($dom->children)); $this->assertEquals(3, count($dom->nodes)); $this->checkStructure($dom); $this->assertEquals('p', $dom->children[$i]->name); $this->assertEquals('br', $dom->children[$i]->firstChild->name); $this->assertEquals($uid, $dom->children[$i]->firstChild->uniqId); $this->assertFalse($dom->children[$i]->selfClosed); $div = new CDomNodeTag('div'); $div->wrap('<p/>'); $this->assertEmpty($div->parent); $dom->wrap('<p/>'); $this->assertEmpty($dom->parent); $dom2 = CDom::fromString(); $dom->html('<br/><br/><br/>'); $i = 1; $uid = $dom->children[$i]->uniqId; $dom->children[$i]->wrap($dom2); $this->assertEquals($uid, $dom->children[$i]->uniqId); $this->assertEquals('br', $dom->children[$i]->name); $text = new CDomNodeText(); $dom->html('<br/>'); $dom->firstChild->wrap($text); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('br', $dom->firstChild->name); $this->assertEmpty($text->parent); $list = new CDomNodesList(); $list->add(new CDomNodeTag('p')); $list->add(new CDomNodeTag('div')); $dom->html('<br/>'); $dom->firstChild->wrap($list); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('p', $dom->firstChild->name); $this->assertEmpty($list->get(1)->parent); $list = new CDomNodesList(); $dom->html('<br/>'); $dom->firstChild->wrap($list); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('br', $dom->firstChild->name); $dom->html('<br/>'); $dom->firstChild->wrap(array()); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('br', $dom->firstChild->name); $list = array(new CDomNodeTag('div'), new CDomNodeTag('p')); $dom->html('<br/>'); $dom->firstChild->wrap($list); $this->assertEquals(1, count($dom->children)); $this->assertEquals(1, count($dom->nodes)); $this->assertEquals('div', $dom->firstChild->name); $this->assertEquals('br', $dom->firstChild->firstChild->name); $this->checkStructure($dom); $dom->html('<br/><p/>'); $dom->firstChild->wrap('p'); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals('p', $dom->firstChild->name); $this->assertEquals('br', $dom->firstChild->firstChild->name); $this->assertNotEquals($dom->lastChild->uniqId, $dom->firstChild->uniqId); $this->checkStructure($dom); $list = $dom->html('<p/><p/><p/>')->find('p'); $list->wrap('<div>'); $this->checkStructure($dom); $this->checkList($list); $this->assertEquals(3, count($dom->nodes)); $this->assertEquals(3, $list->length); $this->assertEquals('p', $list[0]->name); $expected = '<div><p /></div><div><p /></div><div><p /></div>'; $this->assertEquals($expected, $dom->html()); $list[0]->name = 'b'; $expected = '<div><b /></div><div><p /></div><div><p /></div>'; $this->assertEquals($expected, $dom->html()); // WrapInner $text = new CDomNodeText(); $text->wrapInner('<p>'); $this->assertEmpty($text->children); $this->assertEmpty($text->nodes); $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; $this->assertEquals(1, count($div->children)); $this->assertEquals(2, count($div->nodes)); $div->wrapInner('<p>'); $this->assertEquals(1, count($div->children)); $this->assertEquals(1, count($div->nodes)); $this->checkStructure($div); $p = $div->firstChild; $this->assertEquals('p', $p->name); $this->assertEquals(1, count($p->children)); $this->assertEquals(2, count($p->nodes)); $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; $this->assertEquals(1, count($div->children)); $this->assertEquals(2, count($div->nodes)); $div->wrapInner('<>'); $this->assertEquals(1, count($div->children)); $this->assertEquals(2, count($div->nodes)); $p = $div->firstChild; $this->assertEquals('br', $p->name); $list = $dom->html('<p>l</p><p><br/></p>')->find('p'); $list->wrapInner('<b>'); $this->checkStructure($dom); $this->checkList($list); $this->assertEquals(2, $list->length); $expected = '<p><b>l</b></p><p><b><br /></b></p>'; $this->assertEquals($expected, $dom->html()); // WrapAll $list = $dom->html('<p/><l>t</l><l/><b/><i>')->find('l,i'); $this->assertEquals(3, $list->length); $list->wrapAll('<div>'); $this->checkStructure($dom); $this->checkList($list); $expected = '<p /><div><l>t</l><l /><i></i></div><b />'; $this->assertEquals($expected, $dom->html()); // AppendTo $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; CDom::fromString('<p>')->appendTo($div); $this->assertEquals(2, count($div->children)); $this->assertEquals(3, count($div->nodes)); $this->assertEquals('p', $div->lastChild->name); $this->checkStructure($div); $p = CDom::fromString('<p>')->appendTo(array()); $this->assertTrue($p instanceof CDomDocument); $this->assertEmpty($p->parent); $p = CDom::fromString('<p>')->appendTo(array('<div>')); $this->assertTrue($p instanceof CDomDocument); $this->assertEmpty($p->parent); $dom2 = CDom::fromString('text'); $p = CDom::fromString('<p>')->appendTo($dom2->firstNode); $this->assertTrue($p instanceof CDomDocument); $this->assertEmpty($p->parent); $this->assertEmpty($dom2->firstNode->nodes); $list = $dom->html('<i><b></b></i>')->find('*'); $dom2 = CDom::fromString('<br/><t/>'); $list2 = $dom2->find('*')->appendTo($list); $this->assertEmpty($dom2->nodes); $this->assertEquals(4, $list2->length); $this->checkStructure($dom); $this->checkStructure($dom2); $expected = '<i><b><br /><t /></b><br /><t /></i>'; $this->assertEquals($expected, $dom->html()); $list2->remove(); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $this->checkStructure($dom); $list2 = $dom2->find('*')->appendTo($list); $this->assertEquals(0, $list2->length); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $list2 = $dom->find('.unknown')->appendTo('b'); $this->assertEquals(0, $list2->length); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $list2 = $dom->append('<br>')->find('br')->appendTo('b'); $this->assertEquals(1, $list2->length); $expected = '<i><b><br /></b></i>'; $this->assertEquals($expected, $dom->html()); // PrependTo $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; CDom::fromString('<p>')->prependTo($div); $this->assertEquals(2, count($div->children)); $this->assertEquals(3, count($div->nodes)); $this->assertEquals('p', $div->firstChild->name); $this->checkStructure($div); $list = $dom->html('<i><b></b></i>')->find('*'); $dom2 = CDom::fromString('<br/><t/>'); $list2 = $dom2->find('*')->prependTo($list); $this->assertEmpty($dom2->nodes); $this->assertEquals(4, $list2->length); $this->checkStructure($dom); $this->checkStructure($dom2); $expected = '<i><br /><t /><b><br /><t /></b></i>'; $this->assertEquals($expected, $dom->html()); $list2->remove(); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $this->checkStructure($dom); // InsertBefore $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; CDom::fromString('<p>')->insertBefore($div); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals('p', $dom->firstChild->name); $this->assertEquals('div', $dom->lastChild->name); $this->checkStructure($dom); $list = $dom->html('<i><b></b></i>')->find('*'); $dom2 = CDom::fromString('<br/><t/>'); $list2 = $dom2->find('*'); $this->assertEquals(2, $list2->length); $list2->insertBefore($list); $this->assertEmpty($dom2->nodes); $this->assertEquals(4, $list2->length); $this->checkStructure($dom); $this->checkStructure($dom2); $expected = '<br /><t /><i><br /><t /><b></b></i>'; $this->assertEquals($expected, $dom->html()); $list2->remove(); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $this->checkStructure($dom); // InsertAfter $dom->html('<div>text<br/></div>'); $div = $dom->firstChild; CDom::fromString('<p>')->insertAfter($div); $this->assertEquals(2, count($dom->children)); $this->assertEquals(2, count($dom->nodes)); $this->assertEquals('div', $dom->firstChild->name); $this->assertEquals('p', $dom->lastChild->name); $this->checkStructure($dom); $list = $dom->html('<i/><i/>')->find('*'); $this->assertEquals(2, $list->length); $list2 = CDom::fromString('<p/>')->insertAfter($list); $this->assertEquals(2, $list2->length); $this->checkStructure($dom); $this->checkList($list2); $expected = '<i /><p /><i /><p />'; $this->assertEquals($expected, $dom->html()); $list = $dom->html('<i><b></b></i>')->find('*'); $dom2 = CDom::fromString('<br/><t/>'); $list2 = $dom2->find('*'); $this->assertEquals(2, $list2->length); $list2->insertAfter($list); $this->assertEmpty($dom2->nodes); $this->assertEquals(4, $list2->length); $this->checkStructure($dom); $this->checkStructure($dom2); $expected = '<i><b></b><br /><t /></i><br /><t />'; $this->assertEquals($expected, $dom->html()); $list2->remove(); $expected = '<i><b></b></i>'; $this->assertEquals($expected, $dom->html()); $this->checkStructure($dom); $dom->clear(); $dom2->clear(); }
/** * Returns or sets the text contents of this element, * including descendants. * * @link http://api.jquery.com/text/ * * @param string|CDomNode|CDomNode[]|CDomNodesList $value * * @return string|$this */ public function text($value = null) { // Set if ($value !== null) { // array if (is_array($value)) { $list = new CDomNodesList(); $list->list = $value; $value = new CDomNodeText($list->textAll()); } else { if (!is_object($value)) { $value = new CDomNodeText($value); } else { if ($value instanceof CDomNodesList) { $value = new CDomNodeText($value->textAll()); } else { if (!$value instanceof CDomNodeText) { $value = new CDomNodeText($value->text()); } } } } return $this->clearChildren()->append($value); } // Get return $this->_text(); }