Hướng dẫn escape html npm - thoát html npm

Escape-html

Hướng dẫn escape html npm - thoát html npm

Chuỗi thoát để sử dụng trong HTML

Mô -đun này xuất một hàm duy nhất, escapeHtml, được sử dụng để thoát một chuỗi nội dung sao cho nó có thể được nội suy trong nội dung HTML.

Cài đặt

Đây là mô -đun Node.js có sẵn thông qua sổ đăng ký NPM. Cài đặt được thực hiện bằng lệnh npm install:

$ npm install escape-html

API

EscapeHtml (chuỗi)

Thoát các ký tự đặc biệt trong chuỗi văn bản đã cho, sao cho nó có thể được nội suy trong nội dung HTML.

Hàm này sẽ thoát khỏi các ký tự sau: ", ', &,

var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
0 và
var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
1.

Lưu ý rằng giá trị thoát chỉ phù hợp để được nội suy vào HTML vì nội dung văn bản của các phần tử trong đó thẻ không có cơ chế thoát khác nhau (ví dụ, nó không thể được đặt bên trong

var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
2 hoặc
var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
3 vì các cơ quan nội dung đó không phải là HTML, nhưng CSS và JavaScript, tương ứng; chúng được gọi là "các phần tử văn bản thô" trong tiêu chuẩn HTML).
that the escaped value is only suitable for being interpolated into HTML as the text content of elements in which the tag does not have different escaping mechanisms (it cannot be placed inside
var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
2 or
var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'
3, for example, as those content bodies are not HTML, but CSS and JavaScript, respectively; these are known as "raw text elements" in the HTML standard).

Lưu ý Khi sử dụng giá trị thoát trong thẻ, nó chỉ phù hợp là giá trị của một thuộc tính, trong đó giá trị được trích dẫn bằng ký tự trích dẫn kép (") hoặc một ký tự trích dẫn duy nhất ('). when using the escaped value within a tag, it is only suitable as the value of an attribute, where the value is quoted with either a double quote character (") or a single quote character (').

Thí dụ

Hàm escapeHtml được thiết kế để chấp nhận đầu vào chuỗi của văn bản và trả về giá trị thoát để nội suy thành HTML.

var escapeHtml = require('escape-html')

// example values
var desc = 'I <b>think</b> this is good.'
var fullName = 'John "Johnny" Smith'

// example passing in text into a html attribute
console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">')
// -> '<input name="full_name" value="John &quot;Johnny&quot; Smith">'

// example passing in text in html body
console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>')
// -> '<textarea name="desc">I &lt;b&gt;think&lt;/b&gt; this is good.</textarea>'

Điểm chuẩn

$ npm run-script bench

>  bench nodejs-escape-html
> node benchmark/index.js


  
  
  
  
  
  
  modules@11
  

  1 test completed.
  2 tests completed.
  3 tests completed.

  no special characters    x 19,435,271 ops/sec ±0.85% (187 runs sampled)
  single special character x  6,132,421 ops/sec ±0.67% (194 runs sampled)
  many special characters  x  3,175,826 ops/sec ±0.65% (193 runs sampled)

Giấy phép

MIT

1.0.3 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản 7 năm trướcPublic • Published 7 years ago

  • Readme
  • Khám phá BetaBETA
  • 0 phụ thuộc
  • 1.832 người phụ thuộc
  • 5 phiên bản

escape-html

Chuỗi thoát để sử dụng trong HTML

Thí dụ

var escape = require('escape-html');

var html = escape('foo & bar');

Điểm chuẩn

$ npm run-script bench

>  bench nodejs-escape-html
> node benchmark/index.js


  
  
  
  
  
  
  modules@11
  

  1 test completed.
  2 tests completed.
  3 tests completed.

  no special characters    x 19,435,271 ops/sec ±0.85% (187 runs sampled)
  single special character x  6,132,421 ops/sec ±0.67% (194 runs sampled)
  many special characters  x  3,175,826 ops/sec ±0.65% (193 runs sampled)

Giấy phép

MIT