Category - JavaScript

2021-12-30 15:43:37    0    0    0

html转换成AST

想要的效果

  1. 输入: let str ="<div><span>tests</span></div>"
  2. 输出 : {
  3. tag: 'div',
  4. children: [
  5. {
  6. tag: 'span'
  7. },
  8. ],
  9. }

实现

  1. // 设置每个节点标签属性
  2. // let attrRE = /\s([^'"/\s><]+?)[\s/>]|([^\s=]+)=\s?(".*?"|'.*?')/g;
  3. function parseTag(tag) {
  4. let res = {
  5. type: "tag",
  6. name: "",
  7. voidElement: false,
  8. attrs: {},
  9. children: [],
  10. };
  11. let tagMatch = tag.match(/<\/?([^\s]+?)[/\s>]/);
  12. if (tagMatch) {
  13. // 标签名称为正则匹配的第2项
  14. res.name = tagMatch[1];
  15. if (tag.charAt(tag.length - 2) === "/") {
  16. // 判断tag字符串倒数第二项是不是 / 设置为空标签。 例子:<img/>
  17. res.voidElement = true;
  18. }
  19. }
  20. // 匹配所有的标签正则
  21. let classList = tag.match(/\s([^'"/\s><]+?)\s*?=\s*?(".*?"|'.*?')/g);
  22. if (classList && classList.length) {
  23. for (let i = 0; i < classList.length; i++) {
  24. // 去空格再以= 分隔字符串 得到['属性名称','属性值']
2021-07-27 17:10:55    2    0    0
  1. var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. function promiseForEach(arr, cb) {
  3. let realResult = []
  4. let result = Promise.resolve()
  5. arr.forEach((a, index) => {
  6. result = result.then(() => {
  7. if (typeof cb === "function") {
  8. return cb(a).then((res) => {
  9. realResult.push(res)
  10. })
  11. }
  12. })
  13. })
  14. return result.then(() => {
  15. return realResult
  16. })
  17. }
  18. promiseForEach(arr, (ele) => {
  19. return new Promise((resolve, reject) => {
  20. setTimeout(() => {
  21. console.log(ele)
  22. return resolve(ele)
  23. }, Math.random() * 1000)
  24. })
  25. }).then((data) => {
  26. console.log("成功")
  27. console.log(data)
  28. }).catch((err) => {
  29. console.log("失败")
  30. console.log(err)
  31. })
2021-06-09 10:10:33    2    0    0
  1. /**
  2. * 复制文本
  3. * @param text
  4. */
  5. export function copyText(text) {
  6. const input = document.createElement("input")
  7. input.value = text
  8. document.body.appendChild(input)
  9. input.select()
  10. document.execCommand("Copy")
  11. window.setTimeout(function () {
  12. document.body.removeChild(input)
  13. }, 0)
  14. }
  15. /**
  16. * esc监听全屏
  17. */
  18. export const listenfullscreen = (callback) => {
  19. function listen() {
  20. callback()
  21. }
  22. document.addEventListener("fullscreenchange", function () {
  23. listen()
  24. })
  25. document.addEventListener("mozfullscreenchange", function () {
  26. listen()
  27. })
  28. document.addEventListener("webkitfullscreenchange", function () {
  29. listen()
  30. })
  31. document.addEventListener("msfullscreenchange", function () {
  32. listen()
  33. })
  34. }
  35. /**
  36. * 浏览器判断是否全屏
  37. */
  38. export const fullscreenEnable = () => {
  39. return document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
  40. }
  41. /**
  42. * 浏览器全屏
  43. */
  44. export const re
2021-05-21 18:16:17    6    0    0
  1. Number.prototype.toFixed = function (n) {
  2. if (n != undefined && (isNaN(n) || Number(n) > 17 || Number(n) < 0)) {
  3. throw new Error("输入正确的精度范围");
  4. }
  5. // 拆分小数点整数和小数
  6. var num = this;
  7. var f = '';
  8. if (Number(num) < 0) {
  9. num = -Number(num);
  10. f = '-';
  11. }
  12. var numList = num.toString().split(".");
  13. // 整数
  14. var iN = numList[0];
  15. // 小数
  16. var dN = numList[1];
  17. n = parseInt(n);
  18. if (isNaN(n) || Number(n) === 0) {
  19. // 0或者不填的时候,按0来处理
  20. if(dN === undefined){
  21. return num + '';
  22. }
  23. var idN = Number(dN.toString().substr(0, 1));
  24. if (idN >= 5) {
  25. if (Number(iN) < 0) {
  26. iN = Number(iN) - 1
  27. } else {
  28. iN = Number(iN) + 1
  29. }
  30. }
  31. return iN + '';
  32. } else {
  33. var dNL = dN === undefined ? 0 : dN.length;
  34. if (dNL < n) {
  35. // 如果小数位不够的话,那就补全
  36. var oldN = num.toString().ind
2019-11-14 15:46:32    4    0    0
参考: http://caibaojian.com/hide-scrollbar.html
2019-11-14 15:46:32    4    0    0
- Array.apply(null, {length: 20}) - 参考: https://segmentfault.com/a/1190000011435501 - `map函数并不会遍历数组中没有初始化或者被delete的元素(有相同限制还有forEach, reduce方法)`
2019-11-14 15:46:32    5    0    0
> 参考: https://blog.csdn.net/handsomexiaominge/article/details/80977402
2019-11-14 15:46:32    6    0    0
> 参考: https://blog.csdn.net/weixin_41830601/article/details/79908597
2019-11-14 15:46:32    2    0    0
1. viewerjs 预览: https://fengyuanchen.github.io/viewerjs/ 地址: https://github.com/fengyuanchen/viewerjs
2019-11-14 15:46:32    15    0    0
> 参考: https://www.cnblogs.com/Joanna-Yan/p/9896180.html
1/2