מבוא
רוב כלי HTML נעצרים בעץ. Aspose.HTML FOSS עבור Python הולך שכבה אחת קדימה: הוא מיישם את מודל האובייקטים של CSS, ולכן גיליון סגנונות אינו רק טקסט שאתה נושא — הוא משתתף במזגן אמיתי, וכל אלמנט יכול לספר לך את הסגנון שהוא בפועל מקבל.
הצלילה העמוקה הזו מכסה את שכבת ה-CSSOM: ניתוח גיליונות סגנונות עם CSSStyleSheet, עריכת הצהרות דרך CSSStyleDeclaration, וקריאת התוצאות בחזרה עם Element.get_computed_style() — כולל כיצד ספציפיות, !important, והורשה מתקשרים. הספרייה נמצאת ב-PyPI בשם aspose-html-foss, ברישיון MIT, וטהורה Python (3.10+), מה שהופך אותה למעשית לאימות סגנונות במערכי בדיקות ושירותים שבהם דפדפן ללא ראש יהיה מיותר.
תכונות מרכזיות
פענוח וחיבור גיליונות סגנונות
CSSStyleSheet.replace_sync() מפענח את טקסט גיליון הסגנונות במקומו; Document.attach_style_sheet() רושם את הגיליון עם המסמך כך שהוא משתתף במזגן. ניתן גם לבנות גיליונות כלל-אחר-כלל עם 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)
ספציפיות קובעת את המנצח
כאשר כמה כללים מתייחסים לאותו אלמנט, הערך המחושב נובע ממעמד הסלקטור. סלקטור ID (מעמד 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 .פתר קונפליקט תלת-כיווני — כלל מחלקה מול כלל מזהה מול הצהרה משולבת:
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.