Debian Codegraph @

README.md

raw symbols SCIP
1 # text
2
3 A [RequireJS](http://requirejs.org)/AMD loader plugin for loading text
4 resources.
5
6 Known to work in RequireJS, but should work in other AMD loaders that support
7 the same loader plugin API.
8
9 ## Docs
10
11 See the [RequireJS API text section](http://requirejs.org/docs/api.html#text).
12
13 ## Latest release
14
15 The latest release is always available from [the "latest" tag](https://raw.github.com/requirejs/text/latest/text.js).
16
17 It can also be installed using [volo](https://github.com/volojs/volo):
18
19 volo add requirejs/text
20
21 If using npm:
22
23 npm install requirejs/text
24
25 ## Usage
26
27 It is nice to build HTML using regular HTML tags, instead of building up DOM
28 structures in script. However, there is no good way to embed HTML in a
29 JavaScript file. The best that can be done is using a string of HTML, but that
30 can be hard to manage, particularly for multi-line HTML.
31
32 The text.js AMD loader plugin can help with this issue. It will automatically be
33 loaded if the text! prefix is used for a dependency. Download the plugin and put
34 it in the app's [baseUrl](http://requirejs.org/docs/api.html#config-baseUrl)
35 directory (or use the [paths config](http://requirejs.org/docs/api.html#config-paths) to place it in other areas).
36
37 You can specify a text file resource as a dependency like so:
38
39 ```javascript
40 require(["some/module", "text!some/module.html", "text!some/module.css"],
41 function(module, html, css) {
42 //the html variable will be the text
43 //of the some/module.html file
44 //the css variable will be the text
45 //of the some/module.css file.
46 }
47 );
48 ```
49
50 Notice the .html and .css suffixes to specify the extension of the file. The
51 "some/module" part of the path will be resolved according to normal module name
52 resolution: it will use the **baseUrl** and **paths** [configuration
53 options](http://requirejs.org/docs/api.html#config) to map that name to a path.
54
55 For HTML/XML/SVG files, there is another option. You can pass !strip, which
56 strips XML declarations so that external SVG and XML documents can be added to a
57 document without worry. Also, if the string is an HTML document, only the part
58 inside the body tag is returned. Example:
59
60 ```javascript
61 require(["text!some/module.html!strip"],
62 function(html) {
63 //the html variable will be the text of the
64 //some/module.html file, but only the part
65 //inside the body tag.
66 }
67 );
68 ```
69
70 The text files are loaded via asynchronous XMLHttpRequest (XHR) calls, so you
71 can only fetch files from the same domain as the web page (see **XHR
72 restrictions** below).
73
74 However, [the RequireJS optimizer](http://requirejs.org/docs/optimization.html)
75 will inline any text! references with the actual text file contents into the
76 modules, so after a build, the modules that have text! dependencies can be used
77 from other domains.
78
79 ## Configuration
80
81 ### XHR restrictions
82
83 The text plugin works by using XMLHttpRequest (XHR) to fetch the text for the
84 resources it handles.
85
86 However, XHR calls have some restrictions, due to browser/web security policies:
87
88 1) Many browsers do not allow file:// access to just any file. You are better
89 off serving the application from a local web server than using local file://
90 URLs. You will likely run into trouble otherwise.
91
92 2) There are restrictions for using XHR to access files on another web domain.
93 While CORS can help enable the server for cross-domain access, doing so must
94 be done with care (in particular if you also host an API from that domain),
95 and not all browsers support CORS.
96
97 So if the text plugin determines that the request for the resource is on another
98 domain, it will try to access a ".js" version of the resource by using a
99 script tag. Script tag GET requests are allowed across domains. The .js version
100 of the resource should just be a script with a define() call in it that returns
101 a string for the module value.
102
103 Example: if the resource is 'text!example.html' and that resolves to a path
104 on another web domain, the text plugin will do a script tag load for
105 'example.html.js'.
106
107 The [requirejs optimizer](http://requirejs.org/docs/optimization.html) will
108 generate these '.js' versions of the text resources if you set this in the
109 build profile:
110
111 optimizeAllPluginResources: true
112
113 In some cases, you may want the text plugin to not try the .js resource, maybe
114 because you have configured CORS on the other server, and you know that only
115 browsers that support CORS will be used. In that case you can use the
116 [module config](http://requirejs.org/docs/api.html#config-moduleconfig)
117 (requires RequireJS 2+) to override some of the basic logic the plugin uses to
118 determine if the .js file should be requested:
119
120 ```javascript
121 requirejs.config({
122 config: {
123 text: {
124 useXhr: function (url, protocol, hostname, port) {
125 //Override function for determining if XHR should be used.
126 //url: the URL being requested
127 //protocol: protocol of page text.js is running on
128 //hostname: hostname of page text.js is running on
129 //port: port of page text.js is running on
130 //Use protocol, hostname, and port to compare against the url
131 //being requested.
132 //Return true or false. true means "use xhr", false means
133 //"fetch the .js version of this resource".
134 }
135 }
136 }
137 });
138 ```
139
140 ### Custom XHR hooks
141
142 There may be cases where you might want to provide the XHR object to use
143 in the request, or you may just want to add some custom headers to the
144 XHR object used to make the request. You can use the following hooks:
145
146 ```javascript
147 requirejs.config({
148 config: {
149 text: {
150 onXhr: function (xhr, url) {
151 //Called after the XHR has been created and after the
152 //xhr.open() call, but before the xhr.send() call.
153 //Useful time to set headers.
154 //xhr: the xhr object
155 //url: the url that is being used with the xhr object.
156 },
157 createXhr: function () {
158 //Overrides the creation of the XHR object. Return an XHR
159 //object from this function.
160 //Available in text.js 2.0.1 or later.
161 },
162 onXhrComplete: function (xhr, url) {
163 //Called whenever an XHR has completed its work. Useful
164 //if browser-specific xhr cleanup needs to be done.
165 }
166 }
167 }
168 });
169 ```
170
171 ### Forcing the environment implementation
172
173 The text plugin tries to detect what environment it is available for loading
174 text resources, Node, XMLHttpRequest (XHR) or Rhino, but sometimes the
175 Node or Rhino environment may have loaded a library that introduces an XHR
176 implementation. You can force the environment implementation to use by passing
177 an "env" module config to the plugin:
178
179 ```javascript
180 requirejs.config({
181 config: {
182 text: {
183 //Valid values are 'node', 'xhr', or 'rhino'
184 env: 'rhino'
185 }
186 }
187 });
188 ```
189
190 ## License
191
192 MIT
193
194 ## Code of Conduct
195
196 [jQuery Foundation Code of Conduct](https://jquery.org/conduct/).
197
198 ## Where are the tests?
199
200 They are in the [requirejs](https://github.com/requirejs/requirejs) and
201 [r.js](https://github.com/requirejs/r.js) repos.
202
203 ## History
204
205 This plugin was in the [requirejs repo](https://github.com/requirejs/requirejs)
206 up until the requirejs 2.0 release.