Comparing version
@@ -5,2 +5,34 @@ # Changelog | ||
## [2.0.2](https://github.com/wind2sing/cparse/compare/v2.0.1...v2.0.2) (2024-12-16) | ||
### 🎯 进一步优化 - 移除冗余语法糖 | ||
继续优化项目,移除价值有限的语法糖,专注于真正有价值的功能。 | ||
### 🗑️ 移除功能 | ||
- **移除类条件语法糖**:`selector[.class]` → 直接使用标准 CSS `selector.class` | ||
- 原因:Cheerio 原生支持更简洁,无需额外语法糖 | ||
- 迁移:将 `div[.active]` 改为 `div.active` | ||
### 🔧 改进功能 | ||
- **修复 nextNode() 方法**:增加错误处理和更好的文本节点查找 | ||
- **更新版本号**:修复代码中的版本号不一致问题 | ||
### 📚 文档更新 | ||
- 更新 README 和示例,移除已废弃的语法糖说明 | ||
- 突出标准 CSS 选择器的完全支持 | ||
### 💥 Breaking Changes | ||
- 移除 `selector[.class]` 语法糖支持 | ||
- 用户需要使用标准 CSS 类选择器 `selector.class` | ||
### 🔄 迁移指南 | ||
```javascript | ||
// ❌ 旧语法(已移除) | ||
parse('div[.active]', $) | ||
// ✅ 新语法(推荐) | ||
parse('div.active', $) | ||
``` | ||
## [2.0.1](https://github.com/wind2sing/cparse/compare/v2.0.0...v2.0.1) (2024-12-16) | ||
@@ -7,0 +39,0 @@ |
@@ -94,5 +94,5 @@ /** | ||
console.log('\n类条件简化:'); | ||
console.log(' 活跃菜单项:', parse('.menu-item[.active] .nav-link', $)); | ||
console.log(' 外部链接:', parse('a[.external]@href', $)); | ||
console.log('\n标准 CSS 类选择器:'); | ||
console.log(' 活跃菜单项:', parse('.menu-item.active .nav-link', $)); | ||
console.log(' 外部链接:', parse('a.external@href', $)); | ||
@@ -99,0 +99,0 @@ console.log('\n自定义伪选择器:'); |
@@ -5,3 +5,3 @@ /** | ||
* @author wind2sing | ||
* @version 1.0.6 | ||
* @version 2.0.1 | ||
* @license MIT | ||
@@ -8,0 +8,0 @@ */ |
{ | ||
"name": "cparse", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "一个基于 Cheerio 的 HTML 解析和数据提取工具库", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,3 @@ # cparse | ||
- **数组提取语法**:`[selector]` - 获取所有匹配元素 | ||
- **类条件简化**:`selector[.class]` - 简化的类选择器 | ||
- **标准 CSS 支持**:完全兼容 Cheerio 原生 CSS 选择器 | ||
- **自定义伪选择器**:`:not-empty` - 扩展的伪选择器 | ||
@@ -146,10 +146,9 @@ | ||
### 3. 类条件简化 `[.class]` | ||
### 3. 标准 CSS 选择器支持 | ||
```javascript | ||
// 传统 Cheerio 写法 | ||
$('div').filter('.active').text(); | ||
// cparse 语法糖 | ||
parse('div[.active]', $); | ||
// 完全支持 Cheerio 原生 CSS 选择器 | ||
parse('div.active', $); // 类选择器 | ||
parse('input[type="text"]', $); // 属性选择器 | ||
parse('li:first-child', $); // 伪选择器 | ||
``` | ||
@@ -258,8 +257,8 @@ | ||
#### 1. 类条件简化 | ||
#### 1. 标准 CSS 选择器支持 | ||
```javascript | ||
// 语法糖 | ||
parse('div[.active]', $) | ||
// 等价于 Cheerio 原生 | ||
parse('div.active', $) | ||
// 完全支持 Cheerio 原生 CSS 选择器 | ||
parse('div.active', $) // 类选择器 | ||
parse('input[type="text"]', $) // 属性选择器 | ||
parse('li:first-child', $) // 伪选择器 | ||
``` | ||
@@ -394,3 +393,3 @@ | ||
- ✅ 数组提取语法:`[selector]` | ||
- ✅ 类条件简化:`selector[.class]` | ||
- ✅ 标准 CSS 支持:完全兼容 Cheerio 原生选择器 | ||
- ✅ 自定义伪选择器:`:not-empty` | ||
@@ -397,0 +396,0 @@ - ✅ 强大的过滤器系统 |
@@ -10,3 +10,13 @@ | ||
const el = this.get(0); | ||
if (el) return el.nextSibling.nodeValue; | ||
if (el && el.nextSibling) { | ||
// 查找下一个文本节点 | ||
let sibling = el.nextSibling; | ||
while (sibling) { | ||
if (sibling.type === 'text' && sibling.data.trim()) { | ||
return sibling.data.trim(); | ||
} | ||
sibling = sibling.nextSibling; | ||
} | ||
} | ||
return undefined; | ||
}, | ||
@@ -13,0 +23,0 @@ extract(attr) { |
@@ -36,7 +36,5 @@ /** | ||
function parseCustomSyntax(selectorPart) { | ||
// 检查类条件语法糖: selector[.class] -> selector.class | ||
let result = selectorPart.replace(/([^[\]]+)\[\.([^[\]]+)\]/g, '$1.$2'); | ||
// 检查自定义伪选择器: selector:not-empty -> selector:not(:empty) | ||
result = result.replace(/([^:]+):not-empty/g, '$1:not(:empty)'); | ||
// 这是 Cheerio 不支持的语法糖,有实际价值 | ||
const result = selectorPart.replace(/([^:]+):not-empty/g, '$1:not(:empty)'); | ||
@@ -78,3 +76,3 @@ return result; | ||
* queryParser('span | trim | int') // { selector: 'span', attribute: undefined, filters: [...], getAll: false } | ||
* queryParser('div[.active]') // { selector: 'div.active', attribute: undefined, filters: [], getAll: false } | ||
* queryParser('div.active') // { selector: 'div.active', attribute: undefined, filters: [], getAll: false } | ||
* queryParser('p:not-empty') // { selector: 'p:not(:empty)', attribute: undefined, filters: [], getAll: false } | ||
@@ -81,0 +79,0 @@ */ |
105761
1.42%2165
0.42%427
-0.23%