shr cleanup and background color fix
[emacs.git] / lisp / net / shr.el
blobf40fd34cb8e18dea6c72ac8efbfcf985622a2699
1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This package takes a HTML parse tree (as provided by
26 ;; libxml-parse-html-region) and renders it in the current buffer. It
27 ;; does not do CSS, JavaScript or anything advanced: It's geared
28 ;; towards rendering typical short snippets of HTML, like what you'd
29 ;; find in HTML email and the like.
31 ;;; Code:
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'url)) ;For url-filename's setf handler.
35 (require 'browse-url)
36 (require 'subr-x)
37 (require 'dom)
39 (defgroup shr nil
40 "Simple HTML Renderer"
41 :version "25.1"
42 :group 'web)
44 (defcustom shr-max-image-proportion 0.9
45 "How big pictures displayed are in relation to the window they're in.
46 A value of 0.7 means that they are allowed to take up 70% of the
47 width and height of the window. If they are larger than this,
48 and Emacs supports it, then the images will be rescaled down to
49 fit these criteria."
50 :version "24.1"
51 :group 'shr
52 :type 'float)
54 (defcustom shr-blocked-images nil
55 "Images that have URLs matching this regexp will be blocked."
56 :version "24.1"
57 :group 'shr
58 :type '(choice (const nil) regexp))
60 (defcustom shr-use-fonts nil
61 "If non-nil, use proportional fonts for text."
62 :version "25.1"
63 :group 'shr
64 :type 'boolean)
66 (defcustom shr-table-horizontal-line nil
67 "Character used to draw horizontal table lines.
68 If nil, don't draw horizontal table lines."
69 :group 'shr
70 :type '(choice (const nil) character))
72 (defcustom shr-table-vertical-line ?\s
73 "Character used to draw vertical table lines."
74 :group 'shr
75 :type 'character)
77 (defcustom shr-table-corner ?\s
78 "Character used to draw table corners."
79 :group 'shr
80 :type 'character)
82 (defcustom shr-hr-line ?-
83 "Character used to draw hr lines."
84 :group 'shr
85 :type 'character)
87 (defcustom shr-width nil
88 "Frame width to use for rendering.
89 May either be an integer specifying a fixed width in characters,
90 or nil, meaning that the full width of the window should be
91 used."
92 :version "25.1"
93 :type '(choice (integer :tag "Fixed width in characters")
94 (const :tag "Use the width of the window" nil))
95 :group 'shr)
97 (defcustom shr-bullet "* "
98 "Bullet used for unordered lists.
99 Alternative suggestions are:
100 - \" \"
101 - \" \""
102 :version "24.4"
103 :type 'string
104 :group 'shr)
106 (defcustom shr-external-browser 'browse-url-default-browser
107 "Function used to launch an external browser."
108 :version "24.4"
109 :group 'shr
110 :type 'function)
112 (defcustom shr-image-animate t
113 "Non nil means that images that can be animated will be."
114 :version "24.4"
115 :group 'shr
116 :type 'boolean)
118 (defvar shr-content-function nil
119 "If bound, this should be a function that will return the content.
120 This is used for cid: URLs, and the function is called with the
121 cid: URL as the argument.")
123 (defvar shr-put-image-function 'shr-put-image
124 "Function called to put image and alt string.")
126 (defface shr-strike-through '((t (:strike-through t)))
127 "Font for <s> elements."
128 :group 'shr)
130 (defface shr-link
131 '((t (:inherit link)))
132 "Font for link elements."
133 :group 'shr)
135 (defvar shr-inhibit-images nil
136 "If non-nil, inhibit loading images.")
138 ;;; Internal variables.
140 (defvar shr-folding-mode nil)
141 (defvar shr-start nil)
142 (defvar shr-indentation 0)
143 (defvar shr-internal-width nil)
144 (defvar shr-list-mode nil)
145 (defvar shr-content-cache nil)
146 (defvar shr-kinsoku-shorten nil)
147 (defvar shr-table-depth 0)
148 (defvar shr-stylesheet nil)
149 (defvar shr-base nil)
150 (defvar shr-depth 0)
151 (defvar shr-warning nil)
152 (defvar shr-ignore-cache nil)
153 (defvar shr-external-rendering-functions nil)
154 (defvar shr-target-id nil)
155 (defvar shr-inhibit-decoration nil)
156 (defvar shr-table-separator-length 1)
157 (defvar shr-table-separator-pixel-width 0)
158 (defvar shr-table-id nil)
159 (defvar shr-current-font nil)
161 (defvar shr-map
162 (let ((map (make-sparse-keymap)))
163 (define-key map "a" 'shr-show-alt-text)
164 (define-key map "i" 'shr-browse-image)
165 (define-key map "z" 'shr-zoom-image)
166 (define-key map [?\t] 'shr-next-link)
167 (define-key map [?\M-\t] 'shr-previous-link)
168 (define-key map [follow-link] 'mouse-face)
169 (define-key map [mouse-2] 'shr-browse-url)
170 (define-key map "I" 'shr-insert-image)
171 (define-key map "w" 'shr-copy-url)
172 (define-key map "u" 'shr-copy-url)
173 (define-key map "v" 'shr-browse-url)
174 (define-key map "o" 'shr-save-contents)
175 (define-key map "\r" 'shr-browse-url)
176 map))
178 ;; Public functions and commands.
179 (declare-function libxml-parse-html-region "xml.c"
180 (start end &optional base-url))
182 (defun shr-render-buffer (buffer)
183 "Display the HTML rendering of the current buffer."
184 (interactive (list (current-buffer)))
185 (or (fboundp 'libxml-parse-html-region)
186 (error "This function requires Emacs to be compiled with libxml2"))
187 (pop-to-buffer "*html*")
188 (erase-buffer)
189 (shr-insert-document
190 (with-current-buffer buffer
191 (libxml-parse-html-region (point-min) (point-max))))
192 (goto-char (point-min)))
194 ;;;###autoload
195 (defun shr-render-region (begin end &optional buffer)
196 "Display the HTML rendering of the region between BEGIN and END."
197 (interactive "r")
198 (unless (fboundp 'libxml-parse-html-region)
199 (error "This function requires Emacs to be compiled with libxml2"))
200 (with-current-buffer (or buffer (current-buffer))
201 (let ((dom (libxml-parse-html-region begin end)))
202 (delete-region begin end)
203 (goto-char begin)
204 (shr-insert-document dom))))
206 ;;;###autoload
207 (defun shr-insert-document (dom)
208 "Render the parsed document DOM into the current buffer.
209 DOM should be a parse tree as generated by
210 `libxml-parse-html-region' or similar."
211 (setq shr-content-cache nil)
212 (let ((start (point))
213 (shr-start nil)
214 (shr-base nil)
215 (shr-depth 0)
216 (shr-table-id 0)
217 (shr-warning nil)
218 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
219 (shr-internal-width (or (and shr-width
220 (if (not shr-use-fonts)
221 shr-width
222 (* shr-width (frame-char-width))))
223 (if (not shr-use-fonts)
224 (- (window-width) 2)
225 (- (window-pixel-width)
226 (* (frame-fringe-width) 2))))))
227 (shr-descend dom)
228 (shr-fill-lines start (point))
229 (shr-remove-trailing-whitespace start (point))
230 (when shr-warning
231 (message "%s" shr-warning))))
233 (defun shr-remove-trailing-whitespace (start end)
234 (let ((width (window-width)))
235 (save-restriction
236 (narrow-to-region start end)
237 (goto-char start)
238 (while (not (eobp))
239 (end-of-line)
240 (when (> (shr-previous-newline-padding-width (current-column)) width)
241 (dolist (overlay (overlays-at (point)))
242 (when (overlay-get overlay 'before-string)
243 (overlay-put overlay 'before-string nil))))
244 (forward-line 1)))))
246 (defun shr-copy-url (&optional image-url)
247 "Copy the URL under point to the kill ring.
248 If IMAGE-URL (the prefix) is non-nil, or there is no link under
249 point, but there is an image under point then copy the URL of the
250 image under point instead.
251 If called twice, then try to fetch the URL and see whether it
252 redirects somewhere else."
253 (interactive "P")
254 (let ((url (or (get-text-property (point) 'shr-url)
255 (get-text-property (point) 'image-url))))
256 (cond
257 ((not url)
258 (message "No URL under point"))
259 ;; Resolve redirected URLs.
260 ((equal url (car kill-ring))
261 (url-retrieve
263 (lambda (a)
264 (when (and (consp a)
265 (eq (car a) :redirect))
266 (with-temp-buffer
267 (insert (cadr a))
268 (goto-char (point-min))
269 ;; Remove common tracking junk from the URL.
270 (when (re-search-forward ".utm_.*" nil t)
271 (replace-match "" t t))
272 (message "Copied %s" (buffer-string))
273 (copy-region-as-kill (point-min) (point-max)))))
274 nil t))
275 ;; Copy the URL to the kill ring.
277 (with-temp-buffer
278 (insert (url-encode-url url))
279 (copy-region-as-kill (point-min) (point-max))
280 (message "Copied %s" (buffer-string)))))))
282 (defun shr-next-link ()
283 "Skip to the next link."
284 (interactive)
285 (let ((skip (text-property-any (point) (point-max) 'help-echo nil)))
286 (if (or (eobp)
287 (not (setq skip (text-property-not-all skip (point-max)
288 'help-echo nil))))
289 (message "No next link")
290 (goto-char skip)
291 (message "%s" (get-text-property (point) 'help-echo)))))
293 (defun shr-previous-link ()
294 "Skip to the previous link."
295 (interactive)
296 (let ((start (point))
297 (found nil))
298 ;; Skip past the current link.
299 (while (and (not (bobp))
300 (get-text-property (point) 'help-echo))
301 (forward-char -1))
302 ;; Find the previous link.
303 (while (and (not (bobp))
304 (not (setq found (get-text-property (point) 'help-echo))))
305 (forward-char -1))
306 (if (not found)
307 (progn
308 (message "No previous link")
309 (goto-char start))
310 ;; Put point at the start of the link.
311 (while (and (not (bobp))
312 (get-text-property (point) 'help-echo))
313 (forward-char -1))
314 (forward-char 1)
315 (message "%s" (get-text-property (point) 'help-echo)))))
317 (defun shr-show-alt-text ()
318 "Show the ALT text of the image under point."
319 (interactive)
320 (let ((text (get-text-property (point) 'shr-alt)))
321 (if (not text)
322 (message "No image under point")
323 (message "%s" (shr-fill-text text)))))
325 (defun shr-browse-image (&optional copy-url)
326 "Browse the image under point.
327 If COPY-URL (the prefix if called interactively) is non-nil, copy
328 the URL of the image to the kill buffer instead."
329 (interactive "P")
330 (let ((url (get-text-property (point) 'image-url)))
331 (cond
332 ((not url)
333 (message "No image under point"))
334 (copy-url
335 (with-temp-buffer
336 (insert url)
337 (copy-region-as-kill (point-min) (point-max))
338 (message "Copied %s" url)))
340 (message "Browsing %s..." url)
341 (browse-url url)))))
343 (defun shr-insert-image ()
344 "Insert the image under point into the buffer."
345 (interactive)
346 (let ((url (get-text-property (point) 'image-url)))
347 (if (not url)
348 (message "No image under point")
349 (message "Inserting %s..." url)
350 (url-retrieve url 'shr-image-fetched
351 (list (current-buffer) (1- (point)) (point-marker))
352 t t))))
354 (defun shr-zoom-image ()
355 "Toggle the image size.
356 The size will be rotated between the default size, the original
357 size, and full-buffer size."
358 (interactive)
359 (let ((url (get-text-property (point) 'image-url))
360 (size (get-text-property (point) 'image-size))
361 (buffer-read-only nil))
362 (if (not url)
363 (message "No image under point")
364 ;; Delete the old picture.
365 (while (get-text-property (point) 'image-url)
366 (forward-char -1))
367 (forward-char 1)
368 (let ((start (point)))
369 (while (get-text-property (point) 'image-url)
370 (forward-char 1))
371 (forward-char -1)
372 (put-text-property start (point) 'display nil)
373 (when (> (- (point) start) 2)
374 (delete-region start (1- (point)))))
375 (message "Inserting %s..." url)
376 (url-retrieve url 'shr-image-fetched
377 (list (current-buffer) (1- (point)) (point-marker)
378 (list (cons 'size
379 (cond ((or (eq size 'default)
380 (null size))
381 'original)
382 ((eq size 'original)
383 'full)
384 ((eq size 'full)
385 'default)))))
386 t))))
388 ;;; Utility functions.
390 (defsubst shr-generic (dom)
391 (dolist (sub (dom-children dom))
392 (if (stringp sub)
393 (shr-insert sub)
394 (shr-descend sub))))
396 (defun shr-descend (dom)
397 (let ((function
399 ;; Allow other packages to override (or provide) rendering
400 ;; of elements.
401 (cdr (assq (dom-tag dom) shr-external-rendering-functions))
402 (intern (concat "shr-tag-" (symbol-name (dom-tag dom))) obarray)))
403 (style (dom-attr dom 'style))
404 (shr-stylesheet shr-stylesheet)
405 (shr-depth (1+ shr-depth))
406 (start (point)))
407 ;; shr uses about 12 frames per nested node.
408 (if (> shr-depth (/ max-specpdl-size 12))
409 (setq shr-warning "Too deeply nested to render properly; consider increasing `max-specpdl-size'")
410 (when style
411 (if (string-match "color\\|display\\|border-collapse" style)
412 (setq shr-stylesheet (nconc (shr-parse-style style)
413 shr-stylesheet))
414 (setq style nil)))
415 ;; If we have a display:none, then just ignore this part of the DOM.
416 (unless (equal (cdr (assq 'display shr-stylesheet)) "none")
417 (if (fboundp function)
418 (funcall function dom)
419 (shr-generic dom))
420 (when (and shr-target-id
421 (equal (dom-attr dom 'id) shr-target-id))
422 ;; If the element was empty, we don't have anything to put the
423 ;; anchor on. So just insert a dummy character.
424 (when (= start (point))
425 (insert "*"))
426 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
427 ;; If style is set, then this node has set the color.
428 (when style
429 (shr-colorize-region
430 start (point)
431 (cdr (assq 'color shr-stylesheet))
432 (cdr (assq 'background-color shr-stylesheet))))))))
434 (defun shr-fill-text (text)
435 (if (zerop (length text))
436 text
437 (with-temp-buffer
438 (let ((shr-indentation 0)
439 (shr-start nil)
440 (shr-internal-width (- (window-pixel-width)
441 (* (frame-fringe-width) 2))))
442 (shr-insert text)
443 (buffer-string)))))
445 (define-inline shr-char-breakable-p (char)
446 "Return non-nil if a line can be broken before and after CHAR."
447 (inline-quote (aref fill-find-break-point-function-table ,char)))
448 (define-inline shr-char-nospace-p (char)
449 "Return non-nil if no space is required before and after CHAR."
450 (inline-quote (aref fill-nospace-between-words-table ,char)))
452 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
453 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
454 ;; parentheses, and so on, that should not be placed in the beginning
455 ;; of a line or the end of a line.
456 (define-inline shr-char-kinsoku-bol-p (char)
457 "Return non-nil if a line ought not to begin with CHAR."
458 (inline-letevals (char)
459 (inline-quote (and (not (eq ,char ?'))
460 (aref (char-category-set ,char) ?>)))))
461 (define-inline shr-char-kinsoku-eol-p (char)
462 "Return non-nil if a line ought not to end with CHAR."
463 (inline-quote (aref (char-category-set ,char) ?<)))
464 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
465 (load "kinsoku" nil t))
467 (defun shr-pixel-column ()
468 (if (not shr-use-fonts)
469 (current-column)
470 (if (not (get-buffer-window (current-buffer)))
471 (save-window-excursion
472 (set-window-buffer nil (current-buffer))
473 (car (window-text-pixel-size nil (line-beginning-position) (point))))
474 (car (window-text-pixel-size nil (line-beginning-position) (point))))))
476 (defun shr-pixel-region ()
477 (- (shr-pixel-column)
478 (save-excursion
479 (goto-char (mark))
480 (shr-pixel-column))))
482 (defun shr-string-pixel-width (string)
483 (if (not shr-use-fonts)
484 (length string)
485 (with-temp-buffer
486 (insert string)
487 (shr-pixel-column))))
489 (defun shr-insert (text)
490 (when (and (not (bolp))
491 (get-text-property (1- (point)) 'image-url))
492 (insert "\n"))
493 (cond
494 ((eq shr-folding-mode 'none)
495 (insert text))
497 (when (and (string-match "\\`[ \t\n\r ]" text)
498 (not (bolp))
499 (not (eq (char-after (1- (point))) ? )))
500 (insert " "))
501 (let ((start (point))
502 (bolp (bolp)))
503 (insert text)
504 (save-restriction
505 (narrow-to-region start (point))
506 (goto-char start)
507 (when (looking-at "[ \t\n\r ]+")
508 (replace-match "" t t))
509 (while (re-search-forward "[ \t\n\r ]+" nil t)
510 (replace-match " " t t))
511 (goto-char (point-max)))
512 ;; We may have removed everything we inserted if if was just
513 ;; spaces.
514 (unless (= start (point))
515 ;; Mark all lines that should possibly be folded afterwards.
516 (when bolp
517 (shr-mark-fill start))
518 (when shr-use-fonts
519 (add-face-text-property start (point)
520 (or shr-current-font 'variable-pitch)
521 t)))))))
523 (defun shr-fill-lines (start end)
524 (if (<= shr-internal-width 0)
526 (save-restriction
527 (narrow-to-region start end)
528 (goto-char start)
529 (when (get-text-property (point) 'shr-indentation)
530 (shr-fill-line))
531 (while (setq start (next-single-property-change start 'shr-indentation))
532 (goto-char start)
533 (when (bolp)
534 (shr-fill-line)))
535 (goto-char (point-max)))))
537 (defun shr-vertical-motion (column)
538 (if (not shr-use-fonts)
539 (move-to-column column)
540 (unless (eolp)
541 (forward-char 1))
542 (vertical-motion (cons (/ column (frame-char-width)) 0))
543 (unless (eolp)
544 (forward-char 1))))
546 (defun shr-fill-line ()
547 (let ((shr-indentation (get-text-property (point) 'shr-indentation))
548 (continuation (get-text-property
549 (point) 'shr-continuation-indentation))
550 start)
551 (put-text-property (point) (1+ (point)) 'shr-indentation nil)
552 (shr-indent)
553 (setq start (point))
554 (setq shr-indentation (or continuation shr-indentation))
555 (shr-vertical-motion shr-internal-width)
556 (when (looking-at " $")
557 (delete-region (point) (line-end-position)))
558 (while (not (eolp))
559 ;; We have to do some folding. First find the first
560 ;; previous point suitable for folding.
561 (if (or (not (shr-find-fill-point (line-beginning-position)))
562 (= (point) start))
563 ;; We had unbreakable text (for this width), so just go to
564 ;; the first space and carry on.
565 (progn
566 (beginning-of-line)
567 (skip-chars-forward " ")
568 (search-forward " " (line-end-position) 'move)))
569 ;; Success; continue.
570 (when (= (preceding-char) ?\s)
571 (delete-char -1))
572 (insert "\n")
573 (shr-indent)
574 (setq start (point))
575 (shr-vertical-motion shr-internal-width)
576 (when (looking-at " $")
577 (delete-region (point) (line-end-position))))))
579 (defun shr-find-fill-point (start)
580 (let ((bp (point))
581 (end (point))
582 failed)
583 (while (not (or (setq failed (<= (point) start))
584 (eq (preceding-char) ? )
585 (eq (following-char) ? )
586 (shr-char-breakable-p (preceding-char))
587 (shr-char-breakable-p (following-char))
588 (and (shr-char-kinsoku-bol-p (preceding-char))
589 (shr-char-breakable-p (following-char))
590 (not (shr-char-kinsoku-bol-p (following-char))))
591 (shr-char-kinsoku-eol-p (following-char))
592 (bolp)))
593 (backward-char 1))
594 (if failed
595 ;; There's no breakable point, so we give it up.
596 (let (found)
597 (goto-char bp)
598 (unless shr-kinsoku-shorten
599 (while (setq found (re-search-forward
600 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
601 (line-end-position) 'move)))
602 (if (and found
603 (not (match-beginning 1)))
604 (goto-char (match-beginning 0)))))
606 (eolp)
607 ;; Don't put kinsoku-bol characters at the beginning of a line,
608 ;; or kinsoku-eol characters at the end of a line.
609 (cond
610 (shr-kinsoku-shorten
611 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
612 (shr-char-kinsoku-eol-p (preceding-char)))
613 (backward-char 1))
614 (when (setq failed (<= (point) start))
615 ;; There's no breakable point that doesn't violate kinsoku,
616 ;; so we look for the second best position.
617 (while (and (progn
618 (forward-char 1)
619 (<= (point) end))
620 (progn
621 (setq bp (point))
622 (shr-char-kinsoku-eol-p (following-char)))))
623 (goto-char bp)))
624 ((shr-char-kinsoku-eol-p (preceding-char))
625 ;; Find backward the point where kinsoku-eol characters begin.
626 (let ((count 4))
627 (while
628 (progn
629 (backward-char 1)
630 (and (> (setq count (1- count)) 0)
631 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
632 (or (shr-char-kinsoku-eol-p (preceding-char))
633 (shr-char-kinsoku-bol-p (following-char)))))))
634 (when (setq failed (<= (point) start))
635 ;; There's no breakable point that doesn't violate kinsoku,
636 ;; so we go to the second best position.
637 (if (looking-at "\\(\\c<+\\)\\c<")
638 (goto-char (match-end 1))
639 (forward-char 1))))
640 ((shr-char-kinsoku-bol-p (following-char))
641 ;; Find forward the point where kinsoku-bol characters end.
642 (let ((count 4))
643 (while (progn
644 (forward-char 1)
645 (and (>= (setq count (1- count)) 0)
646 (shr-char-kinsoku-bol-p (following-char))
647 (shr-char-breakable-p (following-char))))))))
648 (when (eq (following-char) ? )
649 (forward-char 1))))
650 (not failed)))
652 (defun shr-parse-base (url)
653 ;; Always chop off anchors.
654 (when (string-match "#.*" url)
655 (setq url (substring url 0 (match-beginning 0))))
656 ;; NB: <base href="" > URI may itself be relative to the document s URI
657 (setq url (shr-expand-url url))
658 (let* ((parsed (url-generic-parse-url url))
659 (local (url-filename parsed)))
660 (setf (url-filename parsed) "")
661 ;; Chop off the bit after the last slash.
662 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
663 (setq local (match-string 1 local)))
664 ;; Always make the local bit end with a slash.
665 (when (and (not (zerop (length local)))
666 (not (eq (aref local (1- (length local))) ?/)))
667 (setq local (concat local "/")))
668 (list (url-recreate-url parsed)
669 local
670 (url-type parsed)
671 url)))
673 (autoload 'url-expand-file-name "url-expand")
675 ;; FIXME This needs some tests writing.
676 ;; Does it even need to exist, given that url-expand-file-name does?
677 (defun shr-expand-url (url &optional base)
678 (setq base
679 (if base
680 ;; shr-parse-base should never call this with non-nil base!
681 (shr-parse-base base)
682 ;; Bound by the parser.
683 shr-base))
684 (when (zerop (length url))
685 (setq url nil))
686 (cond ((or (not url)
687 (not base)
688 (string-match "\\`[a-z]*:" url))
689 ;; Absolute or empty URI
690 (or url (nth 3 base)))
691 ((eq (aref url 0) ?/)
692 (if (and (> (length url) 1)
693 (eq (aref url 1) ?/))
694 ;; //host...; just use the protocol
695 (concat (nth 2 base) ":" url)
696 ;; Just use the host name part.
697 (concat (car base) url)))
698 ((eq (aref url 0) ?#)
699 ;; A link to an anchor.
700 (concat (nth 3 base) url))
702 ;; Totally relative.
703 (url-expand-file-name url (concat (car base) (cadr base))))))
705 (defun shr-ensure-newline ()
706 (unless (zerop (current-column))
707 (insert "\n")))
709 (defun shr-ensure-paragraph ()
710 (unless (bobp)
711 (if (<= (current-column) shr-indentation)
712 (unless (save-excursion
713 (forward-line -1)
714 (looking-at " *$"))
715 (insert "\n"))
716 (if (save-excursion
717 (beginning-of-line)
718 ;; If the current line is totally blank, and doesn't even
719 ;; have any face properties set, then delete the blank
720 ;; space.
721 (and (looking-at " *$")
722 (not (get-text-property (point) 'face))
723 (not (= (next-single-property-change (point) 'face nil
724 (line-end-position))
725 (line-end-position)))))
726 (delete-region (match-beginning 0) (match-end 0))
727 (insert "\n\n")))))
729 (defun shr-indent ()
730 (when (> shr-indentation 0)
731 (insert
732 (if (not shr-use-fonts)
733 (make-string shr-indentation ?\s)
734 (propertize " "
735 'display
736 `(space :width (,shr-indentation)))))))
738 (defun shr-fontize-dom (dom &rest types)
739 (let ((start (point)))
740 (shr-generic dom)
741 (dolist (type types)
742 (shr-add-font start (point) type))))
744 ;; Add face to the region, but avoid putting the font properties on
745 ;; blank text at the start of the line, and the newline at the end, to
746 ;; avoid ugliness.
747 (defun shr-add-font (start end type)
748 (unless shr-inhibit-decoration
749 (save-excursion
750 (goto-char start)
751 (while (< (point) end)
752 (when (bolp)
753 (skip-chars-forward " "))
754 (add-face-text-property (point) (min (line-end-position) end) type t)
755 (if (< (line-end-position) end)
756 (forward-line 1)
757 (goto-char end))))))
759 (defun shr-mouse-browse-url (ev)
760 "Browse the URL under the mouse cursor."
761 (interactive "e")
762 (mouse-set-point ev)
763 (shr-browse-url))
765 (defun shr-browse-url (&optional external mouse-event)
766 "Browse the URL under point.
767 If EXTERNAL, browse the URL using `shr-external-browser'."
768 (interactive (list current-prefix-arg last-nonmenu-event))
769 (mouse-set-point mouse-event)
770 (let ((url (get-text-property (point) 'shr-url)))
771 (cond
772 ((not url)
773 (message "No link under point"))
774 ((string-match "^mailto:" url)
775 (browse-url-mail url))
777 (if external
778 (funcall shr-external-browser url)
779 (browse-url url))))))
781 (defun shr-save-contents (directory)
782 "Save the contents from URL in a file."
783 (interactive "DSave contents of URL to directory: ")
784 (let ((url (get-text-property (point) 'shr-url)))
785 (if (not url)
786 (message "No link under point")
787 (url-retrieve (shr-encode-url url)
788 'shr-store-contents (list url directory)
789 nil t))))
791 (defun shr-store-contents (status url directory)
792 (unless (plist-get status :error)
793 (when (or (search-forward "\n\n" nil t)
794 (search-forward "\r\n\r\n" nil t))
795 (write-region (point) (point-max)
796 (expand-file-name (file-name-nondirectory url)
797 directory)))))
799 (defun shr-image-fetched (status buffer start end &optional flags)
800 (let ((image-buffer (current-buffer)))
801 (when (and (buffer-name buffer)
802 (not (plist-get status :error)))
803 (url-store-in-cache image-buffer)
804 (when (or (search-forward "\n\n" nil t)
805 (search-forward "\r\n\r\n" nil t))
806 (let ((data (shr-parse-image-data)))
807 (with-current-buffer buffer
808 (save-excursion
809 (let ((alt (buffer-substring start end))
810 (properties (text-properties-at start))
811 (inhibit-read-only t))
812 (delete-region start end)
813 (goto-char start)
814 (funcall shr-put-image-function data alt flags)
815 (while properties
816 (let ((type (pop properties))
817 (value (pop properties)))
818 (unless (memq type '(display image-size))
819 (put-text-property start (point) type value))))))))))
820 (kill-buffer image-buffer)))
822 (defun shr-image-from-data (data)
823 "Return an image from the data: URI content DATA."
824 (when (string-match
825 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
826 data)
827 (let ((param (match-string 4 data))
828 (payload (url-unhex-string (match-string 5 data))))
829 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
830 (setq payload (base64-decode-string payload)))
831 payload)))
833 ;; Behind display-graphic-p test.
834 (declare-function image-size "image.c" (spec &optional pixels frame))
835 (declare-function image-animate "image" (image &optional index limit))
837 (defun shr-put-image (spec alt &optional flags)
838 "Insert image SPEC with a string ALT. Return image.
839 SPEC is either an image data blob, or a list where the first
840 element is the data blob and the second element is the content-type."
841 (if (display-graphic-p)
842 (let* ((size (cdr (assq 'size flags)))
843 (data (if (consp spec)
844 (car spec)
845 spec))
846 (content-type (and (consp spec)
847 (cadr spec)))
848 (start (point))
849 (image (cond
850 ((eq size 'original)
851 (create-image data nil t :ascent 100
852 :format content-type))
853 ((eq content-type 'image/svg+xml)
854 (create-image data 'svg t :ascent 100))
855 ((eq size 'full)
856 (ignore-errors
857 (shr-rescale-image data content-type)))
859 (ignore-errors
860 (shr-rescale-image data content-type))))))
861 (when image
862 ;; When inserting big-ish pictures, put them at the
863 ;; beginning of the line.
864 (when (and (> (current-column) 0)
865 (> (car (image-size image t)) 400))
866 (insert "\n"))
867 (if (eq size 'original)
868 (insert-sliced-image image (or alt "*") nil 20 1)
869 (insert-image image (or alt "*")))
870 (put-text-property start (point) 'image-size size)
871 (when (and shr-image-animate
872 (cond ((fboundp 'image-multi-frame-p)
873 ;; Only animate multi-frame things that specify a
874 ;; delay; eg animated gifs as opposed to
875 ;; multi-page tiffs. FIXME?
876 (cdr (image-multi-frame-p image)))
877 ((fboundp 'image-animated-p)
878 (image-animated-p image))))
879 (image-animate image nil 60)))
880 image)
881 (insert alt)))
883 (defun shr-rescale-image (data &optional content-type)
884 "Rescale DATA, if too big, to fit the current buffer."
885 (if (not (and (fboundp 'imagemagick-types)
886 (get-buffer-window (current-buffer))))
887 (create-image data nil t :ascent 100)
888 (let ((edges (window-inside-pixel-edges
889 (get-buffer-window (current-buffer)))))
890 (create-image
891 data 'imagemagick t
892 :ascent 100
893 :max-width (truncate (* shr-max-image-proportion
894 (- (nth 2 edges) (nth 0 edges))))
895 :max-height (truncate (* shr-max-image-proportion
896 (- (nth 3 edges) (nth 1 edges))))
897 :format content-type))))
899 ;; url-cache-extract autoloads url-cache.
900 (declare-function url-cache-create-filename "url-cache" (url))
901 (autoload 'mm-disable-multibyte "mm-util")
902 (autoload 'browse-url-mail "browse-url")
904 (defun shr-get-image-data (url)
905 "Get image data for URL.
906 Return a string with image data."
907 (with-temp-buffer
908 (mm-disable-multibyte)
909 (when (ignore-errors
910 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
912 (when (or (search-forward "\n\n" nil t)
913 (search-forward "\r\n\r\n" nil t))
914 (shr-parse-image-data)))))
916 (defun shr-parse-image-data ()
917 (let ((data (buffer-substring (point) (point-max)))
918 (content-type
919 (save-excursion
920 (save-restriction
921 (narrow-to-region (point-min) (point))
922 (let ((content-type (mail-fetch-field "content-type")))
923 (and content-type
924 ;; Remove any comments in the type string.
925 (intern (replace-regexp-in-string ";.*" "" content-type)
926 obarray)))))))
927 ;; SVG images may contain references to further images that we may
928 ;; want to block. So special-case these by parsing the XML data
929 ;; and remove the blocked bits.
930 (when (eq content-type 'image/svg+xml)
931 (setq data
932 (shr-dom-to-xml
933 (libxml-parse-xml-region (point) (point-max)))))
934 (list data content-type)))
936 (defun shr-image-displayer (content-function)
937 "Return a function to display an image.
938 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
939 is an argument. The function to be returned takes three arguments URL,
940 START, and END. Note that START and END should be markers."
941 `(lambda (url start end)
942 (when url
943 (if (string-match "\\`cid:" url)
944 ,(when content-function
945 `(let ((image (funcall ,content-function
946 (substring url (match-end 0)))))
947 (when image
948 (goto-char start)
949 (funcall shr-put-image-function
950 image (buffer-substring start end))
951 (delete-region (point) end))))
952 (url-retrieve url 'shr-image-fetched
953 (list (current-buffer) start end)
954 t t)))))
956 (defun shr-heading (dom &rest types)
957 (shr-ensure-paragraph)
958 (apply #'shr-fontize-dom dom types)
959 (shr-ensure-paragraph))
961 (defun shr-urlify (start url &optional title)
962 (shr-add-font start (point) 'shr-link)
963 (add-text-properties
964 start (point)
965 (list 'shr-url url
966 'help-echo (let ((iri (or (ignore-errors
967 (decode-coding-string
968 (url-unhex-string url)
969 'utf-8 t))
970 url)))
971 (if title (format "%s (%s)" iri title) iri))
972 'follow-link t
973 'mouse-face 'highlight
974 'keymap shr-map)))
976 (defun shr-encode-url (url)
977 "Encode URL."
978 (browse-url-url-encode-chars url "[)$ ]"))
980 (autoload 'shr-color-visible "shr-color")
981 (autoload 'shr-color->hexadecimal "shr-color")
983 (defun shr-color-check (fg bg)
984 "Check that FG is visible on BG.
985 Returns (fg bg) with corrected values.
986 Returns nil if the colors that would be used are the default
987 ones, in case fg and bg are nil."
988 (when (or fg bg)
989 (let ((fixed (cond ((null fg) 'fg)
990 ((null bg) 'bg))))
991 ;; Convert colors to hexadecimal, or set them to default.
992 (let ((fg (or (shr-color->hexadecimal fg)
993 (frame-parameter nil 'foreground-color)))
994 (bg (or (shr-color->hexadecimal bg)
995 (frame-parameter nil 'background-color))))
996 (cond ((eq fixed 'bg)
997 ;; Only return the new fg
998 (list nil (cadr (shr-color-visible bg fg t))))
999 ((eq fixed 'fg)
1000 ;; Invert args and results and return only the new bg
1001 (list (cadr (shr-color-visible fg bg t)) nil))
1003 (shr-color-visible bg fg)))))))
1005 (defun shr-colorize-region (start end fg &optional bg)
1006 (when (and (not shr-inhibit-decoration)
1007 (or fg bg))
1008 (let ((new-colors (shr-color-check fg bg)))
1009 (when new-colors
1010 (when fg
1011 (add-face-text-property start end
1012 (list :foreground (cadr new-colors))
1014 (when bg
1015 (add-face-text-property start end
1016 (list :background (car new-colors))
1017 t)))
1018 new-colors)))
1020 (defun shr-previous-newline-padding-width (width)
1021 (let ((overlays (overlays-at (point)))
1022 (previous-width 0))
1023 (if (null overlays)
1024 width
1025 (dolist (overlay overlays)
1026 (setq previous-width
1027 (+ previous-width
1028 (length (plist-get (overlay-properties overlay)
1029 'before-string)))))
1030 (+ width previous-width))))
1032 ;;; Tag-specific rendering rules.
1034 (defun shr-tag-body (dom)
1035 (let* ((start (point))
1036 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1037 (bgcolor (dom-attr dom 'bgcolor))
1038 (shr-stylesheet (list (cons 'color fgcolor)
1039 (cons 'background-color bgcolor))))
1040 (shr-generic dom)
1041 (shr-colorize-region start (point) fgcolor bgcolor)))
1043 (defun shr-tag-style (_dom)
1046 (defun shr-tag-script (_dom)
1049 (defun shr-tag-comment (_dom)
1052 (defun shr-dom-to-xml (dom)
1053 (with-temp-buffer
1054 (shr-dom-print dom)
1055 (buffer-string)))
1057 (defun shr-dom-print (dom)
1058 "Convert DOM into a string containing the xml representation."
1059 (insert (format "<%s" (dom-tag dom)))
1060 (dolist (attr (dom-attributes dom))
1061 ;; Ignore attributes that start with a colon because they are
1062 ;; private elements.
1063 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1064 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1065 (insert ">")
1066 (let (url)
1067 (dolist (elem (dom-children dom))
1068 (cond
1069 ((stringp elem)
1070 (insert elem))
1071 ((eq (dom-tag elem) 'comment)
1073 ((or (not (eq (dom-tag elem) 'image))
1074 ;; Filter out blocked elements inside the SVG image.
1075 (not (setq url (dom-attr elem ':xlink:href)))
1076 (not shr-blocked-images)
1077 (not (string-match shr-blocked-images url)))
1078 (insert " ")
1079 (shr-dom-print elem)))))
1080 (insert (format "</%s>" (dom-tag dom))))
1082 (defun shr-tag-svg (dom)
1083 (when (and (image-type-available-p 'svg)
1084 (not shr-inhibit-images))
1085 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1086 "SVG Image")))
1088 (defun shr-tag-sup (dom)
1089 (let ((start (point)))
1090 (shr-generic dom)
1091 (put-text-property start (point) 'display '(raise 0.5))))
1093 (defun shr-tag-sub (dom)
1094 (let ((start (point)))
1095 (shr-generic dom)
1096 (put-text-property start (point) 'display '(raise -0.5))))
1098 (defun shr-tag-label (dom)
1099 (shr-generic dom)
1100 (shr-ensure-paragraph))
1102 (defun shr-tag-p (dom)
1103 (shr-ensure-paragraph)
1104 (shr-generic dom)
1105 (shr-ensure-paragraph))
1107 (defun shr-tag-div (dom)
1108 (shr-ensure-newline)
1109 (shr-generic dom)
1110 (shr-ensure-newline))
1112 (defun shr-tag-s (dom)
1113 (shr-fontize-dom dom 'shr-strike-through))
1115 (defun shr-tag-del (dom)
1116 (shr-fontize-dom dom 'shr-strike-through))
1118 (defun shr-tag-b (dom)
1119 (shr-fontize-dom dom 'bold))
1121 (defun shr-tag-i (dom)
1122 (shr-fontize-dom dom 'italic))
1124 (defun shr-tag-em (dom)
1125 (shr-fontize-dom dom 'italic))
1127 (defun shr-tag-strong (dom)
1128 (shr-fontize-dom dom 'bold))
1130 (defun shr-tag-u (dom)
1131 (shr-fontize-dom dom 'underline))
1133 (defun shr-tag-tt (dom)
1134 (let ((shr-current-font 'default))
1135 (shr-generic dom)))
1137 (defun shr-parse-style (style)
1138 (when style
1139 (save-match-data
1140 (when (string-match "\n" style)
1141 (setq style (replace-match " " t t style))))
1142 (let ((plist nil))
1143 (dolist (elem (split-string style ";"))
1144 (when elem
1145 (setq elem (split-string elem ":"))
1146 (when (and (car elem)
1147 (cadr elem))
1148 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1149 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1150 (when (string-match " *!important\\'" value)
1151 (setq value (substring value 0 (match-beginning 0))))
1152 (unless (equal value "inherit")
1153 (push (cons (intern name obarray)
1154 value)
1155 plist))))))
1156 plist)))
1158 (defun shr-tag-base (dom)
1159 (when-let (base (dom-attr dom 'href))
1160 (setq shr-base (shr-parse-base base)))
1161 (shr-generic dom))
1163 (defun shr-tag-a (dom)
1164 (let ((url (dom-attr dom 'href))
1165 (title (dom-attr dom 'title))
1166 (start (point))
1167 shr-start)
1168 (shr-generic dom)
1169 (when (and shr-target-id
1170 (equal (dom-attr dom 'name) shr-target-id))
1171 ;; We have a zero-length <a name="foo"> element, so just
1172 ;; insert... something.
1173 (when (= start (point))
1174 (shr-ensure-newline)
1175 (insert " "))
1176 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1177 (when (and url
1178 (not shr-inhibit-decoration))
1179 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1181 (defun shr-tag-object (dom)
1182 (unless shr-inhibit-images
1183 (let ((start (point))
1184 url multimedia image)
1185 (when-let (type (dom-attr dom 'type))
1186 (when (string-match "\\`image/svg" type)
1187 (setq url (dom-attr dom 'data)
1188 image t)))
1189 (dolist (child (dom-non-text-children dom))
1190 (cond
1191 ((eq (dom-tag child) 'embed)
1192 (setq url (or url (dom-attr child 'src))
1193 multimedia t))
1194 ((and (eq (dom-tag child) 'param)
1195 (equal (dom-attr child 'name) "movie"))
1196 (setq url (or url (dom-attr child 'value))
1197 multimedia t))))
1198 (when url
1199 (cond
1200 (image
1201 (shr-tag-img dom url)
1202 (setq dom nil))
1203 (multimedia
1204 (shr-insert " [multimedia] ")
1205 (shr-urlify start (shr-expand-url url)))))
1206 (when dom
1207 (shr-generic dom)))))
1209 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1210 ("ogv" . 1.0)
1211 ("ogg" . 1.0)
1212 ("opus" . 1.0)
1213 ("flac" . 0.9)
1214 ("wav" . 0.5))
1215 "Preferences for media types.
1216 The key element should be a regexp matched against the type of the source or
1217 url if no type is specified. The value should be a float in the range 0.0 to
1218 1.0. Media elements with higher value are preferred."
1219 :version "24.4"
1220 :group 'shr
1221 :type '(alist :key-type regexp :value-type float))
1223 (defun shr--get-media-pref (elem)
1224 "Determine the preference for ELEM.
1225 The preference is a float determined from `shr-prefer-media-type'."
1226 (let ((type (dom-attr elem 'type))
1227 (p 0.0))
1228 (unless type
1229 (setq type (dom-attr elem 'src)))
1230 (when type
1231 (dolist (pref shr-prefer-media-type-alist)
1232 (when (and
1233 (> (cdr pref) p)
1234 (string-match-p (car pref) type))
1235 (setq p (cdr pref)))))
1238 (defun shr--extract-best-source (dom &optional url pref)
1239 "Extract the best `:src' property from <source> blocks in DOM."
1240 (setq pref (or pref -1.0))
1241 (let (new-pref)
1242 (dolist (elem (dom-non-text-children dom))
1243 (when (and (eq (dom-tag elem) 'source)
1244 (< pref
1245 (setq new-pref
1246 (shr--get-media-pref elem))))
1247 (setq pref new-pref
1248 url (dom-attr elem 'src))
1249 ;; libxml's html parser isn't HTML5 compliant and non terminated
1250 ;; source tags might end up as children. So recursion it is...
1251 (dolist (child (dom-non-text-children elem))
1252 (when (eq (dom-tag child) 'source)
1253 (let ((ret (shr--extract-best-source (list child) url pref)))
1254 (when (< pref (cdr ret))
1255 (setq url (car ret)
1256 pref (cdr ret)))))))))
1257 (cons url pref))
1259 (defun shr-tag-video (dom)
1260 (let ((image (dom-attr dom 'poster))
1261 (url (dom-attr dom 'src))
1262 (start (point)))
1263 (unless url
1264 (setq url (car (shr--extract-best-source dom))))
1265 (if image
1266 (shr-tag-img nil image)
1267 (shr-insert " [video] "))
1268 (shr-urlify start (shr-expand-url url))))
1270 (defun shr-tag-audio (dom)
1271 (let ((url (dom-attr dom 'src))
1272 (start (point)))
1273 (unless url
1274 (setq url (car (shr--extract-best-source dom))))
1275 (shr-insert " [audio] ")
1276 (shr-urlify start (shr-expand-url url))))
1278 (defun shr-tag-img (dom &optional url)
1279 (when (or url
1280 (and dom
1281 (> (length (dom-attr dom 'src)) 0)))
1282 (when (> (current-column) 0)
1283 (insert "\n"))
1284 (let ((alt (dom-attr dom 'alt))
1285 (url (shr-expand-url (or url (dom-attr dom 'src)))))
1286 (let ((start (point-marker)))
1287 (when (zerop (length alt))
1288 (setq alt "*"))
1289 (cond
1290 ((or (member (dom-attr dom 'height) '("0" "1"))
1291 (member (dom-attr dom 'width) '("0" "1")))
1292 ;; Ignore zero-sized or single-pixel images.
1294 ((and (not shr-inhibit-images)
1295 (string-match "\\`data:" url))
1296 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1297 (if image
1298 (funcall shr-put-image-function image alt)
1299 (insert alt))))
1300 ((and (not shr-inhibit-images)
1301 (string-match "\\`cid:" url))
1302 (let ((url (substring url (match-end 0)))
1303 image)
1304 (if (or (not shr-content-function)
1305 (not (setq image (funcall shr-content-function url))))
1306 (insert alt)
1307 (funcall shr-put-image-function image alt))))
1308 ((or shr-inhibit-images
1309 (and shr-blocked-images
1310 (string-match shr-blocked-images url)))
1311 (setq shr-start (point))
1312 (if (> (string-width alt) 8)
1313 (shr-insert (truncate-string-to-width alt 8))
1314 (shr-insert alt)))
1315 ((and (not shr-ignore-cache)
1316 (url-is-cached (shr-encode-url url)))
1317 (funcall shr-put-image-function (shr-get-image-data url) alt))
1319 (insert alt " ")
1320 (when (and shr-ignore-cache
1321 (url-is-cached (shr-encode-url url)))
1322 (let ((file (url-cache-create-filename (shr-encode-url url))))
1323 (when (file-exists-p file)
1324 (delete-file file))))
1325 (url-queue-retrieve
1326 (shr-encode-url url) 'shr-image-fetched
1327 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1328 t t)))
1329 (when (zerop shr-table-depth) ;; We are not in a table.
1330 (put-text-property start (point) 'keymap shr-map)
1331 (put-text-property start (point) 'shr-alt alt)
1332 (put-text-property start (point) 'image-url url)
1333 (put-text-property start (point) 'image-displayer
1334 (shr-image-displayer shr-content-function))
1335 (put-text-property start (point) 'help-echo
1336 (shr-fill-text
1337 (or (dom-attr dom 'title) alt))))))))
1339 (defun shr-tag-pre (dom)
1340 (let ((shr-folding-mode 'none)
1341 (shr-current-font 'default))
1342 (shr-ensure-newline)
1343 (shr-generic dom)
1344 (shr-ensure-newline)))
1346 (defun shr-tag-blockquote (dom)
1347 (shr-ensure-paragraph)
1348 (let ((start (point))
1349 (shr-indentation (+ shr-indentation
1350 (* 4 shr-table-separator-pixel-width))))
1351 (shr-generic dom)
1352 (shr-ensure-paragraph)
1353 (shr-mark-fill start)))
1355 (defun shr-tag-dl (dom)
1356 (shr-ensure-paragraph)
1357 (shr-generic dom)
1358 (shr-ensure-paragraph))
1360 (defun shr-tag-dt (dom)
1361 (shr-ensure-newline)
1362 (shr-generic dom)
1363 (shr-ensure-newline))
1365 (defun shr-tag-dd (dom)
1366 (shr-ensure-newline)
1367 (let ((shr-indentation (+ shr-indentation
1368 (* 4 shr-table-separator-pixel-width))))
1369 (shr-generic dom)))
1371 (defun shr-tag-ul (dom)
1372 (shr-ensure-paragraph)
1373 (let ((shr-list-mode 'ul))
1374 (shr-generic dom))
1375 (shr-ensure-paragraph))
1377 (defun shr-tag-ol (dom)
1378 (shr-ensure-paragraph)
1379 (let ((shr-list-mode 1))
1380 (shr-generic dom))
1381 (shr-ensure-paragraph))
1383 (defun shr-tag-li (dom)
1384 (shr-ensure-newline)
1385 (let ((start (point)))
1386 (let* ((bullet
1387 (if (numberp shr-list-mode)
1388 (prog1
1389 (format "%d " shr-list-mode)
1390 (setq shr-list-mode (1+ shr-list-mode)))
1391 shr-bullet)))
1392 (insert bullet)
1393 (shr-mark-fill start)
1394 (let ((shr-indentation (+ shr-indentation
1395 (shr-string-pixel-width bullet))))
1396 (put-text-property start (1+ start)
1397 'shr-continuation-indentation shr-indentation)
1398 (shr-generic dom)))))
1400 (defun shr-mark-fill (start)
1401 ;; We may not have inserted any text to fill.
1402 (unless (= start (point))
1403 (put-text-property start (1+ start)
1404 'shr-indentation shr-indentation)))
1406 (defun shr-tag-br (dom)
1407 (when (and (not (bobp))
1408 ;; Only add a newline if we break the current line, or
1409 ;; the previous line isn't a blank line.
1410 (or (not (bolp))
1411 (and (> (- (point) 2) (point-min))
1412 (not (= (char-after (- (point) 2)) ?\n)))))
1413 (insert "\n"))
1414 (shr-generic dom))
1416 (defun shr-tag-span (dom)
1417 (shr-generic dom))
1419 (defun shr-tag-h1 (dom)
1420 (shr-heading dom (if shr-use-fonts
1421 '(variable-pitch (:height 1.3 :weight bold))
1422 'bold)))
1424 (defun shr-tag-h2 (dom)
1425 (shr-heading dom 'bold))
1427 (defun shr-tag-h3 (dom)
1428 (shr-heading dom 'italic))
1430 (defun shr-tag-h4 (dom)
1431 (shr-heading dom))
1433 (defun shr-tag-h5 (dom)
1434 (shr-heading dom))
1436 (defun shr-tag-h6 (dom)
1437 (shr-heading dom))
1439 (defun shr-tag-hr (_dom)
1440 (shr-ensure-newline)
1441 (insert (make-string (if (not shr-use-fonts)
1442 shr-internal-width
1443 (1+ (/ shr-internal-width
1444 shr-table-separator-pixel-width)))
1445 shr-hr-line)
1446 "\n"))
1448 (defun shr-tag-title (dom)
1449 (shr-heading dom 'bold 'underline))
1451 (defun shr-tag-font (dom)
1452 (let* ((start (point))
1453 (color (dom-attr dom 'color))
1454 (shr-stylesheet (nconc (list (cons 'color color))
1455 shr-stylesheet)))
1456 (shr-generic dom)
1457 (when color
1458 (shr-colorize-region start (point) color
1459 (cdr (assq 'background-color shr-stylesheet))))))
1461 ;;; Table rendering algorithm.
1463 ;; Table rendering is the only complicated thing here. We do this by
1464 ;; first counting how many TDs there are in each TR, and registering
1465 ;; how wide they think they should be ("width=45%", etc). Then we
1466 ;; render each TD separately (this is done in temporary buffers, so
1467 ;; that we can use all the rendering machinery as if we were in the
1468 ;; main buffer). Now we know how much space each TD really takes, so
1469 ;; we then render everything again with the new widths, and finally
1470 ;; insert all these boxes into the main buffer.
1471 (defun shr-tag-table-1 (dom)
1472 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1473 (let* ((shr-inhibit-images t)
1474 (shr-table-depth (1+ shr-table-depth))
1475 (shr-kinsoku-shorten t)
1476 ;; Find all suggested widths.
1477 (columns (shr-column-specs dom))
1478 ;; Compute how many pixels wide each TD should be.
1479 (suggested-widths (shr-pro-rate-columns columns))
1480 ;; Do a "test rendering" to see how big each TD is (this can
1481 ;; be smaller (if there's little text) or bigger (if there's
1482 ;; unbreakable text).
1483 (elems (or (dom-attr dom 'shr-suggested-widths)
1484 (shr-make-table dom suggested-widths nil
1485 'shr-suggested-widths)))
1486 (sketch (loop for line in elems
1487 collect (mapcar #'car line)))
1488 (natural (loop for line in elems
1489 collect (mapcar #'cdr line)))
1490 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1491 ;; This probably won't work very well.
1492 (when (> (+ (loop for width across sketch-widths
1493 summing (1+ width))
1494 shr-indentation shr-table-separator-pixel-width)
1495 (frame-width))
1496 (setq truncate-lines t))
1497 ;; Then render the table again with these new "hard" widths.
1498 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1500 (defun shr-tag-table (dom)
1501 (shr-ensure-paragraph)
1502 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1503 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1504 (body (dom-non-text-children (or (dom-child-by-tag dom 'tbody)
1505 dom)))
1506 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1507 (bgcolor (dom-attr dom 'bgcolor))
1508 (start (point))
1509 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1510 shr-stylesheet))
1511 (nheader (if header (shr-max-columns header)))
1512 (nbody (if body (shr-max-columns body)))
1513 (nfooter (if footer (shr-max-columns footer))))
1514 (if (and (not caption)
1515 (not header)
1516 (not (dom-child-by-tag dom 'tbody))
1517 (not (dom-child-by-tag dom 'tr))
1518 (not footer))
1519 ;; The table is totally invalid and just contains random junk.
1520 ;; Try to output it anyway.
1521 (shr-generic dom)
1522 ;; It's a real table, so render it.
1523 (if (dom-attr dom 'shr-fixed-table)
1524 (shr-tag-table-1 dom)
1525 ;; Only fix up the table once.
1526 (let ((table
1527 (nconc
1528 (list 'table nil)
1529 (if caption `((tr nil (td nil ,@caption))))
1530 (cond
1531 (header
1532 (if footer
1533 ;; header + body + footer
1534 (if (= nheader nbody)
1535 (if (= nbody nfooter)
1536 `((tr nil (td nil (table nil
1537 (tbody nil ,@header
1538 ,@body ,@footer)))))
1539 (nconc `((tr nil (td nil (table nil
1540 (tbody nil ,@header
1541 ,@body)))))
1542 (if (= nfooter 1)
1543 footer
1544 `((tr nil (td nil (table
1545 nil (tbody
1546 nil ,@footer))))))))
1547 (nconc `((tr nil (td nil (table nil (tbody
1548 nil ,@header)))))
1549 (if (= nbody nfooter)
1550 `((tr nil (td nil (table
1551 nil (tbody nil ,@body
1552 ,@footer)))))
1553 (nconc `((tr nil (td nil (table
1554 nil (tbody nil
1555 ,@body)))))
1556 (if (= nfooter 1)
1557 footer
1558 `((tr nil (td nil (table
1560 (tbody
1562 ,@footer))))))))))
1563 ;; header + body
1564 (if (= nheader nbody)
1565 `((tr nil (td nil (table nil (tbody nil ,@header
1566 ,@body)))))
1567 (if (= nheader 1)
1568 `(,@header (tr nil (td nil (table
1569 nil (tbody nil ,@body)))))
1570 `((tr nil (td nil (table nil (tbody nil ,@header))))
1571 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
1572 (footer
1573 ;; body + footer
1574 (if (= nbody nfooter)
1575 `((tr nil (td nil (table
1576 nil (tbody nil ,@body ,@footer)))))
1577 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
1578 (if (= nfooter 1)
1579 footer
1580 `((tr nil (td nil (table
1581 nil (tbody nil ,@footer)))))))))
1582 (caption
1583 `((tr nil (td nil (table nil (tbody nil ,@body))))))
1584 (body)))))
1585 (dom-set-attribute table 'shr-fixed-table t)
1586 (setcdr dom (cdr table))
1587 (shr-tag-table-1 dom))))
1588 (when bgcolor
1589 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1590 bgcolor))
1591 ;; Finally, insert all the images after the table. The Emacs buffer
1592 ;; model isn't strong enough to allow us to put the images actually
1593 ;; into the tables.
1594 (when (zerop shr-table-depth)
1595 (save-excursion
1596 (shr-expand-alignments start (point)))
1597 (dolist (elem (dom-by-tag dom 'object))
1598 (shr-tag-object elem))
1599 (dolist (elem (dom-by-tag dom 'img))
1600 (shr-tag-img elem)))))
1602 (defun shr-insert-table (table widths)
1603 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1604 "collapse"))
1605 (shr-table-separator-length (if collapse 0 1))
1606 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
1607 (start (point)))
1608 (setq shr-table-id (1+ shr-table-id))
1609 (unless collapse
1610 (shr-insert-table-ruler widths))
1611 (dolist (row table)
1612 (let ((start (point))
1613 (align 0)
1614 (column-number 0)
1615 (height (let ((max 0))
1616 (dolist (column row)
1617 (setq max (max max (nth 2 column))))
1618 max)))
1619 (dotimes (i (max height 1))
1620 (shr-indent)
1621 (insert shr-table-vertical-line "\n"))
1622 (dolist (column row)
1623 (when (> (nth 2 column) -1)
1624 (goto-char start)
1625 ;; Sum up all the widths from the column. (There may be
1626 ;; more than one if this is a "colspan" column.)
1627 (dotimes (i (nth 4 column))
1628 ;; The colspan directive may be wrong and there may not be
1629 ;; that number of columns.
1630 (when (<= column-number (1- (length widths)))
1631 (setq align (+ align
1632 (aref widths column-number)
1633 (* 2 shr-table-separator-pixel-width))))
1634 (setq column-number (1+ column-number)))
1635 (let ((lines (nth 3 column))
1636 (pixel-align (if (not shr-use-fonts)
1637 (* align (frame-char-width))
1638 align)))
1639 (dolist (line lines)
1640 (end-of-line)
1641 (let ((start (point)))
1642 (insert
1643 line
1644 (propertize " "
1645 'display `(space :align-to (,pixel-align))
1646 'face (and (> (length line) 0)
1647 (get-text-property
1648 (1- (length line)) 'face line))
1649 'shr-table-indent shr-table-id)
1650 shr-table-vertical-line)
1651 (shr-colorize-region
1652 start (1- (point)) (nth 5 column) (nth 6 column)))
1653 (forward-line 1))
1654 ;; Add blank lines at padding at the bottom of the TD,
1655 ;; possibly.
1656 (dotimes (i (- height (length lines)))
1657 (end-of-line)
1658 (let ((start (point)))
1659 (insert (propertize " "
1660 'display `(space :align-to (,pixel-align))
1661 'shr-table-indent shr-table-id)
1662 shr-table-vertical-line)
1663 (shr-colorize-region
1664 start (1- (point)) (nth 5 column) (nth 6 column)))
1665 (forward-line 1))))))
1666 (unless collapse
1667 (shr-insert-table-ruler widths)))
1668 (unless (= start (point))
1669 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
1671 (defun shr-expand-alignments (start end)
1672 (while (< (setq start (next-single-property-change
1673 start 'shr-table-id nil end))
1674 end)
1675 (goto-char start)
1676 (let* ((shr-use-fonts t)
1677 (id (get-text-property (point) 'shr-table-id))
1678 (base (shr-pixel-column))
1679 elem)
1680 (when id
1681 (save-excursion
1682 (while (setq elem (text-property-any
1683 (point) end 'shr-table-indent id))
1684 (goto-char elem)
1685 (let ((align (get-text-property (point) 'display)))
1686 (put-text-property (point) (1+ (point)) 'display
1687 `(space :align-to (,(+ (car (nth 2 align))
1688 base)))))
1689 (forward-char 1)))))
1690 (setq start (1+ start))))
1692 (defun shr-insert-table-ruler (widths)
1693 (when shr-table-horizontal-line
1694 (when (and (bolp)
1695 (> shr-indentation 0))
1696 (shr-indent))
1697 (insert shr-table-corner)
1698 (let ((total-width 0))
1699 (dotimes (i (length widths))
1700 (setq total-width (+ total-width (aref widths i)
1701 (* shr-table-separator-pixel-width 2)))
1702 (insert (make-string (1+ (/ (aref widths i)
1703 shr-table-separator-pixel-width))
1704 shr-table-horizontal-line)
1705 (propertize " "
1706 'display `(space :align-to (,total-width))
1707 'shr-table-indent shr-table-id)
1708 shr-table-corner)))
1709 (insert "\n")))
1711 (defun shr-table-widths (table natural-table suggested-widths)
1712 (let* ((length (length suggested-widths))
1713 (widths (make-vector length 0))
1714 (natural-widths (make-vector length 0)))
1715 (dolist (row table)
1716 (let ((i 0))
1717 (dolist (column row)
1718 (aset widths i (max (aref widths i) column))
1719 (setq i (1+ i)))))
1720 (dolist (row natural-table)
1721 (let ((i 0))
1722 (dolist (column row)
1723 (aset natural-widths i (max (aref natural-widths i) column))
1724 (setq i (1+ i)))))
1725 (let ((extra (- (apply '+ (append suggested-widths nil))
1726 (apply '+ (append widths nil))
1727 (* shr-table-separator-pixel-width (1+ (length widths)))))
1728 (expanded-columns 0))
1729 ;; We have extra, unused space, so divide this space amongst the
1730 ;; columns.
1731 (when (> extra 0)
1732 ;; If the natural width is wider than the rendered width, we
1733 ;; want to allow the column to expand.
1734 (dotimes (i length)
1735 (when (> (aref natural-widths i) (aref widths i))
1736 (setq expanded-columns (1+ expanded-columns))))
1737 (dotimes (i length)
1738 (when (> (aref natural-widths i) (aref widths i))
1739 (aset widths i (min
1740 (aref natural-widths i)
1741 (+ (/ extra expanded-columns)
1742 (aref widths i))))))))
1743 widths))
1745 (defun shr-make-table (dom widths &optional fill storage-attribute)
1746 (or (cadr (assoc (list dom widths fill) shr-content-cache))
1747 (let ((data (shr-make-table-1 dom widths fill)))
1748 (push (list (list dom widths fill) data)
1749 shr-content-cache)
1750 (when storage-attribute
1751 (dom-set-attribute dom storage-attribute data))
1752 data)))
1754 (defun shr-make-table-1 (dom widths &optional fill)
1755 (let ((trs nil)
1756 (shr-inhibit-decoration (not fill))
1757 (rowspans (make-vector (length widths) 0))
1758 (colspan-remaining 0)
1759 colspan-width colspan-count
1760 width colspan)
1761 (dolist (row (dom-non-text-children dom))
1762 (when (eq (dom-tag row) 'tr)
1763 (let ((tds nil)
1764 (columns (dom-non-text-children row))
1765 (i 0)
1766 (width-column 0)
1767 column)
1768 (while (< i (length widths))
1769 ;; If we previously had a rowspan definition, then that
1770 ;; means that we now have a "missing" td/th element here.
1771 ;; So just insert a dummy, empty one to (sort of) emulate
1772 ;; rowspan.
1773 (setq column
1774 (if (zerop (aref rowspans i))
1775 (pop columns)
1776 (aset rowspans i (1- (aref rowspans i)))
1777 '(td)))
1778 (when (and (not (stringp column))
1779 (or (memq (dom-tag column) '(td th))
1780 (not column)))
1781 (when-let (span (dom-attr column 'rowspan))
1782 (aset rowspans i (+ (aref rowspans i)
1783 (1- (string-to-number span)))))
1784 ;; Sanity check for invalid column-spans.
1785 (when (>= width-column (length widths))
1786 (setq width-column 0))
1787 (setq width
1788 (if column
1789 (aref widths width-column)
1790 (* 10 shr-table-separator-pixel-width)))
1791 (when (setq colspan (dom-attr column 'colspan))
1792 (setq colspan (min (string-to-number colspan)
1793 ;; The colspan may be wrong, so
1794 ;; truncate it to the length of the
1795 ;; remaining columns.
1796 (- (length widths) i)))
1797 (dotimes (j (1- colspan))
1798 (setq width
1799 (if (> (+ i 1 j) (1- (length widths)))
1800 ;; If we have a colspan spec that's longer
1801 ;; than the table is wide, just use the last
1802 ;; width as the width.
1803 (aref widths (1- (length widths)))
1804 ;; Sum up the widths of the columns we're
1805 ;; spanning.
1806 (+ width
1807 shr-table-separator-length
1808 (aref widths (+ i 1 j))))))
1809 (setq width-column (+ width-column (1- colspan))
1810 colspan-count colspan
1811 colspan-remaining colspan))
1812 (when column
1813 (let ((data (shr-render-td column width fill)))
1814 (if (and (not fill)
1815 (> colspan-remaining 0))
1816 (progn
1817 (setq colspan-width (car data))
1818 (let ((this-width (/ colspan-width colspan-count)))
1819 (push (cons this-width (cadr data)) tds)
1820 (setq colspan-remaining (1- colspan-remaining))))
1821 (if (not fill)
1822 (push (cons (car data) (cadr data)) tds)
1823 (push data tds)))))
1824 (when (and colspan
1825 (> colspan 1))
1826 (dotimes (c (1- colspan))
1827 (setq i (1+ i))
1828 (push
1829 (if fill
1830 (list 0 0 -1 nil 1 nil nil)
1831 '(0 . 0))
1832 tds)))
1833 (setq i (1+ i)
1834 width-column (1+ width-column))))
1835 (push (nreverse tds) trs))))
1836 (nreverse trs)))
1838 (defun shr-pixel-buffer-width ()
1839 (if (not shr-use-fonts)
1840 (save-excursion
1841 (goto-char (point-min))
1842 (let ((max 0))
1843 (while (not (eobp))
1844 (end-of-line)
1845 (setq max (max max (current-column)))
1846 (forward-line 1))
1847 max))
1848 (if (get-buffer-window)
1849 (car (window-text-pixel-size nil (point-min) (point-max)))
1850 (save-window-excursion
1851 (set-window-buffer nil (current-buffer))
1852 (car (window-text-pixel-size nil (point-min) (point-max)))))))
1854 (defun shr-render-td (dom width fill)
1855 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
1856 (or (dom-attr dom cache)
1857 (and fill
1858 (let (result)
1859 (dolist (attr (dom-attributes dom))
1860 (let ((name (symbol-name (car attr))))
1861 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
1862 (let ((cache-width (string-to-number
1863 (match-string 1 name))))
1864 (when (and (>= cache-width width)
1865 (<= (car (cdr attr)) width))
1866 (setq result (cdr attr)))))))
1867 result))
1868 (let ((result (shr-render-td-1 dom width fill)))
1869 (dom-set-attribute dom cache result)
1870 result))))
1872 (defun shr-render-td-1 (dom width fill)
1873 (with-temp-buffer
1874 (let ((bgcolor (dom-attr dom 'bgcolor))
1875 (fgcolor (dom-attr dom 'fgcolor))
1876 (style (dom-attr dom 'style))
1877 (shr-stylesheet shr-stylesheet)
1878 (max-width 0)
1879 natural-width)
1880 (when style
1881 (setq style (and (string-match "color" style)
1882 (shr-parse-style style))))
1883 (when bgcolor
1884 (setq style (nconc (list (cons 'background-color bgcolor))
1885 style)))
1886 (when fgcolor
1887 (setq style (nconc (list (cons 'color fgcolor)) style)))
1888 (when style
1889 (setq shr-stylesheet (append style shr-stylesheet)))
1890 (let ((shr-internal-width width)
1891 (shr-indentation 0))
1892 (shr-descend dom))
1893 (save-window-excursion
1894 (set-window-buffer nil (current-buffer))
1895 (unless fill
1896 (setq natural-width
1897 (or (dom-attr dom 'shr-td-cache-natural)
1898 (let ((natural (max (shr-pixel-buffer-width)
1899 (shr-dom-max-natural-width dom 0))))
1900 (dom-set-attribute dom 'shr-td-cache-natural natural)
1901 natural))))
1902 (if (and natural-width
1903 (<= natural-width width))
1904 (setq max-width natural-width)
1905 (let ((shr-internal-width width))
1906 (shr-fill-lines (point-min) (point-max))
1907 (setq max-width (shr-pixel-buffer-width)))))
1908 (goto-char (point-max))
1909 ;; Delete padding at the bottom of the TDs.
1910 (delete-region
1911 (point)
1912 (progn
1913 (skip-chars-backward " \t\n")
1914 (end-of-line)
1915 (point)))
1916 (goto-char (point-min))
1917 (list max-width
1918 natural-width
1919 (count-lines (point-min) (point-max))
1920 (split-string (buffer-string) "\n")
1921 (if (dom-attr dom 'colspan)
1922 (string-to-number (dom-attr dom 'colspan))
1924 (cdr (assq 'color shr-stylesheet))
1925 (cdr (assq 'background-color shr-stylesheet))))))
1927 (defun shr-dom-max-natural-width (dom max)
1928 (if (eq (dom-tag dom) 'table)
1929 (max max (or
1930 (loop for line in (dom-attr dom 'shr-suggested-widths)
1931 maximize (+
1932 shr-table-separator-length
1933 (loop for elem in line
1934 summing
1935 (+ (cdr elem)
1936 (* 2 shr-table-separator-length)))))
1938 (dolist (child (dom-children dom))
1939 (unless (stringp child)
1940 (setq max (max (shr-dom-max-natural-width child max)))))
1941 max))
1943 (defun shr-buffer-width ()
1944 (goto-char (point-min))
1945 (let ((max 0))
1946 (while (not (eobp))
1947 (end-of-line)
1948 (setq max (max max (current-column)))
1949 (forward-line 1))
1950 max))
1952 (defun shr-pro-rate-columns (columns)
1953 (let ((total-percentage 0)
1954 (widths (make-vector (length columns) 0)))
1955 (dotimes (i (length columns))
1956 (setq total-percentage (+ total-percentage (aref columns i))))
1957 (setq total-percentage (/ 1.0 total-percentage))
1958 (dotimes (i (length columns))
1959 (aset widths i (max (truncate (* (aref columns i)
1960 total-percentage
1961 (- shr-internal-width
1962 (* (1+ (length columns))
1963 shr-table-separator-pixel-width))))
1964 10)))
1965 widths))
1967 ;; Return a summary of the number and shape of the TDs in the table.
1968 (defun shr-column-specs (dom)
1969 (let ((columns (make-vector (shr-max-columns dom) 1)))
1970 (dolist (row (dom-non-text-children dom))
1971 (when (eq (dom-tag row) 'tr)
1972 (let ((i 0))
1973 (dolist (column (dom-non-text-children row))
1974 (when (memq (dom-tag column) '(td th))
1975 (let ((width (dom-attr column 'width)))
1976 (when (and width
1977 (string-match "\\([0-9]+\\)%" width)
1978 (not (zerop (setq width (string-to-number
1979 (match-string 1 width))))))
1980 (aset columns i (/ width 100.0))))
1981 (setq i (1+ i)))))))
1982 columns))
1984 (defun shr-count (dom elem)
1985 (let ((i 0))
1986 (dolist (sub (dom-children dom))
1987 (when (and (not (stringp sub))
1988 (eq (dom-tag sub) elem))
1989 (setq i (1+ i))))
1992 (defun shr-max-columns (dom)
1993 (let ((max 0))
1994 (dolist (row (dom-children dom))
1995 (when (and (not (stringp row))
1996 (eq (dom-tag row) 'tr))
1997 (setq max (max max (+ (shr-count row 'td)
1998 (shr-count row 'th))))))
1999 max))
2001 (provide 'shr)
2003 ;; Local Variables:
2004 ;; coding: utf-8
2005 ;; End:
2007 ;;; shr.el ends here