บทนำ
ส่วนใหญ่ HTML tooling หยุดที่ต้นไม้. Aspose.HTML FOSS สำหรับ Python ไปหนึ่งชั้นต่อไป: มันนำไปใช้โมเดลวัตถุ CSS, ดังนั้นสไตล์ชีตไม่ใช่เพียงข้อความที่คุณพกพา — มันมีส่วนร่วมในการไหลที่แท้จริง, และแต่ละ element สามารถบอกคุณสไตล์ที่มันจริง ๆ แล้วได้.
การสำรวจเชิงลึกนี้ครอบคลุมชั้น CSSOM: การแยกวิเคราะห์สไตล์ชีตด้วย CSSStyleSheet, การแก้ไขการประกาศผ่าน CSSStyleDeclaration, และการอ่านผลลัพธ์กลับด้วย Element.get_computed_style() — รวมถึงวิธีที่ specificity, !important, และ inheritance ทำงานร่วมกัน. ไลบรารีนี้อยู่บน PyPI ในรูปแบบ aspose-html-foss, มีใบอนุญาต MIT, และเป็น pure Python (3.10+), ซึ่งทำให้เป็นประโยชน์สำหรับการตรวจสอบสไตล์ในชุดทดสอบและบริการที่ headless browser จะเกินความจำเป็น.
คุณสมบัติหลัก
แยกวิเคราะห์และแนบสไตล์ชีต
CSSStyleSheet.replace_sync() ทำการแยกวิเคราะห์ข้อความสไตล์ชีตในที่เดียว; Document.attach_style_sheet() ลงทะเบียนแผ่นกับเอกสารเพื่อให้มันมีส่วนร่วมในการไหล. แผ่นสามารถสร้างเป็น rule-by-rule ด้วย insert_rule(), add_rule(), และ remove_rule(), หรือสร้างจากข้อความด้วย 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)
Specificity กำหนดผู้ชนะ
เมื่อกฎหลาย ๆ ตัวกำหนดเป้าหมายที่องค์ประกอบเดียวกัน ค่าที่คำนวณได้จะตามความเฉพาะของตัวเลือก ตัวเลือก ID (specificity 1,0,0) มีลำดับเหนือกว่าตัวเลือกคลาส (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"))
การประกาศแบบอินไลน์และ CSSStyleDeclaration
คุณสมบัติ style ของแต่ละองค์ประกอบเป็น CSSStyleDeclaration ที่ทำงานแบบเรียลไทม์ การประกาศสามารถแก้ไขด้วย set_property() และตรวจสอบด้วย get_property_value(), get_property_priority(), และ item(). การประกาศแบบอินไลน์มีความเฉพาะ (1,0,0) ดังนั้นจึงชนะกฎประเภทและคลาสจากสไตล์ชีทของผู้เขียน:
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"))
ความสำคัญเหนือความเฉพาะ
!important พลิกผลลัพธ์ปกติ: กฎของผู้เขียนที่ทำเครื่องหมาย !important ชนะการประกาศแบบอินไลน์ที่ไม่สำคัญ ไม่ว่าเฉพาะจะเป็นอย่างไร ความสำคัญสามารถตั้งค่าโดยโปรแกรมด้วย 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"))
การสืบทอดและคีย์เวิร์ด inherit
คุณสมบัติที่สามารถสืบทอดได้เช่น color จะไหลจากพาเรนต์ไปยังชิลด์ เว้นแต่ชิลด์จะมีค่าท้องถิ่นของตนเอง — กฎท้องถิ่นจะทับค่าที่สืบทอดเสมอ. คีย์เวิร์ด inherit อย่างชัดเจนจะคัดลอกค่าที่คำนวณของพาเรนต์, และบนองค์ประกอบรากที่ไม่มีพาเรนต์จะย้อนกลับไปใช้ค่าตั้งต้นของคุณสมบัตินั้น:
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"))
เริ่มต้นอย่างรวดเร็ว
ติดตั้งจาก PyPI:
git clone https://github.com/aspose-html-foss/Aspose.HTML-FOSS-for-Python.git
cd Aspose.HTML-FOSS-for-Python
pip install -e .แก้ไขความขัดแย้งแบบสามทาง — กฎคลาส vs. กฎ ID vs. คำประกาศแบบอินไลน์:
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"))
โอเพนซอร์ส & ไลเซนส์
Aspose.HTML FOSS สำหรับ Python ถูกแจกจ่ายภายใต้ไลเซนส์ MIT — การใช้เชิงพาณิชย์, การแก้ไข, และการกระจายต่อได้รับอนุญาต. โค้ดต้นฉบับอยู่บน GitHub และการปล่อยเวอร์ชันสู่ PyPI ในรูปแบบ aspose-html-foss.