gwa/dom-inspector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gwa/dom-inspector

Composer 安装命令:

composer require gwa/dom-inspector

包简介

Provides methods for inspecting nodes in HTML markup.

README 文档

README

PHP >= 5.4

Latest Stable Version Total Downloads Latest Unstable Version License

Build Status Scrutinizer Code Quality

The DOMInspector provides PHP methods for traversing and inspecting nodes in HTML markup.

Installation

Install using composer:

composer require gwa/dom-inspector

Example Usage

Consider the following markup in the variable $markup:

<select name="fruit" class="big">
    <option value="1">apples</option>
    <option value="2" selected>oranges</option>
    <option value="3">pears</option>
    <option value="4">kiwis</option>
</select>

In our unit tests we want to inspect the structure of the HTML.

// Create an Inspector instance, passing the markup.
$inspector = new \Gwa\DOMInspector\Inspector($markup);

The inspector represents a node that contains the nodes in the markup passed into it.

We expect there should be single child node, the select element.

// (We are using the PHPUnit test framework.)

// Test that there is one node
$this->assertEquals(1, $inspector->children()->count());
$select = $inspector->children()->get(0);

// Test the "tag name" of the first node
$this->assertEquals('select', $select->tagname());

// Test that the select has the class `big`
$this->assertTrue($select->hasClass('big'));

// Test the `name` attribute value
$this->assertEquals('fruit', $select->attr('name'));

The select element should expose four option nodes.

$this->assertTrue($select->contains(4, 'option'));
$this->assertEquals(4, $select->children()->count());

Selectors

A selector is a string with one of the following formats:

tag
.classname
#id
tag.classname
tag#id
tag#id.classname

Methods

Inspector / Node

find($selector) NodeList

Returns a NodeList containing all child nodes that match the selector.

children($selector = null) NodeList

Returns a NodeList containing all direct child nodes, or a single Node if an numeric index is specified, or filtered nodes if a selector string is passed.

// return NodeList containing all LIs
$inspector->find('ul')->children();

// return NodeList containing second LI
$inspector->find('ul')->children(1);

// return NodeList containing all LIs with class 'active'
$inspector->find('ul')->children('.active');

tagname() string

Returns the tag name of the node.

id() string|null

Returns the id attribute value of the node.

attr($attr) string|null

Returns the value of an attribute of the node.

html() string

Returns the "outer" HTML value of the node.

text() string

Returns the text value of the node.

For complex text, structure (p and br) is maintained. For example with the following markup

<article>
    <p>
        This is some <strong>text</strong> with <em>inline styles</em>
        and a <a href="http://www.example.com">link</a>.<BR/>
        With a line break.
    </p>
    <p>
        A second paragraph.
    </p>
</article>

the text method

$inspector->children('article')->first()->text();

returns

This is some text with inline styles and a link.
With a line break.

A second paragraph.

hasClass($cssclass) boolean

Assert whether the node has the class passed as an attribute.

contains($selector) boolean

Assert whether the node has one or more direct child nodes that match the selector.

containsDeep($selector) boolean

Assert whether the node contains one or more child nodes that match the selector.

containsNum($selector) boolean

Assert whether the node has a certain number of direct child nodes that match the selector.

containsNumDeep($selector) boolean

Assert whether the node contains a certain number of child nodes that match the selector.

NodeList

The NodeList is a flat list of nodes. It is iterable, so you can do this:

$blanks = [];
$links = $inspector->find('a');
foreach ($links as $link) {
    if ($link->attr('target') === '_blank') {
        $blanks[] = $link;
    }
}

count() integer

Returns the number of nodes in the list.

get($index) Node

Returns the Node at the zero-based index specified.

first() Node

Returns the first Node in the list.

last() Node

Returns the last Node in the list.

filter() NodeList

Returns a new NodeList created by filtering the current list using the selector passed.

Tests

Run tests using phpunit.

$ vendor/bin/phpunit -c tests/phpunit.xml tests

gwa/dom-inspector 适用场景与选型建议

gwa/dom-inspector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.98k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gwa/dom-inspector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gwa/dom-inspector 我们能提供哪些服务?
定制开发 / 二次开发

基于 gwa/dom-inspector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-07-13