JavaScript 7.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : JavaScript.c                           */
17 18 19
/*                                                 */
/* Purpose: JavaScript support.                    */
/*                                                 */
20
/* Author : A.D.Hodgkinson                         */
21 22
/*                                                 */
/* History: 24-Jul-97: Created.                    */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/***************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "swis.h"

#include "HTMLLib.h" /* HTML library API, Which will include html2_ext.h, tags.h and struct.h */

#include "wimp.h"
#include "wimplib.h"
#include "event.h"

#include "svcprint.h"
#include "Global.h"
#include "Utils.h"

41 42 43
#include "Browser.h"
#include "FetchPage.h"
#include "Frames.h"
44
#include "Customer.h"
45
#include "URLutils.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

#include "JavaScript.h"

/*************************************************/
/* javascript_body_onload()                      */
/*                                               */
/* When a page has finished fetching the main    */
/* page data, a BODY tag may specify some        */
/* JavaScript action. This functon should be     */
/* called to carry out that action.              */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             with a filled in 'onload' field   */
/*             describing the contents of the    */
/*             onLoad command in the HTML.       */
/*************************************************/

_kernel_oserror * javascript_body_onload(browser_data * b)
{
65 66 67 68 69 70
  _kernel_oserror * e = NULL;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_body_onload: Called\n");
  #endif

71 72
  #ifdef CUSTOMER_SPECIAL

73 74 75 76 77 78
    /* Only call customer_ functions if on the customer's site */

    if (strstr(browser_current_url(b), "www.customer.com"))
    {
      e = customer_body_onload(b);
    }
79 80 81

  #endif

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  b->onload = NULL;

  return e;
}

/*************************************************/
/* javascript_href_onclick()                     */
/*                                               */
/* When something is clicked upon that has an    */
/* onClick attribute specified for it, this      */
/* function is called with the details of that   */
/* attribute.                                    */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the item clicked on;  */
/*                                               */
/*             Pointer to the token representing */
99 100 101 102 103 104 105 106 107
/*             that item;                        */
/*                                               */
/*             Pointer to an int, in which 1 is  */
/*             written if the contents of the    */
/*             HREF attribute in the link that   */
/*             held the onClick should be        */
/*             ignored (else 0 is written) -     */
/*             this may be NULL if you're not    */
/*             interested.                       */
108 109
/*************************************************/

110
_kernel_oserror * javascript_href_onclick(browser_data * b, HStream * t, int * ignore)
111 112 113 114 115 116 117
{
  _kernel_oserror * e = NULL;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_href_onclick: Called\n");
  #endif

118 119
  if (ignore) *ignore = 0;

120 121 122 123 124 125
  #ifdef CUSTOMER_SPECIAL

    /* Only call customer_ functions if on the customer's site */

    if (strstr(browser_current_url(b), "www.customer.com"))
    {
126
      e = customer_href_onclick(b, t, ignore);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    }

  #endif

  return e;
}

/*************************************************/
/* javascript_window_open()                      */
/*                                               */
/* Opens a given URL in a given target window.   */
/*                                               */
/* Parameters: Pointer to a browser_data struct  */
/*             relevant to the window_open call; */
/*                                               */
/*             1 if the specified target window  */
/*             must be found to proceed, else 0  */
/*             (if the target isn't found the    */
/*             URL is opened in the given        */
/*             browser instead);                 */
/*                                               */
/*             1 if the URL displayed in the     */
/*             target is to be recorded in the   */
/*             History before the new one is     */
/*             fetched, else 0;                  */
/*                                               */
/*             Pointer to the URL to fetch;      */
/*                                               */
/*             Pointer to the target (or NULL /  */
/*             a null string for the given       */
/*             browser).                         */
/*************************************************/

_kernel_oserror * javascript_window_open(browser_data * b, int must_find, int record, char * url, char * target)
{
  browser_data * targetted = b;

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_window_open: Called for %p with '%s' and target '%s'\n",b,url,target);
  #endif

  /* Exit if no URL or base browser is given */

  if (!b || !url || !*url) return NULL;

  /* If a target is specified, try and find it */

  if (target && *target)
  {
176
    browser_data * ancestor = utils_ancestor(b);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Proceeding with target '%s'\n",target);
    #endif

    targetted = frames_find_named(ancestor, target);

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: targetted = %p\n",targetted);
    #endif

    /* If the target can't be found and the parameters specify it must be, */
    /* then exit; else target the base browser instead.                    */

    if (!targetted)
    {
      if (must_find) return NULL;
      else targetted = b;
    }
  }

  /* Open the URL */

  #ifdef TRACE
    if (tl & (1u<<24)) Printf("javascript_window_open: Opening to target %p\n",targetted);
  #endif

  /* Relativise the URL if necessary */

206
  if (!strstr(url, HTTPmethod ProtocolSeparator)) /* (URLutils.h) */
207
  {
208
    browser_data * ancestor = utils_ancestor(targetted);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    char         * r_url;

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Relativising URL\n");
    #endif

    /* Need to go down a sequence of options for which URL to relativise to! */

    r_url = browser_current_url(targetted);

    if (!r_url) r_url = browser_fetch_url(targetted);
    if (!r_url) r_url = browser_current_url(ancestor);
    if (!r_url) r_url = browser_fetch_url(ancestor);

    url = HtmlRelativiseURL(r_url, url, targetted->stream);

    #ifdef TRACE
      if (tl & (1u<<24)) Printf("javascript_window_open: Relativised URL is '%s'\n",url);
    #endif
  }
229

230
  return fetchpage_new(targetted, url, record, 0);
231
}