浏览代码

automatically strip whitespace when parsing html

painor 4 年之前
父节点
当前提交
fe1c2b75b3
共有 5 个文件被更改,包括 52 次插入9 次删除
  1. 45 4
      gramjs/Helpers.ts
  2. 1 1
      gramjs/Version.ts
  3. 3 1
      gramjs/extensions/html.ts
  4. 2 2
      package-lock.json
  5. 1 1
      package.json

+ 45 - 4
gramjs/Helpers.ts

@@ -1,6 +1,8 @@
 import {isNode} from 'browser-or-node';
 import bigInt from "big-integer";
 import type {EntityLike} from "./define";
+import type {Api} from "./tl";
+
 
 export const IS_NODE = isNode;
 const crypto = require(isNode ? 'crypto' : './crypto/crypto');
@@ -38,7 +40,7 @@ export function escapeRegex(string: string) {
 /**
  * Helper to find if a given object is an array (or similar)
  */
-export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number' && typeof x !== 'function' && typeof x!=='string');
+export const isArrayLike = (<T>(x: any): x is ArrayLike<T> => x && typeof x.length === 'number' && typeof x !== 'function' && typeof x !== 'string');
 
 /*
 export function addSurrogate(text: string) {
@@ -198,6 +200,45 @@ async function calcKey(sharedKey, msgKey, client) {
 }
 
  */
+export function stripText(text: string, entities: Api.TypeMessageEntity[]) {
+    if (!entities || !entities.length) {
+        return text.trim();
+    }
+    while (text && text[text.length - 1].trim() === "") {
+        const e = entities[entities.length - 1];
+        if ((e.offset + e.length) == text.length) {
+            if (e.length == 1) {
+                entities.pop();
+                if (!entities.length) {
+                    return text.trim();
+                }
+            } else {
+                e.length -= 1;
+            }
+        }
+        text = text.slice(0, -1);
+    }
+    while (text && text[0].trim() === "") {
+        for (let i = 0; i < entities.length; i++) {
+            const e = entities[i];
+            if (e.offset != 0) {
+                e.offset--;
+                continue;
+            }
+            if (e.length == 1) {
+                entities.shift();
+                if (!entities.length) {
+                    return text.trimLeft();
+                }
+            } else {
+                e.length -= 1;
+            }
+        }
+        text = text.slice(1);
+    }
+    return text;
+}
+
 
 /**
  * Generates the key data corresponding to the given nonces
@@ -299,12 +340,12 @@ export function getMinBigInt(arrayOfBigInts: bigInt.BigInteger[]) {
     if (arrayOfBigInts.length == 0) {
         return bigInt.zero;
     }
-    if (arrayOfBigInts.length==1){
+    if (arrayOfBigInts.length == 1) {
         return arrayOfBigInts[0];
     }
     let smallest = arrayOfBigInts[0];
-    for (let i=1;i<arrayOfBigInts.length;i++){
-        if (arrayOfBigInts[i]<smallest){
+    for (let i = 1; i < arrayOfBigInts.length; i++) {
+        if (arrayOfBigInts[i] < smallest) {
             smallest = arrayOfBigInts[i];
         }
     }

+ 1 - 1
gramjs/Version.ts

@@ -1 +1 @@
-export const version = "1.6.8";
+export const version = "1.6.10";

+ 3 - 1
gramjs/extensions/html.ts

@@ -1,6 +1,7 @@
 import {Parser} from "htmlparser2";
 import {Handler} from "htmlparser2/lib/Parser";
 import {Api} from "../tl";
+import {helpers} from "../index";
 
 class HTMLToTelegramParser implements Handler {
     text: string;
@@ -151,7 +152,8 @@ export class HTMLParser {
         const parser = new Parser(handler);
         parser.write(html);
         parser.end();
-        return [handler.text, handler.entities];
+        const text = helpers.stripText(handler.text,handler.entities);
+        return [text, handler.entities];
 
     }
 

+ 2 - 2
package-lock.json

@@ -1,12 +1,12 @@
 {
   "name": "telegram",
-  "version": "1.6.9",
+  "version": "1.6.10",
   "lockfileVersion": 2,
   "requires": true,
   "packages": {
     "": {
       "name": "telegram",
-      "version": "1.6.9",
+      "version": "1.6.10",
       "license": "MIT",
       "dependencies": {
         "@cryptography/aes": "^0.1.1",

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "telegram",
-  "version": "1.6.9",
+  "version": "1.6.10",
   "description": "NodeJS MTProto API Telegram client library,",
   "main": "index.js",
   "types": "index.d.ts",