-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
report: truncate long attribute values in HTML snippets #10984
Conversation
@@ -123,6 +123,14 @@ function getOuterHTMLSnippet(element, ignoreAttrs = []) { | |||
ignoreAttrs.forEach(attribute =>{ | |||
clone.removeAttribute(attribute); | |||
}); | |||
for (const attributeName of clone.getAttributeNames()) { | |||
let attributeValue = clone.getAttribute(attributeName); | |||
if (attributeValue.length > 100) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd pull this 100 up to a const towards the top of the method
for (const attributeName of clone.getAttributeNames()) { | ||
let attributeValue = clone.getAttribute(attributeName); | ||
if (attributeValue.length > 100) { | ||
attributeValue = attributeValue.slice(0, 97) + '...'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
protip: option-;
on mac is …
which is a single character ellipsis.
before/after screenshots? :) |
oh, I see, not limiting which attributes, just making sure none of them are ridiculously long. I guess that's also not that hard to imagine :) |
This is technically a breaking change. Any way we could truncate in just the report but leave |
I could look into making this change in the snippet renderer instead. I think eventually we'd want to do it in page-functions for the next major release. |
Spoke offline: nah not worth it.
…On Mon, Jun 22, 2020, 1:09 PM Michael Blasingame ***@***.***> wrote:
This is technically a breaking change.
Any way we could truncate in just the report but leave snippet untouched?
I could look into making this change in the snippet renderer instead. I
think eventually we'd want to do it in page-functions for the next major
release.
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#10984 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA7CAMWMH2SLDLQGVJW4MRTRX6277ANCNFSM4OBADRPQ>
.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. pending approval on seeing a before/after (cnn.com has nice big ones).
Maybe we should also have a |
That would be nice, but then we run the risk of more important identifiers being cut off because they fall behind the other super long data attributes. I think the reason we opted for truncating per attribute was so that wouldn't happen. An alternative is that we could omit data-* attributes. They tend to be long and I don't know if they're actually useful in regards to element identification. |
spoke offline:
|
do we need to? since this adds so much more complexity i'd rather defer until we across a real example that necessitates it. |
Can you elaborate? the example in the OP is exactly why this is necessary. Just having a cap on the size of individual attributes does not prevent a lengthy node snippet when there are many attributes. |
So the approach I ended up taking here is to establish an "attribute character budget" of sorts. We simply keep count of the number of characters used for attribute names/values and once we've exceeded that budget we remove the remaining attributes. I'm seeing 50-60% reductions in snippet length for some of the longer snippets (1200+ characters). |
const reOpeningTag = /^[\s\S]*?>/; | ||
const match = clone.outerHTML.match(reOpeningTag); | ||
if (match && match[0] && charCount > SNIPPET_CHAR_LIMIT) { | ||
return match[0].slice(0, match[0].length - 1) + '…>'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add some tests for this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, currently working on it
charCount += attributeName.length + attributeValue.length; | ||
} | ||
} | ||
|
||
const reOpeningTag = /^[\s\S]*?>/; | ||
const match = clone.outerHTML.match(reOpeningTag); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe replace with
const [match] = clone.outerHTML.match(reOpeningTag) || [];
and then drop all the match && match[0]
for just match
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some nits, but marking my approval anyway
Co-authored-by: Connor Clark <[email protected]>
Addresses #10717.
Before:
After: