소개

대부분의 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 라이선스로 배포됩니다 — 상업적 사용, 수정 및 재배포가 허용됩니다. Source는 GitHub에 있으며 릴리스는 PyPI에 aspose-html-foss로 제공됩니다.


시작하기

관련 리소스