繁体   English   中英

window.prompt() 破坏文本

[英]window.prompt() corrupting text

我希望我的用户输入 JSON。 所以我执行:

const inputJSON = window.prompt("Enter JSON:");

然后我允许用户像这样编辑 JSON:

const newJson = window.prompt("Enter new JSON:", inputJSON ); // assume the user doesn't edit the JSON

但我注意到,当我使用上述默认值调用提示 function 时,文本已损坏:

console.assert(inputJSON  === newJson); // fails!

这是我使用的 JSON:

{"$schema":"http://adaptivecards.io/schemas/adaptive-card.json","type":"AdaptiveCard","version":"1.0","speak":"<s>Flight KL0605 to San Fransisco has been delayed.</s><s>It will not leave until 10:10 AM.</s>","body":[{"type":"ColumnSet","columns":[{"type":"Column","width":"auto","items":[{"type":"Image","size":"small","url":"https://adaptivecards.io/content/airplane.png"}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"Flight Status","horizontalAlignment":"right","isSubtle":true},{"type":"TextBlock","text":"DELAYED","horizontalAlignment":"right","spacing":"none","size":"large","color":"attention"}]}]},{"type":"ColumnSet","separator":true,"spacing":"medium","columns":[{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"Passengers","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"Sarah Hum","spacing":"small"},{"type":"TextBlock","text":"Jeremy Goldberg","spacing":"small"},{"type":"TextBlock","text":"Evan Litvak","spacing":"small"}]},{"type":"Column","width":"auto","items":[{"type":"TextBlock","text":"Seat","horizontalAlignment":"right","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"14A","horizontalAlignment":"right","spacing":"small"},{"type":"TextBlock","text":"14B","horizontalAlignment":"right","spacing":"small"},{"type":"TextBlock","text":"14C","horizontalAlignment":"right","spacing":"small"}]}]},{"type":"ColumnSet","spacing":"medium","separator":true,"columns":[{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Flight","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"KL0605","spacing":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Departs","isSubtle":true,"horizontalAlignment":"center","weight":"bolder"},{"type":"TextBlock","text":"10:10 AM","color":"attention","weight":"bolder","horizontalAlignment":"center","spacing":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Arrives","isSubtle":true,"horizontalAlignment":"right","weight":"bolder"},{"type":"TextBlock","text":"12:00 AM","color":"attention","horizontalAlignment":"right","weight":"bolder","spacing":"small"}]}]},{"type":"ColumnSet","spacing":"medium","separator":true,"columns":[{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Amsterdam","isSubtle":true},{"type":"TextBlock","text":"AMS","size":"extraLarge","color":"accent","spacing":"none"}]},{"type":"Column","width":"auto","items":[{"type":"TextBlock","text":" "},{"type":"Image","url":"https://adaptivecards.io/content/airplane.png","size":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"San Francisco","isSubtle":true,"horizontalAlignment":"right"},{"type":"TextBlock","text":"SFO","horizontalAlignment":"right","size":"extraLarge","color":"accent","spacing":"none"}]}]}]}

TL;DR:这应该是真的: longJson == prompt("Just press enter", longJSON)

编辑:

我在浏览器中对此进行了测试:

const json = `{"$schema":"http://adaptivecards.io/schemas/adaptive-card.json","type":"AdaptiveCard","version":"1.0","speak":"<s>Flight KL0605 to San Fransisco has been delayed.</s><s>It will not leave until 10:10 AM.</s>","body":[{"type":"ColumnSet","columns":[{"type":"Column","width":"auto","items":[{"type":"Image","size":"small","url":"https://adaptivecards.io/content/airplane.png"}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"Flight Status","horizontalAlignment":"right","isSubtle":true},{"type":"TextBlock","text":"DELAYED","horizontalAlignment":"right","spacing":"none","size":"large","color":"attention"}]}]},{"type":"ColumnSet","separator":true,"spacing":"medium","columns":[{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"Passengers","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"Sarah Hum","spacing":"small"},{"type":"TextBlock","text":"Jeremy Goldberg","spacing":"small"},{"type":"TextBlock","text":"Evan Litvak","spacing":"small"}]},{"type":"Column","width":"auto","items":[{"type":"TextBlock","text":"Seat","horizontalAlignment":"right","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"14A","horizontalAlignment":"right","spacing":"small"},{"type":"TextBlock","text":"14B","horizontalAlignment":"right","spacing":"small"},{"type":"TextBlock","text":"14C","horizontalAlignment":"right","spacing":"small"}]}]},{"type":"ColumnSet","spacing":"medium","separator":true,"columns":[{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Flight","isSubtle":true,"weight":"bolder"},{"type":"TextBlock","text":"KL0605","spacing":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Departs","isSubtle":true,"horizontalAlignment":"center","weight":"bolder"},{"type":"TextBlock","text":"10:10 AM","color":"attention","weight":"bolder","horizontalAlignment":"center","spacing":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Arrives","isSubtle":true,"horizontalAlignment":"right","weight":"bolder"},{"type":"TextBlock","text":"12:00 AM","color":"attention","horizontalAlignment":"right","weight":"bolder","spacing":"small"}]}]},{"type":"ColumnSet","spacing":"medium","separator":true,"columns":[{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"Amsterdam","isSubtle":true},{"type":"TextBlock","text":"AMS","size":"extraLarge","color":"accent","spacing":"none"}]},{"type":"Column","width":"auto","items":[{"type":"TextBlock","text":" "},{"type":"Image","url":"https://adaptivecards.io/content/airplane.png","size":"small"}]},{"type":"Column","width":1,"items":[{"type":"TextBlock","text":"San Francisco","isSubtle":true,"horizontalAlignment":"right"},{"type":"TextBlock","text":"SFO","horizontalAlignment":"right","size":"extraLarge","color":"accent","spacing":"none"}]}]}]}`;
console.log(json == window.prompt("Don't edit. Just press enter.", json);) // false
console.log(json.length); // 2818
console.log(window.prompt("Don't edit. Just press enter.", json).length) // 2000

您无法轻松比较 JavaScript 中的两个 Object。

{} === {} // return false

因此,您可以将对象转换为字符串,然后进行比较:

console.assert(JSON.stringify(oldJson) === JSON.stringify(newJson));

好的,我发现了这个问题。 在 Chrome 中,提示仅限于 2000 个字符。 我用这个测试了它:

const expectedLength = 4000;
const actualLength = prompt("Don't edit. Just press enter.", Array(expectedLength).join("0")).length
console.assert(expectedLength == actualLength);

JSON 中存在问题。 JSON 被覆盖,因此,值不匹配。 这是输入 JSON 和 output JSON 的区别。 差异检查器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM