Forum Moderators: open
var cssArr = [
'display',
'background',
'position',
'white-space',
'width',
'height',
'min-width',
'min-height',
'padding',
'margin',
'position',
'top',
'bottom',
'left',
'right'
] var a = $('#comment').html();
var pattern;
for (var i = 0; i < cssArr.length; i++) {
pattern = RegExp("style=(\"|')(\w+;)*\s*" + cssArr[i] + "[^:>\1]*:\w+?[;\1]", 'gi');
while (pattern.text(a)
a = a.replace(pattern, 'style=$1$2');
}
// remove any leftover empty style="" tags
a = a.replace(/style=("|')\s*\1/gi, ''); Honestly, I'd think twice about permitting style=blahblah at all. Delete it across the board, and that will just leave the basic HTML tags like <i> and <b> and <a href = "http://example.spam/">
With all these jQuerry, Ajax & JS scripts that need to download for each new visitor, how fast is you site?
// I'm pretty sure this should be "new RegExp", not just "RegExp"
pattern = new RegExp("style=(\"|')(\w+;)*\s*" + cssArr[i] + "[^:>\1]*:\w+?[;\1]", 'gi');
// two typos here: I said "text" instead of "test", and left off the last closing )
while (pattern.test(a))
I love regular expressions, but still avoid them whenever I reasonably can.
So, what if you (1) iterate over all children of #comment, (2) check if they have the style attribute, (3) maybe check if they have CSS attributes from cssArr, (4) then instead of using replace() you simply reset the value using css(), effectively removing the attribute from the element's (in-line) styling.
Do you permit anything in the form foo-bar? I notice for example that you list both min-width and min-height, but what about max-eachofthose?
<div style="width: 600px">
<div style="width: 100%; height: auto; border: 1px solid #000000; background: #FEFEFE">
This is what the first person submitted
</div>
<div style="width: 100%; height: auto; border: 1px solid #000000; background: #FEFEFE">
This is what the second person submitted<br><div style="font-weight: bold">Kewl.</div>
</div>
</div> <div style="width: 600px">
<div style="width: 100%; height: auto; border: 1px solid #000000; background: #FEFEFE">
<div style="width: 2000px; margin-bottom: 1000px">This is what the first person submitted</div>
</div>
<div style="width: 100%; height: auto; border: 1px solid #000000; background: #FEFEFE">
<div style="position: absolute; top: -50px; right: 1000px">This is what the second person submitted<br><div style="height: 5000px; font-weight: bold">Kewl.</div>
</div>
</div> align-content
align-items
align-self
all
animation
animation-delay
animation-direction
animation-duration
animation-fill-mode
animation-iteration-count
animation-name
animation-play-state
animation-timing-function
backface-visibility
background
background-attachment
background-blend-mode
background-clip
background-color
background-image
background-origin
background-position
background-repeat
background-size
border
border-bottom
border-bottom-color
border-bottom-left-radius
border-bottom-right-radius
border-bottom-style
border-bottom-width
border-collapse
border-color
border-image
border-image-outset
border-image-repeat
border-image-slice
border-image-source
border-image-width
border-left
border-left-color
border-left-style
border-left-width
border-radius
border-right
border-right-color
border-right-style
border-right-width
border-spacing
border-style
border-top
border-top-color
border-top-left-radius
border-top-right-radius
border-top-style
border-top-width
border-width
bottom
box-decoration-break
box-shadow
box-sizing
caption-side
caret-color
@charset
clear
clip
color
column-count
column-fill
column-gap
column-rule
column-rule-color
column-rule-style
column-rule-width
column-span
column-width
columns
content
counter-increment
counter-reset
cursor
direction
display
empty-cells
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
float
font
@font-face
font-family
font-kerning
font-size
font-size-adjust
font-stretch
font-style
font-variant
font-weight
grid
grid-area
grid-auto-columns
grid-auto-flow
grid-auto-rows
grid-column
grid-column-end
grid-column-gap
grid-column-start
grid-gap
grid-row
grid-row-end
grid-row-gap
grid-row-start
grid-template
grid-template-areas
grid-template-columns
grid-template-rows
hanging-punctuation
height
hyphens
@import
isolation
justify-content
@keyframes
left
letter-spacing
line-height
list-style
list-style-image
list-style-position
list-style-type
margin
margin-bottom
margin-left
margin-right
margin-top
max-height
max-width
@media
min-height
min-width
mix-blend-mode
object-fit
object-position
opacity
order
outline
outline-color
outline-offset
outline-style
outline-width
overflow
overflow-x
overflow-y
padding
padding-bottom
padding-left
padding-right
padding-top
page-break-after
page-break-before
page-break-inside
perspective
perspective-origin
pointer-events
position
quotes
resize
right
tab-size
table-layout
text-align
text-align-last
text-decoration
text-decoration-color
text-decoration-line
text-decoration-style
text-indent
text-justify
text-overflow
text-shadow
text-transform
top
transform
transform-origin
transform-style
transition
transition-delay
transition-duration
transition-property
transition-timing-function
unicode-bidi
user-select
vertical-align
visibility
white-space
width
word-break
word-spacing
word-wrap
z-index
but it doesn't include -moz and -o formatsI shouldn't think -o- is much of an issue: doesn't that go back to when Opera had its own rendering engine, whereas now it's all webkit? And -ms- is even more fun, since it requires MSIE to admit that we do this particular thing differently from all other browsers, and I don't really see that happening. In any case you would be perfectly justified in wiping any and all vendor extensions, i.e. stuff beginning in a hyphen, unless you have a lot of users on elderly browsers who don't recognize things like “column-count” but require “-moz-column-count” instead--and then you might need to add extensions so it displays as intended on everyone's browser, not just the person who posted it.
Which brings us to ... just how complicated are the things your users are posting?
<div style="position: absolute; left: 357.02px; top: 151.43px; font-size: 30px; font-family: serif; transform: scaleX(1.06183);">POSITION VACANCY</div><div style="position: absolute; left: 150px; top: 232.508px; font-size: 26.6px; font-family: serif; transform: scaleX(1.03406);">Tax Administrator</div> If you've got people trying to shove 2000-px-wide elements into something that's only 600px wide, make sure your overall CSS has the appropriate “overflow” setting.
<div><span style="position:absolute;">example</span></div>...then that position attribute won't be removed. For some reason, I couldn't get it to work with a single find().each() or filter().each() loop, which is what I meant to say where the comment reads "Find all children of this element and repeat the process (couldn't get it to work)".