HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
section.section
h1.title.is-3 テキスト内の数字の差を表示
p 左右のテキストエリアの同じ行にある数字を比較します。
p.help ※差は10進数。
p.help ※カンマ区切りの数字、全角数字は未対応。
section.section
#app
.content.input-field
label.label.has-text-grey.is-size-7 比較するテキスト
.content
.compare
textarea.textarea.input-a(v-model="inputA" :rows='inputARows')
| iPhone11
| 75.7 × 150.9 × 8.3mm
| 6.1インチ
| 194g
textarea.textarea.input-b(v-model="inputB" :rows='inputBRows')
| Pixel4
| 68.8 × 147.1 × 8.2mm
| 5.7インチ
| 162g
.content.has-text-centered
.icon.has-text-grey-light.is-size-4
i.fas.fa-arrow-down
.content.output-field
label.label.has-text-grey.is-size-7 数値の比較結果
.content
.result.compare
.output-a.has-background-white-bis
p(v-for="result in resultA").is-marginless
template(v-for="item in result")
span(v-if="typeof item === 'string'") {{item}}
//- 比較結果
template(v-else)
span.diff-number(:class="{ 'is-positive': item.diff > 0, 'is-negative': item.diff < 0 }")
template(v-if="item.diff >= 0") +
| {{item.diff}}
//- 空白行
template(v-if="result === ''")
.output-b.has-background-white-bis
p(v-for="result in resultB").is-marginless
template(v-for="item in result")
span(v-if="typeof item === 'string'") {{item}}
//- 比較結果
template(v-else)
span.diff-number(:class="{ 'is-positive': item.diff > 0, 'is-negative': item.diff < 0 }")
template(v-if="item.diff >= 0") +
| {{item.diff}}
//- 空白行
template(v-if="result === ''")
html {
background-color: #f5f5f5;
}
// 比較用
.compare {
display: flex;
justify-content: space-between;
> * {
width: calc(50% - 5px);
min-width: auto;
}
}
// 結果
.result {
.output-a,
.output-b {
padding: 0.625rem;
}
}
.diff-number {
margin-left: 0.2rem;
margin-right: 0.2rem;
font-size: 0.8rem;
padding: 0.1rem 0.5rem;
border-radius: 1rem;
// diff = 0 の場合
color: #607d8b;
background-color: #eceff1;
color: #fb8c00;
background-color: #fff3e0;
// プラス
&.is-positive {
color: #388e3c;
background-color: #e8f5e9;
}
// マイナス
&.is-negative {
color: #0288d1;
background-color: #e1f5fe;
}
}
type ComparisonResult = {
diff: number;
}; // 数値の比較結果
const initialData = {
inputA: document.body.querySelector('.input-a').value, // 比較テキストA
inputB: document.body.querySelector('.input-b').value, // 比較テキストB
resultA: [], // 結果A
resultB: [], // 結果B
};
new Vue({
el: '#app',
data () {
return initialData;
},
mounted () {
this.compare();
},
methods: {
compare () {
// 行を取り出す
const linesA = this.inputA.split(/\r\n|\r|\n/); // 入力Aを行ごとに分割
const lineALen = linesA.length;
const linesB = this.inputB.split(/\r\n|\r|\n/); // 入力Bを行ごとに分割
const lineBLen = linesB.length;
const len = Math.max(lineALen, lineBLen); // 比較する(=両方入力されている)行数
const resultA = []; // 結果
const resultB = []; // 結果
// 結果を初期化
for (let i = 0; i < len; i++) {
// 行ごとの数値を比較する
if (linesA[i] && linesB[i]) {
// どちらも入力がある
const separatedA: (string | number | ComparisonResult)[] = separateNumber(linesA[i]); // 数字とそれ以外を切り分ける
const separatedB: (string | number | ComparisonResult)[] = separateNumber(linesB[i]); // 数字とそれ以外を切り分ける
const compareLen = Math.min(separatedA.length, separatedB.length); // 比較件数
// 1行内の区切りごとに比較
for (let j = compareLen - 1; 0 <= j; j--) {
const itemA = Number(separatedA[j]); // 比較アイテム
const itemB = Number(separatedB[j]); // 比較アイテム
// 両方数字なら比較して、結果を後ろに代入
if (!isNaN(itemA) && !isNaN(itemB)) {
// 比較結果を数字の後に入れる
separatedA.splice(j + 1, 0, {
diff: new Decimal(itemA).sub(itemB),
});
separatedB.splice(j + 1, 0, {
diff: new Decimal(itemB).sub(itemA),
});
}
}
// 結果の配列に入れる
resultA.push(separatedA);
resultB.push(separatedB);
}
else {
// どちらかの値がない→比較せず入力されたテキストを入れていく
resultA.push(linesA[i] || '');
resultB.push(linesB[i] || '');
}
}
this.resultA = resultA;
this.resultB = resultB;
},
},
computed: {
inputARows () {
const lines = this.inputA.split('\n').length;
return (lines > 3) ? lines : 3;
},
inputBRows () {
const lines = this.inputB.split('\n').length;
return (lines > 3) ? lines : 3;
},
},
watch: {
inputA () {
this.compare();
},
inputB () {
this.compare();
},
},
});
/**
* 数字を切り出す
* @param str 文字列
*/
function separateNumber (str) {
const delimier = '!\t!'; // 区切り文字
const separated = []; // 分解した結果
let tmp; // 検出用
let isNum; // 検出しているのが数値か
const detect = {
tmp: '', // 検出している文字
mode: '', // 検出している
}; // 検出
// 先頭から見ていくと符号が判別しづらいので、後ろから走査していく
for (let i = str.length - 1; 0 <= i; i--) {
const char = str.charAt(i); // 取り出した文字
if (detect.mode !== '') {
// 数字と文字の区切りを判定
if (detect.mode === 'number') {
// 数値として判定中
// 数字として認識できるか
if(!isNaN(Number(`${char}${detect.tmp}`))) {
// 数値としてマージ
detect.tmp = `${char}${detect.tmp}`;
}
else {
// 数字と文字列の区切り
separated.unshift(detect.tmp); // 切り出す
detect.mode = 'string';
detect.tmp = char;
}
}
else {
// 文字として判定中
if (!char.match(/\d/)) {
// 文字列としてマージ
detect.tmp = `${char}${detect.tmp}`;
}
else {
// 数字と文字列の区切り
separated.unshift(detect.tmp);
detect.mode = 'number';
detect.tmp = char;
}
}
}
else {
// 検出する最初の文字
// 判別するモード(数値 or 文字列)を設定する
detect.tmp = char;
detect.mode = char.match(/\d/) ? 'number' : 'string'; // 検出モード。符号や数値なら数字モード、それ以外なら文字列モードにする。
}
}
if (detect.tmp.length > 0) {
separated.unshift(detect.tmp);
}
return separated;
}
Also see: Tab Triggers