arraypress/wp-dom-utils
Composer 安装命令:
composer require arraypress/wp-dom-utils
包简介
A lean WordPress library for DOM operations with CSS selector support and element manipulation
关键字:
README 文档
README
A lightweight PHP library for DOM operations with CSS selector support and comprehensive element manipulation. Designed with clean APIs that make complex DOM tasks simple and reliable.
Features
- 🎯 Clean API: Separate classes for different DOM operations (
DOM,Element,CSS) - 🔍 CSS Selectors: Modern
querySelectorandquerySelectorAllsupport - 🏗️ Element Manipulation: Wrap, unwrap, insert, replace, and transform elements
- 🎨 Style Management: Parse and modify CSS styles with ease
- 🏷️ Class Utilities: Add, remove, and modify CSS classes intelligently
- 🛠️ Structure Modification: Change tag names, clone elements, and restructure DOM
- ⚡ WordPress Ready: Built for WordPress but works in any PHP environment
- 🔒 Safe Operations: Graceful error handling with null returns
Why Not WordPress's HTML API?
WordPress 6.2+ includes an HTML Tag Processor, but it's limited to linear attribute modifications. Our library provides:
- Full DOM tree manipulation (WordPress can't wrap, insert, or restructure)
- CSS selector support (WordPress uses linear tag scanning)
- Element creation and cloning (WordPress is read-modify only)
- Style and class management (WordPress focuses on attributes)
Requirements
- PHP 7.4 or later
- DOM extension
- libxml extension
Installation
composer require arraypress/wp-dom-utils
Basic Usage
Creating and Parsing DOM
use ArrayPress\DOMUtils\DOM; // Create DOM from HTML string $dom = DOM::create( '<div class="container"><p>Hello World</p></div>' ); // Query elements with CSS selectors $container = DOM::query_selector( '.container', $dom ); $paragraphs = DOM::query_selector_all( 'p', $dom ); // Convert back to HTML $html = DOM::to_html( $dom );
Element Manipulation
use ArrayPress\DOMUtils\Element; // Wrap an element $wrapper = Element::wrap( $paragraph, 'div', [ 'class' => 'wrapper' ] ); // Change tag name $new_element = Element::change_tag_name( 'section', $div ); // Insert content Element::insert_before( $target, '<span>Before</span>' ); Element::insert_after( $target, '<span>After</span>' ); Element::replace_with( $target, '<p>Replacement</p>' ); // Clone elements $clone = Element::clone_element( $original, true ); // deep clone
Class Management
use ArrayPress\DOMUtils\Element; // Add multiple classes Element::add_classes( $element, [ 'active', 'highlighted' ] ); // Remove classes Element::remove_classes( $element, [ 'old', 'deprecated' ] ); // Replace classes Element::replace_classes( $element, [ 'old-class' ], [ 'new-class' ] ); // Check for classes if ( Element::has_classes( $element, [ 'required', 'validated' ] ) ) { // Element has all required classes }
CSS Style Management
use ArrayPress\DOMUtils\CSS; use ArrayPress\DOMUtils\Element; // Parse CSS string to array $styles = CSS::string_to_array( 'color: red; margin: 10px; padding: 5px;' ); // Returns: ['color' => 'red', 'margin' => '10px', 'padding' => '5px'] // Convert array back to CSS string $css_string = CSS::array_to_string( [ 'color' => 'blue', 'font-size' => '14px' ] ); // Returns: "color: blue; font-size: 14px" // Add styles to element Element::add_styles( $element, [ 'background-color' => '#f0f0f0', 'border' => '1px solid #ccc', 'padding' => '10px' ] );
Common Use Cases
Content Transformation
function transform_content( $html ) { $dom = DOM::create( $html ); // Wrap all images in figure elements $images = DOM::query_selector_all( 'img', $dom ); foreach ( $images as $img ) { Element::wrap( $img, 'figure', [ 'class' => 'image-wrapper' ] ); } return DOM::to_html( $dom ); }
WordPress Integration
// Hook into WordPress content filters add_filter( 'the_content', function ( $content ) { $dom = DOM::create( $content ); // Add lazy loading to images $images = DOM::query_selector_all( 'img', $dom ); foreach ( $images as $img ) { if ( ! Element::get_attribute( $img, 'loading' ) ) { Element::set_attributes( $img, [ 'loading' => 'lazy' ] ); } } // Wrap tables for responsiveness $tables = DOM::query_selector_all( 'table', $dom ); foreach ( $tables as $table ) { Element::wrap( $table, 'div', [ 'class' => 'table-responsive' ] ); } return DOM::to_html( $dom ); } );
API Reference
DOM Class
create( string $html, array $options = [] ): DOMDocumentquery_selector( string $selector, $context ): ?DOMElementquery_selector_all( string $selector, $context ): DOMNodeListcreate_element( DOMDocument $dom, string $tag, array $attributes = [], $content = ''): ?DOMElementto_html( $node, bool $inner_html = false ): string
Element Class
Attributes:
get_attribute( DOMElement $element, string $attribute, $default = nullset_attributes( DOMElement $element, array $attributes ): voidremove_attributes( DOMElement $element, array $attributes ): void
Classes:
get_classes( DOMElement $element ): arrayadd_classes( DOMElement $element, array $classes ): voidremove_classes( DOMElement $element, array $classes ): voidhas_classes( DOMElement $element, array $classes ): bool
Styles:
get_styles( DOMElement $element ): arrayadd_styles( DOMElement $element, array $styles ): void
Structure:
wrap( DOMElement $element, string $wrapper_tag, array $attributes = [] ): ?DOMElementunwrap( DOMElement $element ): boolchange_tag_name(string $name, DOMElement $element ): ?DOMElementinsert_before( DOMElement $target, $content ): boolinsert_after( DOMElement $target, $content ): boolreplace_with( DOMElement $target, $content ): boolclone_element( DOMElement $element, bool $deep = true ): DOMElement
CSS Class
string_to_array( string $style_string ): arrayarray_to_string( array $styles ): string
Error Handling
All methods return null, false, or empty arrays for invalid inputs rather than throwing exceptions, making them safe for direct use in conditionals.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the GPL-2.0-or-later License.
Support
arraypress/wp-dom-utils 适用场景与选型建议
arraypress/wp-dom-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「html」 「dom」 「css-selectors」 「dom-manipulation」 「arraypress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/wp-dom-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/wp-dom-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/wp-dom-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
A library that provides basic utilities for creating HTML documents.
A bridge between SimpleXML and the DOM extension, plus a bunch of convenience methods
BigPipe: Pipelining web pages for high performance built in PHP.
Load DOM document safety
HTML and form generation
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-09-02