承接 softcreatr/html5-dom-document-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

softcreatr/html5-dom-document-php

最新稳定版本:3.0.0

Composer 安装命令:

composer require softcreatr/html5-dom-document-php

包简介

HTML5 DOMDocument PHP library (extends DOMDocument)

README 文档

README

HTML5DOMDocument extends the native DOMDocument class in PHP. It fixes some long-standing issues and introduces additional, modern functionality for handling HTML5 documents.

Latest Stable Version License

Why Use HTML5DOMDocument?

  • Preserves HTML entities that the native DOMDocument does not handle properly.
  • Preserves void tags like <input> or <br>, which DOMDocument tends to mishandle.
  • Allows inserting HTML code correctly into the document, ensuring that head and body elements are placed in their respective sections.
  • Enables CSS-style selectors for querying the DOM:
    • Supported selectors include *, tagname, tagname#id, #id, tagname.classname, .classname, multiple class selectors, attribute selectors, and complex selectors like div p, div > p, div + p, p ~ ul, and div, p.
  • Element manipulation with:
    • element->classList for manipulating classes
    • element->innerHTML for working with the contents of an element
    • element->outerHTML for manipulating an entire element as a string
  • Efficient handling of duplicate elements and IDs with advanced HTML insertion and validation.

Installation via Composer

Install via Composer with the following command:

composer require "softcreatr/html5-dom-document-php:3.*"

Features & Improvements

  • ClassList Support: The library adds support for manipulating classes of elements through the classList property, making it easier to work with CSS class attributes.
  • Enhanced Query Selectors: Use advanced CSS-like selectors for querying elements from the DOM, such as:
    • div > p (direct child)
    • div + p (adjacent sibling)
    • p ~ ul (general sibling)
    • [attribute], [attribute=value], etc.
  • HTML Insertion: It allows inserting fragments of HTML into the document, intelligently placing them in the correct location (e.g., scripts into <head>, content into <body>).
  • Custom Insert Targets: Define custom targets within the document for precise HTML insertion.

Documentation

Comprehensive documentation is available in the repository.

Examples

Basic Usage

Use HTML5DOMDocument just like the native DOMDocument:

<?php
require 'vendor/autoload.php';

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body>Hello World!</body></html>');
echo $dom->saveHTML();

Querying with CSS Selectors

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<!DOCTYPE html><html><body><h1>Hello</h1><div class="content">This is some text</div></body></html>');

echo $dom->querySelector('h1')->innerHTML;  // Outputs: Hello
echo $dom->querySelector('.content')->outerHTML;  // Outputs: <div class="content">This is some text</div>

Inserting HTML Code

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('
    <!DOCTYPE html>
    <html>
        <head><style>body { color: red; }</style></head>
        <body><h1>Hello</h1></body>
    </html>
');

$dom->insertHTML('
    <html>
        <head><script>alert("JS Script")</script></head>
        <body><div>This is some text</div></body>
    </html>
');

echo $dom->saveHTML();
// Properly merges new elements into the existing head and body.

Manipulating Class Attribute

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<div class="class1"></div>');

$div = $dom->querySelector('div');
$div->classList->add('class2');
$div->classList->remove('class1');

echo $div->getAttribute('class');  // Outputs: "class2"

Custom Insert Targets

$dom = new \SoftCreatR\HTML5DOMDocument\HTML5DOMDocument();
$dom->loadHTML('<html><body><div id="main"></div></body></html>');

$mainDiv = $dom->querySelector('#main');
$mainDiv->appendChild($dom->createInsertTarget('name1'));

$dom->insertHTML('<div id="new-content">New content</div>', 'name1');
echo $dom->saveHTML();

License

This project is licensed under the MIT License. See the license file for more details.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve this library. Let's collaborate to make it even better.

Authors

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 42
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-18