成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

我在開發(fā)"小程序"中做的一些"轉(zhuǎn)換"的工作

pepperwang / 1623人閱讀

摘要:介紹轉(zhuǎn)換意思是將小程序不支持的東西轉(zhuǎn)換成它支持的東西。我在開發(fā)的小程序的過程中遇到了兩種需要做轉(zhuǎn)換的場景轉(zhuǎn)換成轉(zhuǎn)換成我將在下文詳細(xì)介紹我是怎么處理這兩種情況的??偨Y(jié)以上,就是我在開發(fā)小程序中對與做的一些轉(zhuǎn)換的經(jīng)歷。

介紹

“轉(zhuǎn)換” 意思是將"小程序"不支持的東西轉(zhuǎn)換成它支持的東西。我在開發(fā)的小程序的過程中遇到了兩種需要做“轉(zhuǎn)換”的場景:

html 轉(zhuǎn)換成 wxml

svg 轉(zhuǎn)換成 canvas

我將在下文詳細(xì)介紹我是怎么處理這兩種情況的。

html 轉(zhuǎn)換成 wxml

我們的產(chǎn)品在某些場景下,后端接口會直接傳 html 字符串給前端。在 ReactJs 中,我們可以用 dangerouslySetInnerHTML 直接渲染 html 字符串(不一定安全),而 ”小程序“不支持 html ,因此必須對 html 進行處理。解決這個問題的步驟主要是:1. 將 html 轉(zhuǎn)換成 json ( 樹結(jié)構(gòu)) ;2. 將 json 轉(zhuǎn)換成 wxml 。我在對問題做了調(diào)研后發(fā)現(xiàn),現(xiàn)有一個庫 wxParse 滿足該轉(zhuǎn)換的目的,但是在我看來,這個庫做的事情太多,需要依賴文件過多,不滿足只需要簡單處理的需要,所以我決定自己寫。

html 轉(zhuǎn)換成 json

在參考了 html2jsonhimalaya 兩個庫的處理思路的基礎(chǔ)上,我寫了一個簡單的解析庫 htmlParser htmlParser 處理 html字符串分兩步:

lexer: 生成標(biāo)記(token

function lex(html) {
  let string = html
  let tokens = []

  while (string) {
    // 先處理以 "

parser: 根據(jù)標(biāo)記生成樹
上面的 lexerhtml 字符串分隔成了一個一個 token,然后,我們通過遍歷所有的標(biāo)識來構(gòu)建樹

function parse(tokens) {
  let root = {
    tag: "root",
    children: []
  }
  let tagArray = [root]
  tagArray.last = () => tagArray[tagArray.length - 1]

  for (var i = 0; i < tokens.length; i++) {
    const token = tokens[i]
    if (token.type === "tag-start") {
      // 構(gòu)建節(jié)點
      const node = {
        type: "Element",
        tagName: token.tag,
        attributes: Object.assign({}, {
          class: token.tag
        }, token.attributes),
        children: []
      }
      tagArray.push(node)
      continue
    }
    if (token.type === "tag-end") {
      let parent = tagArray[tagArray.length - 2]
      let node = tagArray.pop()
      // 將該節(jié)點加入父節(jié)點中
      parent.children.push(node) 
      continue
    }
    if (token.type === "text") {
      // 往該節(jié)點中加入子元素
      tagArray.last().children.push({
        type: "text",
        content: replaceMark(token.text)
      })
      continue
    }
    if (token.type === "tag-empty") { 
     // 往該節(jié)點中加入子元素
      tagArray.last().children.push({
        type: "Element",
        tagName: token.tag,
        attributes: Object.assign({}, {
          class: token.tag
        }, token.attributes),
      })
      continue
    }
  }
  return root
}

整個程序的運行結(jié)果舉例:

var html = "
" htmlParser(html) # 轉(zhuǎn)換結(jié)果 { "tag": "root", "children": [{ "type": "Element", "tagName": "div", "attributes": { "style": "height:10rpx;width: 20rpx;" }, "children": [ { "type": "Element", "tagName": "img", "attributes": { src: "http://xxx.jpg", class: "image" } }] }] }

以上,我們完成了 html字符串的轉(zhuǎn)換,完整代碼請戳 htmlParser

json 轉(zhuǎn)換成 wxml

在熟悉了“小程序”框架的基礎(chǔ)上,發(fā)現(xiàn)需要借助模板 template ,將 json 數(shù)據(jù)填充進 template,并根據(jù)元素類型渲染相應(yīng)的 wxml 組件以達(dá)到轉(zhuǎn)換目的。比如:

# 定義一個名稱為 html-image 的模板

/* 使用模板
   其中 json 的結(jié)構(gòu)為: {
      "type": "Element",
      "tagName": "img",
      "attributes": {
          src: "http://xxx.jpg",
          class: "image"
      }
    }
 */

這樣,我們就能轉(zhuǎn)化成功了。

而因為模板沒有引用自身的能力,只能使用笨辦法使用多個同樣內(nèi)容,但是模板名稱不一樣的模板來解決嵌套的層級關(guān)系,而嵌套的層級取決于使用的模板個數(shù)