Giới thiệu
Hầu hết các công cụ HTML chỉ dừng lại ở cây. Aspose.HTML FOSS cho Python đi sâu hơn một lớp: nó triển khai mô hình đối tượng CSS, vì vậy một stylesheet không chỉ là văn bản bạn mang theo — nó tham gia vào một cascade thực tế, và mỗi phần tử có thể cho bạn biết kiểu dáng thực tế mà nó cuối cùng nhận được.
Đào sâu này bao phủ lớp CSSOM: phân tích stylesheet bằng CSSStyleSheet, chỉnh sửa khai báo qua CSSStyleDeclaration, và đọc lại kết quả bằng Element.get_computed_style() — bao gồm cách tính độ đặc hiệu, !important, và kế thừa tương tác. Thư viện được lưu trữ trên PyPI dưới dạng aspose-html-foss, có giấy phép MIT, và thuần Python (3.10+), điều này khiến nó thực tế cho việc xác minh kiểu dáng trong các bộ kiểm thử và dịch vụ nơi một trình duyệt không giao diện sẽ là quá mức.
Các tính năng chính
Phân tích và Gắn stylesheet
CSSStyleSheet.replace_sync() phân tích văn bản stylesheet tại chỗ; Document.attach_style_sheet() đăng ký sheet với tài liệu để nó tham gia vào cascade. Các sheet cũng có thể được xây dựng quy tắc-theo-quy tắc với insert_rule(), add_rule(), và remove_rule(), hoặc tạo từ văn bản bằng from_text():
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
Độ đặc hiệu quyết định người thắng
Khi nhiều quy tắc nhắm vào cùng một phần tử, giá trị đã tính toán tuân theo độ đặc hiệu của bộ chọn. Một bộ chọn ID (độ đặc hiệu 1,0,0) có ưu tiên hơn một bộ chọn lớp (0,1,0):
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: blue }")
doc.attach_style_sheet(sheet)
print(el.get_computed_style().get_property_value("color"))
Các khai báo nội tuyến và CSSStyleDeclaration
Thuộc tính style của mỗi phần tử là một CSSStyleDeclaration trực tiếp. Các khai báo được chỉnh sửa bằng set_property() và kiểm tra bằng get_property_value(), get_property_priority(), và item(). Các khai báo nội tuyến mang độ đặc hiệu (1,0,0), vì vậy chúng thắng các quy tắc kiểu và lớp từ bảng kiểu của tác giả:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Tầm quan trọng vượt lên trên độ đặc hiệu
!important đảo ngược kết quả thông thường: một quy tắc của tác giả được đánh dấu !important thắng một khai báo nội tuyến không quan trọng, bất kể độ đặc hiệu. Các mức ưu tiên cũng có thể được đặt bằng chương trình với set_property_priority():
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync("div { color: red !important }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Kế thừa và từ khóa inherit
Các thuộc tính có thể kế thừa như color truyền từ cha sang con trừ khi con có giá trị cục bộ riêng — một quy tắc cục bộ luôn ghi đè lên giá trị kế thừa. Từ khóa inherit rõ ràng sao chép giá trị đã tính toán của cha, và trên một phần tử gốc không có cha, nó sẽ quay lại giá trị khởi tạo của thuộc tính:
from aspose_html.dom import Document
doc = Document()
el = doc.create_element("div")
doc.append_child(el)
inline = el.style
inline.set_property("color", "inherit")
print(el.get_computed_style().get_property_value("color"))
Bắt đầu nhanh
Cài đặt từ PyPI:
git clone https://github.com/aspose-html-foss/Aspose.HTML-FOSS-for-Python.git
cd Aspose.HTML-FOSS-for-Python
pip install -e .Giải quyết xung đột ba chiều — quy tắc lớp vs. quy tắc ID vs. khai báo nội tuyến:
from aspose_html.dom import Document
from aspose_html.cssom import CSSStyleSheet
doc = Document()
el = doc.create_element("div")
el.set_attribute("class", "foo")
el.set_attribute("id", "bar")
doc.append_child(el)
sheet = CSSStyleSheet()
sheet.replace_sync(".foo { color: red } #bar { color: green }")
doc.attach_style_sheet(sheet)
inline = el.style
inline.set_property("color", "blue")
print(el.get_computed_style().get_property_value("color"))
Mã nguồn mở & Bản quyền
Aspose.HTML FOSS cho Python được phân phối theo giấy phép MIT — cho phép sử dụng thương mại, sửa đổi và phân phối lại. Mã nguồn có trên GitHub và các bản phát hành được cung cấp cho PyPI dưới dạng aspose-html-foss.