• R/O
  • HTTP
  • SSH
  • HTTPS

htmltree: 提交

メインリポジトリ


Commit MetaInfo

修订版1b93349836725abb3fd67f7c41f7226c17cc8f2c (tree)
时间2011-07-15 19:06:05
作者Hiromichi MATSUSHIMA <hirom@offi...>
CommiterHiromichi MATSUSHIMA

Log Message

add eq() to HTMLElement

更改概述

差异

--- a/htmltree.py
+++ b/htmltree.py
@@ -99,6 +99,9 @@ class HTMLElement(list):
9999 else:
100100 return "<UNKNOWN>"
101101
102+ def __eq__(self, other):
103+ return id(self) == id(other)
104+
102105 def _attrs2str(self):
103106 str = []
104107 f = lambda x,y: x if y == None else "%s='%s'" % (x,y)
@@ -170,6 +173,17 @@ class HTMLElement(list):
170173 for i in self:
171174 i._r_get_elements_by_class(cls, buf)
172175
176+ def get_elements(self, name, attrs):
177+ elems = self.get_elements_by_name(name)
178+ results = []
179+ for elem in elems:
180+ for name in attrs:
181+ if elem.get_attribute(name, "") != attrs[name]:
182+ break
183+ else:
184+ results.append(elem)
185+ return results
186+
173187 # manipulation functions
174188 def append_tag(self, tag, attrs):
175189 elem = HTMLElement(HTMLElement.TAG, tag, attrs)
--- a/test/sample.html
+++ b/test/sample.html
@@ -3,6 +3,7 @@
33 <head>
44 <title>htmltree.py sample html</title>
55 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6+<link rel="stylesheet" type="text/css" href="main.css">
67 </head>
78 <body id="htmltree" class="content">
89 <div class="main-column">
--- a/test/test_base.py
+++ b/test/test_base.py
@@ -37,9 +37,18 @@ print t.inner_html()
3737 t = r.select_1st("#header1")
3838 print t.inner_html()
3939
40-r.remove_element(t)
40+print "test_remove"
41+attrs = {
42+ "rel": "stylesheet",
43+ "type": "text/css",
44+ "href": "main.css"
45+ }
46+elems = r.get_elements("link", attrs)
47+for elem in elems:
48+ elem.delete()
4149 print r.inner_html()
4250
4351 t = r.select_1st("#htmltree")
4452 t.delete()
4553 print r.inner_html()
54+
Show on old repository browser