diff options
| author | Lars Magne Ingebrigtsen | 2014-11-26 22:08:44 +0100 |
|---|---|---|
| committer | Lars Magne Ingebrigtsen | 2014-11-26 22:08:52 +0100 |
| commit | 7520f8919ae4030b5b2c3fbcc4141c85bb65aad1 (patch) | |
| tree | 0e672a0fe7097a140e4960e962713be79e144043 | |
| parent | c9cb3d535b2daf19b53dcaeedc2f2ae923bca2a1 (diff) | |
| download | emacs-7520f8919ae4030b5b2c3fbcc4141c85bb65aad1.tar.gz emacs-7520f8919ae4030b5b2c3fbcc4141c85bb65aad1.zip | |
Further eww dom.el cleanups
* net/eww.el (eww-tag-title): Use `dom-text'.
* dom.el (dom-by-tag): Use `equal' for comparisons so that tags
can be strings.
(dom-elements): Protect against non-text nodes.
(dom-non-text-children): New function.
| -rw-r--r-- | lisp/ChangeLog | 9 | ||||
| -rw-r--r-- | lisp/dom.el | 12 | ||||
| -rw-r--r-- | lisp/net/eww.el | 27 |
3 files changed, 30 insertions, 18 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 1698eb7631e..4884d5ca9c1 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,12 @@ | |||
| 1 | 2014-11-26 Lars Magne Ingebrigtsen <larsi@gnus.org> | ||
| 2 | |||
| 3 | * dom.el (dom-by-tag): Use `equal' for comparisons so that tags | ||
| 4 | can be strings. | ||
| 5 | (dom-elements): Protect against non-text nodes. | ||
| 6 | (dom-non-text-children): New function. | ||
| 7 | |||
| 8 | * net/eww.el (eww-tag-title): Use `dom-text'. | ||
| 9 | |||
| 1 | 2014-11-26 Sam Steingold <sds@gnu.org> | 10 | 2014-11-26 Sam Steingold <sds@gnu.org> |
| 2 | 11 | ||
| 3 | * textmodes/sgml-mode.el (sgml-validate-command): Pass -utf8 to tidy. | 12 | * textmodes/sgml-mode.el (sgml-validate-command): Pass -utf8 to tidy. |
diff --git a/lisp/dom.el b/lisp/dom.el index 3157e0b2f2a..04d6c219ec0 100644 --- a/lisp/dom.el +++ b/lisp/dom.el | |||
| @@ -47,6 +47,12 @@ | |||
| 47 | (cddr (car node)) | 47 | (cddr (car node)) |
| 48 | (cddr node))) | 48 | (cddr node))) |
| 49 | 49 | ||
| 50 | (defun dom-non-text-children (node) | ||
| 51 | "Return all non-text-node children of NODE." | ||
| 52 | (cl-loop for child in (dom-children node) | ||
| 53 | unless (stringp child) | ||
| 54 | collect child)) | ||
| 55 | |||
| 50 | (defun dom-set-attributes (node attributes) | 56 | (defun dom-set-attributes (node attributes) |
| 51 | "Set the attributes of NODE to ATTRIBUTES." | 57 | "Set the attributes of NODE to ATTRIBUTES." |
| 52 | (setq node (dom-ensure-node node)) | 58 | (setq node (dom-ensure-node node)) |
| @@ -93,7 +99,7 @@ A name is a symbol like `td'." | |||
| 93 | (dom-by-tag child tag)) | 99 | (dom-by-tag child tag)) |
| 94 | when matches | 100 | when matches |
| 95 | append matches))) | 101 | append matches))) |
| 96 | (if (eq (dom-tag dom) tag) | 102 | (if (equal (dom-tag dom) tag) |
| 97 | (cons dom matches) | 103 | (cons dom matches) |
| 98 | matches))) | 104 | matches))) |
| 99 | 105 | ||
| @@ -113,7 +119,9 @@ A name is a symbol like `td'." | |||
| 113 | "Find elements matching MATCH (a regexp) in ATTRIBUTE. | 119 | "Find elements matching MATCH (a regexp) in ATTRIBUTE. |
| 114 | ATTRIBUTE would typically be `class', `id' or the like." | 120 | ATTRIBUTE would typically be `class', `id' or the like." |
| 115 | (let ((matches (cl-loop for child in (dom-children dom) | 121 | (let ((matches (cl-loop for child in (dom-children dom) |
| 116 | for matches = (dom-elements child attribute match) | 122 | for matches = (and (not (stringp child)) |
| 123 | (dom-elements child attribute | ||
| 124 | match)) | ||
| 117 | when matches | 125 | when matches |
| 118 | append matches)) | 126 | append matches)) |
| 119 | (attr (dom-attr dom attribute))) | 127 | (attr (dom-attr dom attribute))) |
diff --git a/lisp/net/eww.el b/lisp/net/eww.el index a1460a8f557..10298b109d1 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el | |||
| @@ -453,14 +453,10 @@ See the `eww-search-prefix' variable for the search engine used." | |||
| 453 | (setq header-line-format nil))) | 453 | (setq header-line-format nil))) |
| 454 | 454 | ||
| 455 | (defun eww-tag-title (dom) | 455 | (defun eww-tag-title (dom) |
| 456 | (let ((title "")) | 456 | (plist-put eww-data :title |
| 457 | (dolist (sub (dom-children dom)) | 457 | (replace-regexp-in-string |
| 458 | (when (stringp sub) | 458 | "^ \\| $" "" |
| 459 | (setq title (concat title sub)))) | 459 | (replace-regexp-in-string "[ \t\r\n]+" " " (dom-text dom)))) |
| 460 | (plist-put eww-data :title | ||
| 461 | (replace-regexp-in-string | ||
| 462 | "^ \\| $" "" | ||
| 463 | (replace-regexp-in-string "[ \t\r\n]+" " " title)))) | ||
| 464 | (eww-update-header-line-format)) | 460 | (eww-update-header-line-format)) |
| 465 | 461 | ||
| 466 | (defun eww-tag-body (dom) | 462 | (defun eww-tag-body (dom) |
| @@ -589,14 +585,13 @@ the like." | |||
| 589 | (defun eww-highest-readability (node) | 585 | (defun eww-highest-readability (node) |
| 590 | (let ((result node) | 586 | (let ((result node) |
| 591 | highest) | 587 | highest) |
| 592 | (dolist (elem (dom-children node)) | 588 | (dolist (elem (dom-non-text-children node)) |
| 593 | (when (and (not (stringp elem)) | 589 | (when (> (or (dom-attr |
| 594 | (> (or (dom-attr | 590 | (setq highest (eww-highest-readability elem)) |
| 595 | (setq highest (eww-highest-readability elem)) | 591 | :eww-readability-score) |
| 596 | :eww-readability-score) | 592 | most-negative-fixnum) |
| 597 | most-negative-fixnum) | 593 | (or (dom-attr result :eww-readability-score) |
| 598 | (or (dom-attr result :eww-readability-score) | 594 | most-negative-fixnum)) |
| 599 | most-negative-fixnum))) | ||
| 600 | (setq result highest))) | 595 | (setq result highest))) |
| 601 | result)) | 596 | result)) |
| 602 | 597 | ||