Skip to content

Instantly share code, notes, and snippets.

@think49
Created November 4, 2011 15:38
Show Gist options
  • Save think49/1339609 to your computer and use it in GitHub Desktop.
Save think49/1339609 to your computer and use it in GitHub Desktop.
is-textcontent-of-script.js : script要素の内容が正しいかを検査する(HTML5準拠)
/**
* is-textcontent-of-script.js
*
* @version 1.0.1
* @author think49
* @see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#restrictions-for-contents-of-script-elements">4.3.1.2 Restrictions for contents of script elements - HTML5 Standard</a>
*/
'use strict';
function isTextcontentOfScript (textContent) {
var script, escape, data1, not_data1, data2, not_data2, data3, not_data3, script_start, script_end, lt, slash, s, c, r, i, p, t, tag_end;
lt = '\x3C';
slash = '/';
s = '[Ss]';
c = '[Cc]';
r = '[Rr]';
i = '[Ii]';
p = '[Pp]';
t = '[Tt]';
tag_end = '[\x09\x0A\x0C\x20\x2F\x3E]';
script_start = lt + s + c + r + i + p + t + tag_end;
script_end = lt + slash + s + c + r + i + p + t + tag_end;
not_data1 = '\x3C--';
data1 = '(?:(?!' + not_data1 + ')[\\s\\S])*';
not_data2 = '(?:' + script_start + '|--\x3E)';
data2 = '(?:(?!' + not_data2 + ')[\\s\\S])*';
not_data3 = '(?:' + script_end + '|--\x3E)';
data3 = '(?:(?!' + not_data3 + ')[\\s\\S])*';
escape = '\x3C--' + data2 + '(?:' + script_start + data3 + script_end + data2 + ')*';
script = data1 + '(?:' + escape + '(?:' + script_start + data3 + ')?--\x3E' + data1 + ')*(?:' + escape + ')?';
return new RegExp('^' + script + '$').test(textContent);
}
@think49
Copy link
Author

think49 commented Nov 4, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment