ronan-gloo/htmlnode

此包的最新版本(dev-master)没有提供许可证信息。

HTML对象创建器

dev-master 2013-05-10 23:07 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:05:57 UTC


README

Build Status

HtmlNode 是一个用于在 PHP 中创建和操作 HTML 元素的 "bootsrap library"。
需要 PHP 5.4+

创建和选项

创建您的第一个节点

$node = new Node("h1", "Hello World", ["class" => "title"]);

节点标签

更改或获取节点的标签

// Setter:
$node->tag("h2");
// Getter:
$node->tag();
// Ask if the node is an autoclosed element:
$node->autoclose();

节点文本对象

在自动关闭的元素(input、meta等)上设置文本将抛出 LogicException

$node->text("Chapter 10");
// The getter
$node->text();
// Check if text contains the string:
$node->contains("Chapter");
// $2: case insensitive or not. $3: type checking
$node->contains(10, true, true);
// Replace part of the text
$node->text->replace("10", "20");
// Check if text match the expression
$node->text->match("/^d%2B$/");
// Text length
$node->text->length();

// moving text in collection
$child = (new Node)->appendTo($node);
$node->text->after($child);
$node->text->before($child);

// text is the first / last element in childs
$node->text->first();
$node->text->last();

节点属性

一旦节点实例化,就可以很容易地自定义它。几种方法允许您以 "jQuery" 方式设置选项。

$node->attr("rel", "tooltip");
// With an array
$node->attr(["rel" => "tooltip", "id" => "hello-world"]);
// Get an attribute
$node->attr("id");

针对属性进行搜索。这是一个非常基本的支持,仅支持 : .# 表达式

$node->is(":disabled");
$node->is(".active");
$node->not("h1");

dataaria 属性自定义支持

$node->data("position", "left");
// multi-level setter
$node->data(["position" => ["before" => "left", "after" => "right"]]);
// An other way is to make use of dotted notation
$node->data("position.before", "left");
// It also workds with the getter
$node->data("position.before");

添加/移除类。

$node->addClass("active");
// With an array
$node->addClass(["active", "title"])
// conditionnal: value not false in second parameter determines if the class should be added
$node->addClassIf("active", true)
// With 3 parameters, the class is added if third $2 match $3
$node->removeClassIf("active", true, false) // nothing is added

Css 内联样式

$node->css("color", "red");
// With an array
$node->css(["color" => "red", "margin" => 20])
// With an array
$node->css(["color" => "red", "margin" => 20])

节点操作

将节点包裹或解包裹到新节点中。

$node->wrap("hgroup", ["class" => "group"]);
$node->unwrap();

如果您要将节点放入另一个节点中,请使用 append()prepend()

// Append to an existing node
$node->appendTo($parent);
// An other way to achieve this is
$parent->append($node);
// Or creates node on-the-fly
$node->prependTo("hgroup", [["class" => "group"]);

请注意,移动的节点是克隆,原始节点的修改不会传播。因此,如果您想存储新节点,请这样做

$appended = $node->appendTo($parent);

您还可以使用 insertBefore()insertAfter() 方法将节点插入父节点

$new = (new Node("h3"))->insertAfter($appended);
// Detach node from $parent
$new->detach();

用另一个节点替换节点

$new = new Node("h3");
$node->replaceWith($new);

节点遍历

一组节点子代是一个实现 ArrayAccess 和 Tarversable 的集合对象,以便操作集合

// Loop throught node childs:
if ($parent->hasChildren()) {
  foreach ($parent->children() as $key => $child) {
    $child->addClassIf("active", $key % 2);
  }
}
// Get the parent:
if ($child->hasParent()) {
  $parent = $child->parent();
}
// You can also check for the child:
if ($parent->isParentOf($child)) {
  $child->detach();
}
// And the parent:
if (! $child->isChildOf($parent)) {
  $parent->append($child);
}

获取子代当前位置(返回一个整数)

$child->index();
// or from the parent node
$parent->children->indexOf($child);

通过集合中的位置获取子代

$child = $parent->children()->eq(2);

遍历集合内的节点兄弟

if ($sibling = $child->next()) {
  $child = $silbing->prev();
}

有几种不同的方法可以获取兄弟集合

// The collection, whithout the node:
$siblings = $child->siblings();
// All next / prev silbings
$na = $child->nextAll();
$pa = $child->prevAll();

节点遍历和搜索

$list = Node::make("ul");
$item = Node::make("li");
// Populate the list:
foreach ($items as $key => $val) {
  $temp = $item->appendTo($list)->addClassIf("even", $key % 2);
  // Set only 1 foo:
  if ($key == 4) {
    $anchor = Node::make("a", "foo")->appendTo($temp);
  }
}

现在,我们可以从我们的元素开始获取集合

// Find all  tags:
$a = $list->find("a");
// Find by attribute name:
$a = $list->find("[required]");
// Find by name / value:
$a = $list->find('[name="email"]');
// find childrens with class .even
$e = $list->children(".even");
// or a single result (a node)
$c = $anchor->closest("ul");
// find in next / prev
$p = $temp->prevAll(".even");

节点渲染

节点对象可以被输出,__toString 方法调用 render()

// Render a node with all its contents:
echo $node->render();
// or simply:
echo $node;
// Render the node html (the text is skipped):
echo $node->html();
// Render the node contents (node children %2B text)
echo $node->contents();
// Render the node, whithout its html / text
echo $node->self();

构建您自己的节点!

注册自定义节点实例,以后可以重新使用。将第三个参数添加为 true 以防止进一步实例化(作为单例)

// register the	custom node:
Node::macro("input", function($name, $value = null, $attrs = []){
  return Node::make("input", compact("name", "value") %2B $attrs);
});
// use it:
Node::input("email", null, ["placeholder" => "Email"]);