[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

w3m-url-valid



>> On Tue, 18 Jul 2017 17:30:06 +0900
>> yamaoka@xxxxxxx (Katsumi Yamaoka) said as follows:

>Q2.
>========
>Why does the function `w3m-url-valid' only check if a given
>string does not match `w3m-url-invalid-regexp' ("\\`http:///")?
>IOW, why is this judged valid?

>(and (w3m-url-valid "ptth://www.gnu.org") 'VALID) => VALID
>--------
>なぜ関数 `w3m-url-valid' は、与えられた文字列が
>`w3m-url-invalid-regexp' ("\\`http:///") に合致しないかどうか
>のチェックしかしないのですか? 言い換えると、これはなぜ有効と
>判定されますか?  --- (w3m-url-valid "ptth://www.gnu.org")

w3m-url-valid() was introduced at 2002-10-23.  Its changelog entry says
as follows:

    2002-10-23  Hideyuki SHIRAI  <shirai@xxxxxxxxxxx>

        * w3m.el (w3m-toggle-inline-images-internal)
        (w3m-toggle-inline-image, w3m-view-this-url, w3m-view-this-url)
        (w3m-submit-form, w3m-external-view, w3m-view-image)
        (w3m-save-image, w3m-view-url-with-external-browser)
        (w3m-download-this-url, w3m-edit-this-url, w3m-goto-url)
        (w3m-goto-url-with-timer, w3m-safe-view-this-url)
        (w3m-safe-view-this-url): Check validity of the URL.
        (w3m-print-current-url, w3m-edit-current-url): Check `w3m-current-url'.

        * w3m-util.el (w3m-url-fallback-base): Move from `w3m.el'.
        (w3m-url-invalid-regexp): New constant.
        (w3m-url-valid): New inline function.

        * mew-w3m.el (mew-mime-text/html-w3m): Remove any properties from
        `xref'.

Its difference on w3m.el and w3m-util.el is attached at the end of this
message.

This change was introduced as the result of the discussion of
"[emacs-w3m:04095] cookie-get error at text/html part".
Especially, its meaning was described at "[emacs-w3m:04101] Re:
cookie-get error at text/html part".

Then, I conclude as follows:

(1) `w3m-url-fallback-base' is considered as two meanings.  The first
meaning is described at [emacs-w3m:12757], and the second meaning is the
impossible value of the URI string described at [emacs-w3m:04101].

(2) And, w3m-url-valid() is not considered as a generic validation
function.  It was simply designed as an ad-hoc function to avoid
handling errors of URIs for the cases when no base URI is given.

-- 
TSUCHIYA Masatoshi
Index: w3m.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m.el,v
retrieving revision 1.755
retrieving revision 1.756
diff -u -u -r1.755 -r1.756
--- w3m.el	22 Oct 2002 13:55:39 -0000	1.755
+++ w3m.el	23 Oct 2002 10:54:02 -0000	1.756
@@ -138,7 +138,7 @@
 
 (defconst emacs-w3m-version
   (eval-when-compile
-    (let ((rev "$Revision: 1.755 $"))
+    (let ((rev "$Revision: 1.756 $"))
       (and (string-match "\\.\\([0-9]+\\) \$$" rev)
 	   (format "1.3.%d"
 		   (- (string-to-number (match-string 1 rev)) 642)))))
@@ -1187,7 +1187,7 @@
 	       ("Nu" . 77) ("Xi" . 78) ("Omicron" . 79) ("Pi" . 80)
 	       ("Rho" . 81) ; No ("Sigmaf" . 82)
 	       ("Sigma" . 83) ("Tau" . 84) ("Upsilon" . 85) ("Phi" . 86)
-	       ("Chi" . 87) ("Psi" . 88) ("Omega" . 89) 
+	       ("Chi" . 87) ("Psi" . 88) ("Omega" . 89)
 	       ("alpha" . 97) ("beta" . 98) ("gamma" . 99) ("delta" . 100)
 	       ("epsilon" . 101) ("zeta" . 102) ("eta" . 103) ("theta" . 104)
 	       ("iota" . 105) ("kappa" . 106) ("lambda" . 107) ("mu" . 108)
@@ -2158,7 +2158,7 @@
 	  (pre name)
 	  (post ""))
       (if (not strict)
-	  (while (and (null val) 
+	  (while (and (null val)
 		      (< 0 (length pre))
 		      (null (setq val (intern-soft pre w3m-entity-db))))
 	    (setq post (concat (substring pre -1) post)
@@ -2365,7 +2365,7 @@
 		     '(w3m-image-dummy t w3m-image "dummy"))
 		    (setq end (point)))
 		(goto-char cur-point)
-		(when iurl
+		(when (w3m-url-valid iurl)
 		  (w3m-process-with-null-handler
 		    (lexical-let ((start (set-marker (make-marker) start))
 				  (end (set-marker (make-marker) end))
@@ -2435,7 +2435,7 @@
 	(scale (get-text-property (point) 'w3m-image-scale)))
     (if (and scale (equal status 'off))
 	(w3m-zoom-in-image 0)
-      (if url
+      (if (w3m-url-valid url)
 	  (progn
 	    (if force (setq status 'off))
 	    (w3m-toggle-inline-images-internal status no-cache url))
@@ -4172,8 +4172,6 @@
 refered in `w3m-expand-url' to keep backward compatibility which is
 described in Section 5.2 of RFC 2396.")
 
-(defconst w3m-url-fallback-base "http:///")
-
 (defun w3m-expand-url (url &optional base)
   "Convert URL to absolute, and canonicalize it."
   (save-match-data
@@ -4293,12 +4291,12 @@
   (interactive (if (member current-prefix-arg '(2 (16)))
 		   (list nil t)
 		 (list current-prefix-arg nil)))
-  (let ((url (w3m-anchor))
+  (let ((url (w3m-url-valid (w3m-anchor)))
 	(act (w3m-action)))
     (cond
      (act (eval act))
      (url (w3m-view-this-url-1 url arg new-session))
-     ((w3m-image)
+     ((w3m-url-valid (w3m-image))
       (if (w3m-display-graphic-p)
 	  (w3m-toggle-inline-image)
 	(w3m-view-image)))
@@ -4329,47 +4327,50 @@
   "Submit form at point."
   (interactive)
   (let ((submit (w3m-submit)))
-    (if submit
+    (if (and submit
+	     w3m-current-url
+	     (w3m-url-valid w3m-current-url))
 	(eval submit)
       (w3m-display-message "Can't Submit at this point"))))
 
 (defun w3m-external-view (url &optional no-cache handler)
-  (lexical-let ((url url)
-		(no-cache no-cache))
-    (w3m-process-do
-	(type (w3m-content-type url no-cache handler))
-      (when type
-	(lexical-let ((method (nth 2 (assoc type w3m-content-type-alist))))
-	  (cond
-	   ((not method)
-	    (if (w3m-url-local-p url)
-		(error "No method to view `%s' is registered. Use `w3m-edit-this-url'"
-		       (file-name-nondirectory (w3m-url-to-file-name url)))
-	      (w3m-download url nil no-cache handler)))
-	   ((functionp method)
-	    (funcall method url))
-	   ((consp method)
-	    (lexical-let
-		((command (w3m-which-command (car method)))
-		 (arguments (cdr method))
-		 (file (make-temp-name
-			(expand-file-name "w3mel" w3m-profile-directory)))
-		 suffix)
-	      (setq suffix (file-name-nondirectory url))
-	      (when (string-match "\\.[a-zA-Z0-9]+$" suffix)
-		(setq suffix (match-string 0 suffix))
-		(when (< (length suffix) 5)
-		  (setq file (concat file suffix))))
-	      (cond
-	       ((and command (memq 'file arguments))
-		(w3m-process-do
-		    (success (w3m-download url file no-cache handler))
-		  (when success
-		    (w3m-external-view-file command file url arguments))))
-	       (command
-		(w3m-external-view-file command nil url arguments))
-	       (t
-		(w3m-download url nil no-cache handler)))))))))))
+  (when (w3m-url-valid url)
+    (lexical-let ((url url)
+		  (no-cache no-cache))
+      (w3m-process-do
+	  (type (w3m-content-type url no-cache handler))
+	(when type
+	  (lexical-let ((method (nth 2 (assoc type w3m-content-type-alist))))
+	    (cond
+	     ((not method)
+	      (if (w3m-url-local-p url)
+		  (error "No method to view `%s' is registered. Use `w3m-edit-this-url'"
+			 (file-name-nondirectory (w3m-url-to-file-name url)))
+		(w3m-download url nil no-cache handler)))
+	     ((functionp method)
+	      (funcall method url))
+	     ((consp method)
+	      (lexical-let
+		  ((command (w3m-which-command (car method)))
+		   (arguments (cdr method))
+		   (file (make-temp-name
+			  (expand-file-name "w3mel" w3m-profile-directory)))
+		   suffix)
+		(setq suffix (file-name-nondirectory url))
+		(when (string-match "\\.[a-zA-Z0-9]+$" suffix)
+		  (setq suffix (match-string 0 suffix))
+		  (when (< (length suffix) 5)
+		    (setq file (concat file suffix))))
+		(cond
+		 ((and command (memq 'file arguments))
+		  (w3m-process-do
+		      (success (w3m-download url file no-cache handler))
+		    (when success
+		      (w3m-external-view-file command file url arguments))))
+		 (command
+		  (w3m-external-view-file command nil url arguments))
+		 (t
+		  (w3m-download url nil no-cache handler))))))))))))
 
 (defun w3m-external-view-file (command file url arguments)
   ;; The 3rd argument `url' is necessary to handle the constant `url'
@@ -4406,7 +4407,7 @@
 (defun w3m-view-image ()
   "View the image under point."
   (interactive)
-  (let ((url (w3m-image)))
+  (let ((url (w3m-url-valid (w3m-image))))
     (if url
 	(w3m-external-view url)
       (w3m-display-message "No image at point"))))
@@ -4414,7 +4415,7 @@
 (defun w3m-save-image ()
   "Save the image under point to a file."
   (interactive)
-  (let ((url (w3m-image)))
+  (let ((url (w3m-url-valid (w3m-image))))
     (if url
 	(w3m-download url)
       (w3m-display-message "No image at point"))))
@@ -4427,15 +4428,17 @@
 		   (w3m-image))
 		 (when (y-or-n-p (format "Browse <%s> ? " w3m-current-url))
 		   w3m-current-url))))
-    (when url
-      (message "Browsing <%s>..." url)
-      (w3m-external-view url))))
+    (if (w3m-url-valid url)
+	(progn
+	  (message "Browsing <%s>..." url)
+	  (w3m-external-view url))
+      (w3m-display-message "No URL at point"))))
 
 (defun w3m-download-this-url ()
   "Download the file or the image which pointed by the link under cursor."
   (interactive)
   (let ((url (or (w3m-anchor) (w3m-image))))
-    (if url
+    (if (w3m-url-valid url)
 	(lexical-let ((pos (point-marker))
 		      (curl w3m-current-url))
 	  (w3m-process-with-null-handler
@@ -4453,8 +4456,9 @@
 (defun w3m-print-current-url ()
   "Print the URL of current page and push it into kill-ring."
   (interactive)
-  (kill-new w3m-current-url)
-  (w3m-display-message "%s" w3m-current-url))
+  (when w3m-current-url
+    (kill-new w3m-current-url)
+    (w3m-display-message "%s" w3m-current-url)))
 
 (defun w3m-print-this-url (&optional arg)
   "Print the URL of the link under point."
@@ -4530,14 +4534,17 @@
 (defun w3m-edit-current-url ()
   "Edit the local file pointed by URL of current page."
   (interactive)
-  (w3m-edit-url w3m-current-url))
+  (if w3m-current-url
+      (w3m-edit-url w3m-current-url)
+    (w3m-display-message "No URL")))
+
 
 (defun w3m-edit-this-url (&optional url)
   "Edit the local file pointed by URL under point."
   (interactive)
   (unless url
     (setq url (w3m-anchor)))
-  (if url
+  (if (w3m-url-valid url)
       (w3m-edit-url url)
     (w3m-display-message "No URL at point")))
 
@@ -5757,7 +5764,7 @@
 	 (string-match "\\`ftp://" url)
 	 (not (string= "text/html" (w3m-local-content-type url))))
     (w3m-goto-ftp-url url))
-   (t
+   ((w3m-url-valid url)
     (w3m-buffer-setup)			; Setup buffer.
     (w3m-arrived-setup)			; Setup arrived database.
     (switch-to-buffer (current-buffer))
@@ -5772,7 +5779,7 @@
     (w3m-history-store-position)
     ;; Access url group
     (if (string-match "\\`group:" url)
-	(let ((urls (mapcar 'w3m-url-decode-string 
+	(let ((urls (mapcar 'w3m-url-decode-string
 			    (split-string (substring url (match-end 0)) "&"))))
 	  (w3m-process-do
 	      (type (prog1
@@ -5796,7 +5803,7 @@
 	  (w3m-history-plist-put :forms w3m-current-forms nil nil t))
 	;; Set current forms using the history structure.
 	(when (setq w3m-current-forms
-		    (when (and (null post-data) ; If post, always reload.
+		    (when (and (null post-data)	; If post, always reload.
 			       (w3m-cache-available-p url))
 		      (w3m-history-plist-get :forms url nil t)))
 	  ;; Mark that the form is from history structure.
@@ -5885,7 +5892,8 @@
 	      ;; restore position must call after hooks for localcgi.
 	      (when (or reload redisplay)
 		(w3m-history-restore-position))
-	      (w3m-refresh-at-time)))))))))
+	      (w3m-refresh-at-time)))))))
+   (t (w3m-message "Invalid URL: %s" url))))
 
 (defun w3m-current-directory (url)
   (let (file)
@@ -5918,7 +5926,7 @@
 
 (defun w3m-goto-url-with-timer (url buffer)
   "Run the command `w3m-goto-url' from the timer of refresh."
-  (when (and url buffer (get-buffer buffer))
+  (when (and (w3m-url-valid url) buffer (get-buffer buffer))
     (if (get-buffer-window buffer)
 	(save-selected-window
 	  (pop-to-buffer buffer)
@@ -6914,13 +6922,14 @@
 vicious forms is viewed, this command should be used instead of
 `w3m-view-this-url'."
   (interactive)
-  (let ((url (w3m-anchor)))
+  (let ((url (w3m-url-valid (w3m-anchor))))
     (cond
      (url (w3m url))
-     ((w3m-image)
+     ((w3m-url-valid (w3m-image))
       (if (w3m-display-graphic-p)
 	  (w3m-toggle-inline-image)
-	(w3m-view-image))))))
+	(w3m-view-image)))
+     (t (w3m-message "No URL at point")))))
 
 (defun w3m-mouse-safe-view-this-url (event)
   "Perform the command `w3m-safe-view-this-url' by the mouse event."
Index: w3m-util.el
===================================================================
RCS file: /storage/cvsroot/emacs-w3m/w3m-util.el,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -u -r1.37 -r1.38
--- w3m-util.el	21 Oct 2002 13:02:02 -0000	1.37
+++ w3m-util.el	23 Oct 2002 10:54:03 -0000	1.38
@@ -316,6 +316,12 @@
 
 
 ;;; Miscellaneous:
+(defconst w3m-url-fallback-base "http:///")
+(defconst w3m-url-invalid-regexp "\\`http:///")
+
+(defsubst w3m-url-valid (url)
+  (and url (not (string-match w3m-url-invalid-regexp url))
+       url))
 
 (defmacro w3m-tag-regexp-of (&rest names)
   "Return a regexp string, not a funtion form.  A regexp should match tags