はじめに

ほとんどの HTML ツールはツリーで止まります。Aspose.HTML FOSS for 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 .

3者間の競合を解決 — クラスルール 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 for Python は MIT ライセンスの下で配布されています — 商用利用、改変、再配布が許可されています。ソースは GitHub にあり、リリースは PyPI に aspose-html-foss として出荷されます。


開始手順

関連リソース